Skip to main content

twilight_http/request/application/command/
set_guild_commands.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::response::{Response, ResponseFuture, marker::ListBody};
3use crate::{
4    client::Client,
5    error::Error,
6    request::{Request, TryIntoRequest},
7    routing::Route,
8};
9use std::future::IntoFuture;
10use twilight_model::{
11    application::command::Command,
12    id::{
13        Id,
14        marker::{ApplicationMarker, GuildMarker},
15    },
16};
17
18/// Set a guild's commands.
19///
20/// This method is idempotent: it can be used on every start, without being
21/// ratelimited if there aren't changes to the commands.
22///
23/// The [`Command`] struct has an [associated builder] in the
24/// [`twilight-util`] crate.
25///
26/// [`twilight-util`]: https://docs.rs/twilight-util/latest/index.html
27/// [associated builder]: https://docs.rs/twilight-util/latest/twilight_util/builder/command/struct.CommandBuilder.html
28#[must_use = "requests must be configured and executed"]
29pub struct SetGuildCommands<'a> {
30    commands: &'a [Command],
31    application_id: Id<ApplicationMarker>,
32    guild_id: Id<GuildMarker>,
33    http: &'a Client,
34}
35
36impl<'a> SetGuildCommands<'a> {
37    pub(crate) const fn new(
38        http: &'a Client,
39        application_id: Id<ApplicationMarker>,
40        guild_id: Id<GuildMarker>,
41        commands: &'a [Command],
42    ) -> Self {
43        Self {
44            commands,
45            application_id,
46            guild_id,
47            http,
48        }
49    }
50}
51
52#[cfg(not(target_os = "wasi"))]
53impl IntoFuture for SetGuildCommands<'_> {
54    type Output = Result<Response<ListBody<Command>>, Error>;
55
56    type IntoFuture = ResponseFuture<ListBody<Command>>;
57
58    fn into_future(self) -> Self::IntoFuture {
59        let http = self.http;
60
61        match self.try_into_request() {
62            Ok(request) => http.request(request),
63            Err(source) => ResponseFuture::error(source),
64        }
65    }
66}
67
68impl TryIntoRequest for SetGuildCommands<'_> {
69    fn try_into_request(self) -> Result<Request, Error> {
70        Request::builder(&Route::SetGuildCommands {
71            application_id: self.application_id.get(),
72            guild_id: self.guild_id.get(),
73        })
74        .json(&self.commands)
75        .build()
76    }
77}