djs_rest/endpoints/interaction.rs
1//! [Interaction endpoints](https://docs.discord.com/developers/interactions/receiving-and-responding#endpoints):
2//! the initial response, the original response and follow-up messages.
3//!
4//! None of them count towards the global rate limit, and initial responses are never queued
5//! behind one another (they must arrive within 3 seconds). Interaction tokens are valid for
6//! 15 minutes.
7
8use djs_model::{
9 id::{ApplicationId, InteractionId, MessageId},
10 interaction::{InteractionCallbackResponse, InteractionResponse},
11 message::Message,
12 rest::{CreateFollowupMessage, EditWebhookMessage},
13};
14
15use super::webhook::WebhookQuery;
16use crate::client::boxed;
17use crate::{
18 Error, Request, Rest, Route,
19 query::{Query, QueryWriter},
20};
21
22/// `?with_response=true` of Create Interaction Response (D-106).
23#[derive(Debug)]
24struct WithResponse;
25
26impl Query for WithResponse {
27 fn write(&self, query: &mut QueryWriter<'_>) {
28 query.pair("with_response", &true);
29 }
30}
31
32impl Rest {
33 /// [Create Interaction Response](https://docs.discord.com/developers/interactions/receiving-and-responding#create-interaction-response):
34 /// the initial response to an interaction, within 3 seconds of receiving it. Files of
35 /// the response's message are uploaded with it.
36 ///
37 /// To get the callback (e.g. the created message), use
38 /// [`create_interaction_response_with_response`](Self::create_interaction_response_with_response).
39 ///
40 /// discord.js counterpart: `reply`, `deferReply`, `update`, `deferUpdate`, `showModal`,
41 /// `respond` of the interaction classes.
42 ///
43 /// # Examples
44 ///
45 /// ```no_run
46 /// # async fn example(rest: djs_rest::Rest, interaction: djs_model::interaction::Interaction) -> Result<(), djs_rest::Error> {
47 /// use djs_model::interaction::{InteractionResponse, MessageCallbackData};
48 ///
49 /// let reply = InteractionResponse::ChannelMessageWithSource(Box::new(
50 /// MessageCallbackData::from("Pong!"),
51 /// ));
52 /// rest.create_interaction_response(interaction.id, &interaction.token, &reply)
53 /// .await?;
54 /// # Ok(())
55 /// # }
56 /// ```
57 ///
58 /// # Errors
59 ///
60 /// See [`Rest::request`]; `40060` if the interaction was already acknowledged, `10062`
61 /// Unknown Interaction once it expired.
62 pub fn create_interaction_response<'a>(
63 &'a self,
64 interaction_id: InteractionId,
65 interaction_token: &'a str,
66 response: &'a InteractionResponse,
67 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
68 boxed(async move {
69 self.call(callback(interaction_id, interaction_token, response)?)
70 .await
71 })
72 }
73
74 /// [Create Interaction Response](https://docs.discord.com/developers/interactions/receiving-and-responding#create-interaction-response)
75 /// with `with_response`: like
76 /// [`create_interaction_response`](Self::create_interaction_response), but returns the
77 /// [callback](InteractionCallbackResponse) (D-106), which holds e.g. the created message.
78 ///
79 /// discord.js counterpart: the same methods with `withResponse: true`.
80 ///
81 /// # Errors
82 ///
83 /// See [`create_interaction_response`](Self::create_interaction_response).
84 pub fn create_interaction_response_with_response<'a>(
85 &'a self,
86 interaction_id: InteractionId,
87 interaction_token: &'a str,
88 response: &'a InteractionResponse,
89 ) -> impl Future<Output = Result<InteractionCallbackResponse, Error>> + Send + 'a {
90 boxed(async move {
91 self.call(callback(interaction_id, interaction_token, response)?.query(&WithResponse))
92 .await
93 })
94 }
95
96 /// [Get Original Interaction Response](https://docs.discord.com/developers/interactions/receiving-and-responding#get-original-interaction-response):
97 /// returns the message of the initial response.
98 ///
99 /// discord.js counterpart: `fetchReply`.
100 ///
101 /// # Errors
102 ///
103 /// See [`Rest::request`].
104 pub fn get_original_interaction_response<'a>(
105 &'a self,
106 application_id: ApplicationId,
107 interaction_token: &'a str,
108 ) -> impl Future<Output = Result<Message, Error>> + Send + 'a {
109 boxed(async move {
110 self.call(Request::new(Route::GetOriginalInteractionResponse {
111 application_id,
112 interaction_token,
113 }))
114 .await
115 })
116 }
117
118 /// [Edit Original Interaction Response](https://docs.discord.com/developers/interactions/receiving-and-responding#edit-original-interaction-response):
119 /// edits the message of the initial response (also the way to answer a deferred
120 /// response).
121 ///
122 /// discord.js counterpart: `editReply`.
123 ///
124 /// # Errors
125 ///
126 /// See [`Rest::request`].
127 pub fn edit_original_interaction_response<'a>(
128 &'a self,
129 application_id: ApplicationId,
130 interaction_token: &'a str,
131 body: &'a EditWebhookMessage,
132 ) -> impl Future<Output = Result<Message, Error>> + Send + 'a {
133 boxed(async move {
134 let query = WebhookQuery::of_edit(body);
135 self.call(
136 Request::new(Route::EditOriginalInteractionResponse {
137 application_id,
138 interaction_token,
139 })
140 .query(&query)
141 .json(body)?
142 .files(&body.files),
143 )
144 .await
145 })
146 }
147
148 /// [Delete Original Interaction Response](https://docs.discord.com/developers/interactions/receiving-and-responding#delete-original-interaction-response):
149 /// deletes the message of the initial response.
150 ///
151 /// discord.js counterpart: `deleteReply`.
152 ///
153 /// # Errors
154 ///
155 /// See [`Rest::request`].
156 pub fn delete_original_interaction_response<'a>(
157 &'a self,
158 application_id: ApplicationId,
159 interaction_token: &'a str,
160 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
161 boxed(async move {
162 self.call(Request::new(Route::DeleteOriginalInteractionResponse {
163 application_id,
164 interaction_token,
165 }))
166 .await
167 })
168 }
169
170 /// [Create Followup Message](https://docs.discord.com/developers/interactions/receiving-and-responding#create-followup-message):
171 /// sends another message for the interaction and returns it.
172 ///
173 /// Apps installed only for the user (not in the guild) may send at most 5 follow-ups per
174 /// interaction.
175 ///
176 /// discord.js counterpart: `followUp`.
177 ///
178 /// # Errors
179 ///
180 /// See [`Rest::request`]; `40094` once the maximum number of follow-ups is reached.
181 pub fn create_followup_message<'a>(
182 &'a self,
183 application_id: ApplicationId,
184 interaction_token: &'a str,
185 body: &'a CreateFollowupMessage,
186 ) -> impl Future<Output = Result<Message, Error>> + Send + 'a {
187 boxed(async move {
188 self.call(
189 Request::new(Route::CreateFollowupMessage {
190 application_id,
191 interaction_token,
192 })
193 .json(body)?
194 .files(&body.files),
195 )
196 .await
197 })
198 }
199
200 /// [Get Followup Message](https://docs.discord.com/developers/interactions/receiving-and-responding#get-followup-message):
201 /// returns a follow-up message.
202 ///
203 /// discord.js counterpart: `fetchReply` with a message ID.
204 ///
205 /// # Errors
206 ///
207 /// See [`Rest::request`].
208 pub fn get_followup_message<'a>(
209 &'a self,
210 application_id: ApplicationId,
211 interaction_token: &'a str,
212 message_id: MessageId,
213 ) -> impl Future<Output = Result<Message, Error>> + Send + 'a {
214 boxed(async move {
215 self.call(Request::new(Route::GetFollowupMessage {
216 application_id,
217 interaction_token,
218 message_id,
219 }))
220 .await
221 })
222 }
223
224 /// [Edit Followup Message](https://docs.discord.com/developers/interactions/receiving-and-responding#edit-followup-message):
225 /// edits a follow-up message.
226 ///
227 /// discord.js counterpart: `editReply` with a message ID / `InteractionWebhook#editMessage`.
228 ///
229 /// # Errors
230 ///
231 /// See [`Rest::request`].
232 pub fn edit_followup_message<'a>(
233 &'a self,
234 application_id: ApplicationId,
235 interaction_token: &'a str,
236 message_id: MessageId,
237 body: &'a EditWebhookMessage,
238 ) -> impl Future<Output = Result<Message, Error>> + Send + 'a {
239 boxed(async move {
240 let query = WebhookQuery::of_edit(body);
241 self.call(
242 Request::new(Route::EditFollowupMessage {
243 application_id,
244 interaction_token,
245 message_id,
246 })
247 .query(&query)
248 .json(body)?
249 .files(&body.files),
250 )
251 .await
252 })
253 }
254
255 /// [Delete Followup Message](https://docs.discord.com/developers/interactions/receiving-and-responding#delete-followup-message):
256 /// deletes a follow-up message.
257 ///
258 /// discord.js counterpart: `deleteReply` with a message ID.
259 ///
260 /// # Errors
261 ///
262 /// See [`Rest::request`].
263 pub fn delete_followup_message<'a>(
264 &'a self,
265 application_id: ApplicationId,
266 interaction_token: &'a str,
267 message_id: MessageId,
268 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
269 boxed(async move {
270 self.call(Request::new(Route::DeleteFollowupMessage {
271 application_id,
272 interaction_token,
273 message_id,
274 }))
275 .await
276 })
277 }
278}
279
280/// The request of Create Interaction Response, with the files of the response's message.
281fn callback<'a>(
282 interaction_id: InteractionId,
283 interaction_token: &'a str,
284 response: &'a InteractionResponse,
285) -> Result<Request<'a>, Error> {
286 Ok(Request::new(Route::CreateInteractionResponse {
287 interaction_id,
288 interaction_token,
289 })
290 .json(response)?
291 .files(response.files()))
292}