twilight_util/builder/
interaction_response_data.rs1#![allow(deprecated)]
2
3use twilight_model::{
4 application::command::CommandOptionChoice,
5 channel::message::{AllowedMentions, Component, Embed, MessageFlags},
6 http::{attachment::Attachment, interaction::InteractionResponseData},
7 poll::Poll,
8};
9
10#[derive(Clone, Debug)]
43#[deprecated = "use the `interaction_response` builders instead"]
44#[must_use = "builders have no effect if unused"]
45pub struct InteractionResponseDataBuilder(InteractionResponseData);
46
47impl InteractionResponseDataBuilder {
48 pub const fn new() -> Self {
50 Self(InteractionResponseData {
51 allowed_mentions: None,
52 attachments: None,
53 choices: None,
54 components: None,
55 content: None,
56 custom_id: None,
57 embeds: None,
58 flags: None,
59 title: None,
60 tts: None,
61 poll: None,
62 })
63 }
64
65 #[must_use = "builders have no effect if unused"]
67 pub fn build(self) -> InteractionResponseData {
68 self.0
69 }
70
71 pub fn allowed_mentions(mut self, allowed_mentions: AllowedMentions) -> Self {
75 self.0.allowed_mentions = Some(allowed_mentions);
76
77 self
78 }
79
80 pub fn attachments(mut self, attachments: impl IntoIterator<Item = Attachment>) -> Self {
84 self.0.attachments = Some(attachments.into_iter().collect());
85
86 self
87 }
88
89 pub fn choices(mut self, choices: impl IntoIterator<Item = CommandOptionChoice>) -> Self {
96 self.0.choices = Some(choices.into_iter().collect());
97
98 self
99 }
100
101 pub fn components(mut self, components: impl IntoIterator<Item = Component>) -> Self {
105 self.0.components = Some(components.into_iter().collect());
106
107 self
108 }
109
110 pub fn content(mut self, content: impl Into<String>) -> Self {
114 self.0.content = Some(content.into());
115
116 self
117 }
118
119 pub fn custom_id(mut self, custom_id: impl Into<String>) -> Self {
123 self.0.custom_id = Some(custom_id.into());
124
125 self
126 }
127
128 pub fn embeds(mut self, embeds: impl IntoIterator<Item = Embed>) -> Self {
132 self.0.embeds = Some(embeds.into_iter().collect());
133
134 self
135 }
136
137 pub const fn flags(mut self, flags: MessageFlags) -> Self {
146 self.0.flags = Some(flags);
147
148 self
149 }
150
151 pub fn title(mut self, title: impl Into<String>) -> Self {
155 self.0.title = Some(title.into());
156
157 self
158 }
159
160 pub const fn tts(mut self, value: bool) -> Self {
164 self.0.tts = Some(value);
165
166 self
167 }
168
169 pub fn poll(mut self, poll: Poll) -> Self {
171 self.0.poll = Some(poll);
172
173 self
174 }
175}
176
177impl Default for InteractionResponseDataBuilder {
178 fn default() -> Self {
179 Self::new()
180 }
181}
182
183#[cfg(test)]
184mod tests {
185 use super::*;
186 use static_assertions::assert_impl_all;
187 use std::fmt::Debug;
188 use twilight_model::{
189 channel::message::{
190 MentionType,
191 component::{Button, ButtonStyle},
192 },
193 poll::{PollLayoutType, PollMedia},
194 util::Timestamp,
195 };
196
197 assert_impl_all!(
198 InteractionResponseDataBuilder: Clone,
199 Debug,
200 Default,
201 Send,
202 Sync
203 );
204
205 #[test]
206 fn callback_data_builder() {
207 let allowed_mentions = AllowedMentions {
208 parse: Vec::from([MentionType::Everyone]),
209 ..Default::default()
210 };
211
212 let component = Component::Button(Button {
213 style: ButtonStyle::Primary,
214 emoji: None,
215 label: Some("test label".into()),
216 custom_id: Some("test custom id".into()),
217 url: None,
218 disabled: false,
219 sku_id: None,
220 id: None,
221 });
222
223 let embed = Embed {
224 author: None,
225 color: Some(123),
226 description: Some("a description".to_owned()),
227 fields: Vec::new(),
228 flags: None,
229 footer: None,
230 image: None,
231 kind: "rich".to_owned(),
232 provider: None,
233 thumbnail: None,
234 timestamp: Some(Timestamp::from_secs(1_580_608_922).unwrap()),
235 title: Some("a title".to_owned()),
236 url: Some("https://example.com".to_owned()),
237 video: None,
238 };
239
240 let poll = Poll {
241 answers: vec![],
242 allow_multiselect: false,
243 expiry: None,
244 layout_type: PollLayoutType::Default,
245 question: PollMedia {
246 emoji: None,
247 text: Some("lorem ipsum".to_owned()),
248 },
249 results: None,
250 };
251
252 let value = InteractionResponseDataBuilder::new()
253 .allowed_mentions(allowed_mentions.clone())
254 .components([component.clone()])
255 .content("a content")
256 .embeds([embed.clone()])
257 .flags(MessageFlags::empty())
258 .tts(false)
259 .poll(poll.clone())
260 .build();
261
262 let expected = InteractionResponseData {
263 allowed_mentions: Some(allowed_mentions),
264 attachments: None,
265 choices: None,
266 components: Some(vec![component]),
267 content: Some("a content".to_owned()),
268 custom_id: None,
269 embeds: Some(vec![embed]),
270 flags: Some(MessageFlags::empty()),
271 title: None,
272 tts: Some(false),
273 poll: Some(poll),
274 };
275
276 assert_eq!(value, expected);
277 }
278}