twilight_http/client/
interaction.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use crate::{
    request::application::{
        command::{
            CreateGlobalCommand, CreateGuildCommand, DeleteGlobalCommand, DeleteGuildCommand,
            GetCommandPermissions, GetGlobalCommand, GetGlobalCommands, GetGuildCommand,
            GetGuildCommandPermissions, GetGuildCommands, SetGlobalCommands, SetGuildCommands,
            UpdateCommandPermissions, UpdateGlobalCommand, UpdateGuildCommand,
        },
        interaction::{
            CreateFollowup, CreateResponse, DeleteFollowup, DeleteResponse, GetFollowup,
            GetResponse, UpdateFollowup, UpdateResponse,
        },
    },
    Client,
};
use twilight_model::{
    application::command::{permissions::CommandPermission, Command},
    http::interaction::InteractionResponse,
    id::{
        marker::{ApplicationMarker, CommandMarker, GuildMarker, InteractionMarker, MessageMarker},
        Id,
    },
};

/// Client interface for using interactions.
///
/// # Examples
///
/// Retrieve the application ID and then use an interaction request:
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::env;
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// let client = Client::new(env::var("DISCORD_TOKEN")?);
/// let application_id = Id::new(123);
///
/// let interaction_client = client.interaction(application_id);
///
/// let commands = interaction_client.global_commands().await?.models().await?;
///
/// println!("there are {} global commands", commands.len());
/// # Ok(()) }
/// ```
#[derive(Debug)]
pub struct InteractionClient<'a> {
    application_id: Id<ApplicationMarker>,
    client: &'a Client,
}

impl<'a> InteractionClient<'a> {
    /// Create a new interface for using interactions.
    pub(super) const fn new(client: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
        Self {
            application_id,
            client,
        }
    }

    /// Respond to an interaction, by its ID and token.
    ///
    /// For variants of [`InteractionResponse`] that contain
    /// [`InteractionResponseData`], there is an [associated builder] in the
    /// [`twilight-util`] crate.
    ///
    /// This endpoint is not bound to the application's global rate limit.
    ///
    /// [`InteractionResponseData`]: twilight_model::http::interaction::InteractionResponseData
    /// [`twilight-util`]: https://docs.rs/twilight-util/latest/index.html
    /// [associated builder]: https://docs.rs/twilight-util/latest/twilight_util/builder/struct.InteractionResponseDataBuilder.html
    pub const fn create_response(
        &'a self,
        interaction_id: Id<InteractionMarker>,
        interaction_token: &'a str,
        response: &'a InteractionResponse,
    ) -> CreateResponse<'a> {
        CreateResponse::new(self.client, interaction_id, interaction_token, response)
    }

    /// Delete the original message, by its token.
    ///
    /// This endpoint is not bound to the application's global rate limit.
    pub const fn delete_response(&'a self, interaction_token: &'a str) -> DeleteResponse<'a> {
        DeleteResponse::new(self.client, self.application_id, interaction_token)
    }

    /// Get the original message, by its token.
    ///
    /// This endpoint is not bound to the application's global rate limit.
    pub const fn response(&'a self, interaction_token: &'a str) -> GetResponse<'a> {
        GetResponse::new(self.client, self.application_id, interaction_token)
    }

    /// Edit the original message, by its token.
    ///
    /// The update must include at least one of [`attachments`], [`components`],
    /// [`content`] or [`embeds`].
    ///
    /// This endpoint is not bound to the application's global rate limit.
    ///
    /// [`attachments`]: CreateFollowup::attachments
    /// [`components`]: CreateFollowup::components
    /// [`content`]: CreateFollowup::content
    /// [`embeds`]: CreateFollowup::embeds
    pub const fn update_response(&'a self, interaction_token: &'a str) -> UpdateResponse<'a> {
        UpdateResponse::new(self.client, self.application_id, interaction_token)
    }

    /// Create a followup message to an interaction, by its token.
    ///
    /// The message must include at least one of [`attachments`], [`components`]
    /// [`content`] or [`embeds`].
    ///
    /// This endpoint is not bound to the application's global rate limit.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use std::env;
    /// use twilight_http::Client;
    /// use twilight_model::id::Id;
    ///
    /// let client = Client::new(env::var("DISCORD_TOKEN")?);
    /// let application_id = Id::new(1);
    ///
    /// client
    ///     .interaction(application_id)
    ///     .create_followup("webhook token")
    ///     .content("Pinkie...")
    ///     .await?;
    /// # Ok(()) }
    /// ```
    ///
    /// [`attachments`]: CreateFollowup::attachments
    /// [`components`]: CreateFollowup::components
    /// [`content`]: CreateFollowup::content
    /// [`embeds`]: CreateFollowup::embeds
    pub const fn create_followup(&'a self, interaction_token: &'a str) -> CreateFollowup<'a> {
        CreateFollowup::new(self.client, self.application_id, interaction_token)
    }

    /// Delete a followup message to an interaction, by its token and message
    /// ID.
    ///
    /// This endpoint is not bound to the application's global rate limit.
    pub const fn delete_followup(
        &'a self,
        interaction_token: &'a str,
        message_id: Id<MessageMarker>,
    ) -> DeleteFollowup<'a> {
        DeleteFollowup::new(
            self.client,
            self.application_id,
            interaction_token,
            message_id,
        )
    }

    /// Get a followup message of an interaction, by its token and the message
    /// ID.
    ///
    /// This endpoint is not bound to the application's global rate limit.
    pub const fn followup(
        &'a self,
        interaction_token: &'a str,
        message_id: Id<MessageMarker>,
    ) -> GetFollowup<'a> {
        GetFollowup::new(
            self.client,
            self.application_id,
            interaction_token,
            message_id,
        )
    }

    /// Edit a followup message of an interaction, by its token and the message
    /// ID.
    ///
    /// This endpoint is not bound to the application's global rate limit.
    pub const fn update_followup(
        &'a self,
        interaction_token: &'a str,
        message_id: Id<MessageMarker>,
    ) -> UpdateFollowup<'a> {
        UpdateFollowup::new(
            self.client,
            self.application_id,
            interaction_token,
            message_id,
        )
    }

    /// Create a new global command.
    pub const fn create_global_command(&'a self) -> CreateGlobalCommand<'a> {
        CreateGlobalCommand::new(self.client, self.application_id)
    }

    /// Delete a global command, by ID.
    pub const fn delete_global_command(
        &self,
        command_id: Id<CommandMarker>,
    ) -> DeleteGlobalCommand<'_> {
        DeleteGlobalCommand::new(self.client, self.application_id, command_id)
    }

    /// Fetch a global command for your application.
    pub const fn global_command(&self, command_id: Id<CommandMarker>) -> GetGlobalCommand<'_> {
        GetGlobalCommand::new(self.client, self.application_id, command_id)
    }

    /// Fetch all global commands for your application.
    pub const fn global_commands(&self) -> GetGlobalCommands<'_> {
        GetGlobalCommands::new(self.client, self.application_id)
    }

    /// Set global commands.
    ///
    /// This method is idempotent: it can be used on every start, without being
    /// ratelimited if there aren't changes to the commands.
    ///
    /// The [`Command`] struct has an [associated builder] in the
    /// [`twilight-util`] crate.
    ///
    /// [`twilight-util`]: https://docs.rs/twilight-util/latest/index.html
    /// [associated builder]: https://docs.rs/twilight-util/latest/twilight_util/builder/command/struct.CommandBuilder.html
    pub const fn set_global_commands(&'a self, commands: &'a [Command]) -> SetGlobalCommands<'a> {
        SetGlobalCommands::new(self.client, self.application_id, commands)
    }

    /// Edit a global command, by ID.
    ///
    /// You must specify a name and description. See
    /// [Discord Docs/Edit Global Application Command].
    ///
    /// [Discord Docs/Edit Global Application Command]: https://discord.com/developers/docs/interactions/application-commands#edit-global-application-command
    pub const fn update_global_command(
        &self,
        command_id: Id<CommandMarker>,
    ) -> UpdateGlobalCommand<'_> {
        UpdateGlobalCommand::new(self.client, self.application_id, command_id)
    }

    /// Create a new command in a guild.
    pub const fn create_guild_command(
        &'a self,
        guild_id: Id<GuildMarker>,
    ) -> CreateGuildCommand<'a> {
        CreateGuildCommand::new(self.client, self.application_id, guild_id)
    }

    /// Delete a command in a guild, by ID.
    pub const fn delete_guild_command(
        &self,
        guild_id: Id<GuildMarker>,
        command_id: Id<CommandMarker>,
    ) -> DeleteGuildCommand<'_> {
        DeleteGuildCommand::new(self.client, self.application_id, guild_id, command_id)
    }

    /// Fetch a guild command for your application.
    pub const fn guild_command(
        &self,
        guild_id: Id<GuildMarker>,
        command_id: Id<CommandMarker>,
    ) -> GetGuildCommand<'_> {
        GetGuildCommand::new(self.client, self.application_id, guild_id, command_id)
    }

    /// Fetch all commands for a guild, by ID.
    pub const fn guild_commands(&self, guild_id: Id<GuildMarker>) -> GetGuildCommands<'_> {
        GetGuildCommands::new(self.client, self.application_id, guild_id)
    }

    /// Set a guild's commands.
    ///
    /// This method is idempotent: it can be used on every start, without being
    /// ratelimited if there aren't changes to the commands.
    ///
    /// The [`Command`] struct has an [associated builder] in the
    /// [`twilight-util`] crate.
    ///
    /// [`twilight-util`]: https://docs.rs/twilight_util/index.html
    /// [associated builder]: https://docs.rs/twilight-util/latest/twilight_util/builder/command/struct.CommandBuilder.html
    pub const fn set_guild_commands(
        &'a self,
        guild_id: Id<GuildMarker>,
        commands: &'a [Command],
    ) -> SetGuildCommands<'a> {
        SetGuildCommands::new(self.client, self.application_id, guild_id, commands)
    }

    /// Edit a command in a guild, by ID.
    ///
    /// You must specify a name and description. See
    /// [Discord Docs/Edit Guild Application Command].
    ///
    /// [Discord Docs/Edit Guild Application Command]: https://discord.com/developers/docs/interactions/application-commands#edit-guild-application-command
    pub const fn update_guild_command(
        &self,
        guild_id: Id<GuildMarker>,
        command_id: Id<CommandMarker>,
    ) -> UpdateGuildCommand<'_> {
        UpdateGuildCommand::new(self.client, self.application_id, guild_id, command_id)
    }

    /// Fetch command permissions for a command from the current application
    /// in a guild.
    pub const fn command_permissions(
        &self,
        guild_id: Id<GuildMarker>,
        command_id: Id<CommandMarker>,
    ) -> GetCommandPermissions<'_> {
        GetCommandPermissions::new(self.client, self.application_id, guild_id, command_id)
    }

    /// Fetch command permissions for all commands from the current
    /// application in a guild.
    pub const fn guild_command_permissions(
        &self,
        guild_id: Id<GuildMarker>,
    ) -> GetGuildCommandPermissions<'_> {
        GetGuildCommandPermissions::new(self.client, self.application_id, guild_id)
    }

    /// Update command permissions for a single command in a guild.
    ///
    /// This overwrites the command permissions so the full set of permissions
    /// have to be sent every time.
    ///
    /// This request requires that the client was configured with an OAuth2 Bearer
    /// token.
    ///
    /// # Errors
    ///
    /// Returns an error of type [`PermissionsCountInvalid`] if the permissions
    /// are invalid.
    ///
    /// [`PermissionsCountInvalid`]: twilight_validate::command::CommandValidationErrorType::PermissionsCountInvalid
    pub fn update_command_permissions(
        &'a self,
        guild_id: Id<GuildMarker>,
        command_id: Id<CommandMarker>,
        permissions: &'a [CommandPermission],
    ) -> UpdateCommandPermissions<'a> {
        UpdateCommandPermissions::new(
            self.client,
            self.application_id,
            guild_id,
            command_id,
            permissions,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::InteractionClient;
    use static_assertions::assert_impl_all;
    use std::fmt::Debug;

    assert_impl_all!(InteractionClient<'_>: Debug, Send, Sync);
}