serenity/builder/
bot_auth_parameters.rs1use 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#[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 pub fn new() -> Self {
24 Self::default()
25 }
26
27 #[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 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 #[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 pub fn scopes(mut self, scopes: &[Scope]) -> Self {
88 self.scopes = scopes.to_vec();
89 self
90 }
91
92 pub fn permissions(mut self, permissions: Permissions) -> Self {
94 self.permissions = permissions;
95 self
96 }
97
98 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 pub fn disable_guild_select(mut self, disable: bool) -> Self {
106 self.disable_guild_select = disable;
107 self
108 }
109}