Skip to main content

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