twilight_http/request/application/command/
set_guild_commands.rs

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