serenity/builder/
bot_auth_parameters.rs

1use arrayvec::ArrayVec;
2use url::Url;
3
4#[cfg(feature = "http")]
5use crate::http::Http;
6#[cfg(feature = "http")]
7use crate::internal::prelude::*;
8use crate::model::prelude::*;
9
10/// A builder for constructing an invite link with custom OAuth2 scopes.
11#[derive(Debug, Clone, Default)]
12#[must_use]
13pub struct CreateBotAuthParameters {
14    client_id: Option<ApplicationId>,
15    scopes: Vec<Scope>,
16    permissions: Permissions,
17    guild_id: Option<GuildId>,
18    disable_guild_select: bool,
19}
20
21impl CreateBotAuthParameters {
22    /// Equivalent to [`Self::default`].
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Builds the url with the provided data.
28    #[must_use]
29    pub fn build(self) -> String {
30        let mut valid_data = ArrayVec::<_, 5>::new();
31        let bits = self.permissions.bits();
32
33        if let Some(client_id) = self.client_id {
34            valid_data.push(("client_id", client_id.to_string()));
35        }
36
37        if !self.scopes.is_empty() {
38            valid_data.push((
39                "scope",
40                self.scopes.iter().map(ToString::to_string).collect::<Vec<_>>().join(" "),
41            ));
42        }
43
44        if bits != 0 {
45            valid_data.push(("permissions", bits.to_string()));
46        }
47
48        if let Some(guild_id) = self.guild_id {
49            valid_data.push(("guild", guild_id.to_string()));
50        }
51
52        if self.disable_guild_select {
53            valid_data.push(("disable_guild_select", self.disable_guild_select.to_string()));
54        }
55
56        let url = Url::parse_with_params("https://discord.com/api/oauth2/authorize", &valid_data)
57            .expect("failed to construct URL");
58
59        url.to_string()
60    }
61
62    /// Specify the client Id of your application.
63    pub fn client_id(mut self, client_id: impl Into<ApplicationId>) -> Self {
64        self.client_id = Some(client_id.into());
65        self
66    }
67
68    /// Automatically fetch and set the client Id of your application by inquiring Discord's API.
69    ///
70    /// # Errors
71    ///
72    /// Returns an [`HttpError::UnsuccessfulRequest`] if the user is not authorized for this
73    /// endpoint.
74    ///
75    /// [`HttpError::UnsuccessfulRequest`]: crate::http::HttpError::UnsuccessfulRequest
76    #[cfg(feature = "http")]
77    pub async fn auto_client_id(mut self, http: impl AsRef<Http>) -> Result<Self> {
78        self.client_id = http.as_ref().get_current_application_info().await.map(|v| Some(v.id))?;
79        Ok(self)
80    }
81
82    /// Specify the scopes for your application.
83    ///
84    /// **Note**: This needs to include the [`Bot`] scope.
85    ///
86    /// [`Bot`]: Scope::Bot
87    pub fn scopes(mut self, scopes: &[Scope]) -> Self {
88        self.scopes = scopes.to_vec();
89        self
90    }
91
92    /// Specify the permissions your application requires.
93    pub fn permissions(mut self, permissions: Permissions) -> Self {
94        self.permissions = permissions;
95        self
96    }
97
98    /// Specify the Id of the guild to prefill the dropdown picker for the user.
99    pub fn guild_id(mut self, guild_id: impl Into<GuildId>) -> Self {
100        self.guild_id = Some(guild_id.into());
101        self
102    }
103
104    /// Specify whether the user cannot change the guild in the dropdown picker.
105    pub fn disable_guild_select(mut self, disable: bool) -> Self {
106        self.disable_guild_select = disable;
107        self
108    }
109}