djs_rs/manager/command.rs
1//! [`CommandManager`] and [`GuildCommandManager`] (D-244).
2
3use std::future::Future;
4
5use djs_cache::Backend;
6use djs_collection::Collection;
7use djs_model::{
8 application_command::{ApplicationCommand, GuildCommandPermissions},
9 id::{ApplicationId, CommandId, GenericId, GuildId},
10 rest::{CreateApplicationCommand, EditApplicationCommand, GetApplicationCommands},
11};
12use djs_rest::Error;
13
14use super::Fetch;
15use crate::Context;
16
17fn by_id(commands: Vec<ApplicationCommand>) -> Collection<CommandId, ApplicationCommand> {
18 commands
19 .into_iter()
20 .map(|command| (command.id, command))
21 .collect()
22}
23
24/// The bot's global application commands. Returned by [`Context::commands`].
25///
26/// The cache does not keep commands, so the [`djs-model`](djs_model) [`ApplicationCommand`]
27/// is returned. Build the bodies with `djs_rs::builders` (`SlashCommandBuilder`, …).
28///
29/// discord.js counterpart: `ApplicationCommandManager` (`client.application.commands`).
30#[derive(Debug)]
31pub struct CommandManager<'a, B: Backend> {
32 ctx: &'a Context<B>,
33 application_id: ApplicationId,
34}
35
36impl<B: Backend> Clone for CommandManager<'_, B> {
37 fn clone(&self) -> Self {
38 *self
39 }
40}
41
42impl<B: Backend> Copy for CommandManager<'_, B> {}
43
44impl<'a, B: Backend> CommandManager<'a, B> {
45 pub(crate) const fn new(ctx: &'a Context<B>, application_id: ApplicationId) -> Self {
46 Self {
47 ctx,
48 application_id,
49 }
50 }
51
52 /// The application these commands belong to.
53 ///
54 /// discord.js counterpart: `ClientApplication#id`, whose commands `ApplicationCommandManager` holds.
55 #[must_use]
56 pub const fn application_id(self) -> ApplicationId {
57 self.application_id
58 }
59
60 /// A global command ([Get Global Application
61 /// Command](djs_rest::Rest::get_global_application_command)).
62 ///
63 /// discord.js counterpart: `ApplicationCommandManager#fetch(id)`.
64 ///
65 /// # Errors
66 ///
67 /// Returns the REST error if the request failed.
68 pub fn fetch(
69 self,
70 command_id: CommandId,
71 ) -> Fetch<
72 ApplicationCommand,
73 impl Future<Output = Result<ApplicationCommand, Error>> + Send + 'a,
74 > {
75 let Self {
76 ctx,
77 application_id,
78 } = self;
79 Fetch::new(None, async move {
80 ctx.rest()
81 .get_global_application_command(application_id, command_id)
82 .await
83 })
84 }
85
86 /// Every global command ([Get Global Application
87 /// Commands](djs_rest::Rest::get_global_application_commands)).
88 ///
89 /// discord.js counterpart: `ApplicationCommandManager#fetch()`.
90 ///
91 /// # Errors
92 ///
93 /// Returns the REST error if the request failed.
94 pub async fn list(
95 self,
96 query: &GetApplicationCommands,
97 ) -> Result<Collection<CommandId, ApplicationCommand>, Error> {
98 let commands = self
99 .ctx
100 .rest()
101 .get_global_application_commands(self.application_id, query)
102 .await?;
103 Ok(by_id(commands))
104 }
105
106 /// Creates a global command, or replaces the one with the same name and type ([Create
107 /// Global Application Command](djs_rest::Rest::create_global_application_command)).
108 ///
109 /// discord.js counterpart: `ApplicationCommandManager#create`.
110 ///
111 /// # Errors
112 ///
113 /// Returns the REST error if the request failed (at most 200 creations a day).
114 pub async fn create(
115 self,
116 body: &CreateApplicationCommand,
117 ) -> Result<ApplicationCommand, Error> {
118 self.ctx
119 .rest()
120 .create_global_application_command(self.application_id, body)
121 .await
122 }
123
124 /// Edits a global command ([Edit Global Application
125 /// Command](djs_rest::Rest::edit_global_application_command)).
126 ///
127 /// discord.js counterpart: `ApplicationCommandManager#edit`.
128 ///
129 /// # Errors
130 ///
131 /// Returns the REST error if the request failed.
132 pub async fn edit(
133 self,
134 command_id: CommandId,
135 body: &EditApplicationCommand,
136 ) -> Result<ApplicationCommand, Error> {
137 self.ctx
138 .rest()
139 .edit_global_application_command(self.application_id, command_id, body)
140 .await
141 }
142
143 /// Deletes a global command ([Delete Global Application
144 /// Command](djs_rest::Rest::delete_global_application_command)).
145 ///
146 /// discord.js counterpart: `ApplicationCommandManager#delete`.
147 ///
148 /// # Errors
149 ///
150 /// Returns the REST error if the request failed.
151 pub async fn delete(self, command_id: CommandId) -> Result<(), Error> {
152 self.ctx
153 .rest()
154 .delete_global_application_command(self.application_id, command_id)
155 .await
156 }
157
158 /// Replaces every global command with `commands` ([Bulk Overwrite Global Application
159 /// Commands](djs_rest::Rest::bulk_overwrite_global_application_commands)); an empty slice
160 /// deletes them all. Commands left unchanged keep their ID.
161 ///
162 /// discord.js counterpart: `ApplicationCommandManager#set`.
163 ///
164 /// # Errors
165 ///
166 /// Returns the REST error if the request failed.
167 pub async fn set(
168 self,
169 commands: &[CreateApplicationCommand],
170 ) -> Result<Collection<CommandId, ApplicationCommand>, Error> {
171 let commands = self
172 .ctx
173 .rest()
174 .bulk_overwrite_global_application_commands(self.application_id, commands)
175 .await?;
176 Ok(by_id(commands))
177 }
178}
179
180/// The bot's application commands in one guild, and the command permissions there.
181/// Returned by [`Context::guild_commands`].
182///
183/// Guild commands update at once (global ones may take a while), which makes them the
184/// usual choice while developing.
185///
186/// discord.js counterpart: `GuildApplicationCommandManager` (`guild.commands`).
187#[derive(Debug)]
188pub struct GuildCommandManager<'a, B: Backend> {
189 ctx: &'a Context<B>,
190 application_id: ApplicationId,
191 guild_id: GuildId,
192}
193
194impl<B: Backend> Clone for GuildCommandManager<'_, B> {
195 fn clone(&self) -> Self {
196 *self
197 }
198}
199
200impl<B: Backend> Copy for GuildCommandManager<'_, B> {}
201
202impl<'a, B: Backend> GuildCommandManager<'a, B> {
203 pub(crate) const fn new(
204 ctx: &'a Context<B>,
205 application_id: ApplicationId,
206 guild_id: GuildId,
207 ) -> Self {
208 Self {
209 ctx,
210 application_id,
211 guild_id,
212 }
213 }
214
215 /// The guild these commands belong to.
216 ///
217 /// discord.js counterpart: `GuildApplicationCommandManager#guild`.
218 #[must_use]
219 pub const fn guild_id(self) -> GuildId {
220 self.guild_id
221 }
222
223 /// A guild command ([Get Guild Application
224 /// Command](djs_rest::Rest::get_guild_application_command)).
225 ///
226 /// discord.js counterpart: `GuildApplicationCommandManager#fetch(id)`.
227 ///
228 /// # Errors
229 ///
230 /// Returns the REST error if the request failed.
231 pub fn fetch(
232 self,
233 command_id: CommandId,
234 ) -> Fetch<
235 ApplicationCommand,
236 impl Future<Output = Result<ApplicationCommand, Error>> + Send + 'a,
237 > {
238 let Self {
239 ctx,
240 application_id,
241 guild_id,
242 } = self;
243 Fetch::new(None, async move {
244 ctx.rest()
245 .get_guild_application_command(application_id, guild_id, command_id)
246 .await
247 })
248 }
249
250 /// Every command of the guild ([Get Guild Application
251 /// Commands](djs_rest::Rest::get_guild_application_commands)).
252 ///
253 /// discord.js counterpart: `GuildApplicationCommandManager#fetch()`.
254 ///
255 /// # Errors
256 ///
257 /// Returns the REST error if the request failed.
258 pub async fn list(
259 self,
260 query: &GetApplicationCommands,
261 ) -> Result<Collection<CommandId, ApplicationCommand>, Error> {
262 let commands = self
263 .ctx
264 .rest()
265 .get_guild_application_commands(self.application_id, self.guild_id, query)
266 .await?;
267 Ok(by_id(commands))
268 }
269
270 /// Creates a guild command, or replaces the one with the same name and type ([Create
271 /// Guild Application Command](djs_rest::Rest::create_guild_application_command)).
272 ///
273 /// discord.js counterpart: `GuildApplicationCommandManager#create`.
274 ///
275 /// # Errors
276 ///
277 /// Returns the REST error if the request failed.
278 pub async fn create(
279 self,
280 body: &CreateApplicationCommand,
281 ) -> Result<ApplicationCommand, Error> {
282 self.ctx
283 .rest()
284 .create_guild_application_command(self.application_id, self.guild_id, body)
285 .await
286 }
287
288 /// Edits a guild command ([Edit Guild Application
289 /// Command](djs_rest::Rest::edit_guild_application_command)).
290 ///
291 /// discord.js counterpart: `GuildApplicationCommandManager#edit`.
292 ///
293 /// # Errors
294 ///
295 /// Returns the REST error if the request failed.
296 pub async fn edit(
297 self,
298 command_id: CommandId,
299 body: &EditApplicationCommand,
300 ) -> Result<ApplicationCommand, Error> {
301 self.ctx
302 .rest()
303 .edit_guild_application_command(self.application_id, self.guild_id, command_id, body)
304 .await
305 }
306
307 /// Deletes a guild command ([Delete Guild Application
308 /// Command](djs_rest::Rest::delete_guild_application_command)).
309 ///
310 /// discord.js counterpart: `GuildApplicationCommandManager#delete`.
311 ///
312 /// # Errors
313 ///
314 /// Returns the REST error if the request failed.
315 pub async fn delete(self, command_id: CommandId) -> Result<(), Error> {
316 self.ctx
317 .rest()
318 .delete_guild_application_command(self.application_id, self.guild_id, command_id)
319 .await
320 }
321
322 /// Replaces every command of the guild with `commands` ([Bulk Overwrite Guild
323 /// Application Commands](djs_rest::Rest::bulk_overwrite_guild_application_commands)).
324 ///
325 /// discord.js counterpart: `GuildApplicationCommandManager#set`.
326 ///
327 /// # Errors
328 ///
329 /// Returns the REST error if the request failed.
330 pub async fn set(
331 self,
332 commands: &[CreateApplicationCommand],
333 ) -> Result<Collection<CommandId, ApplicationCommand>, Error> {
334 let commands = self
335 .ctx
336 .rest()
337 .bulk_overwrite_guild_application_commands(self.application_id, self.guild_id, commands)
338 .await?;
339 Ok(by_id(commands))
340 }
341
342 /// The command permissions set in the guild, by command (the application's ID for the
343 /// permissions of every command) ([Get Guild Application Command
344 /// Permissions](djs_rest::Rest::get_guild_application_command_permissions)). Editing them
345 /// needs a user's `OAuth2` token, so it is left to
346 /// [`Rest::edit_application_command_permissions`](djs_rest::Rest::edit_application_command_permissions).
347 ///
348 /// discord.js counterpart: `ApplicationCommandPermissionsManager#fetch({ guild })`.
349 ///
350 /// # Errors
351 ///
352 /// Returns the REST error if the request failed.
353 pub async fn permissions(
354 self,
355 ) -> Result<Collection<GenericId, GuildCommandPermissions>, Error> {
356 let permissions = self
357 .ctx
358 .rest()
359 .get_guild_application_command_permissions(self.application_id, self.guild_id)
360 .await?;
361 Ok(permissions
362 .into_iter()
363 .map(|permissions| (permissions.id, permissions))
364 .collect())
365 }
366
367 /// The permissions of one command in the guild ([Get Application Command
368 /// Permissions](djs_rest::Rest::get_application_command_permissions)), global or guild.
369 ///
370 /// discord.js counterpart: `ApplicationCommandPermissionsManager#fetch({ guild, command })`.
371 ///
372 /// # Errors
373 ///
374 /// Returns the REST error if the request failed (404 when none are set).
375 pub async fn command_permissions(
376 self,
377 command_id: CommandId,
378 ) -> Result<GuildCommandPermissions, Error> {
379 self.ctx
380 .rest()
381 .get_application_command_permissions(self.application_id, self.guild_id, command_id)
382 .await
383 }
384}