1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use url::Url;
use crate::http::client::Http;
use crate::internal::prelude::*;
use crate::model::prelude::*;
#[derive(Debug, Clone, Default)]
pub struct CreateBotAuthParameters {
client_id: UserId,
scopes: Vec<OAuth2Scope>,
permissions: Permissions,
guild_id: GuildId,
disable_guild_select: bool,
}
impl CreateBotAuthParameters {
pub fn build(self) -> String {
let mut valid_data = vec![];
let bits = self.permissions.bits();
if self.client_id.0 != 0 {
valid_data.push(("client_id", self.client_id.0.to_string()));
}
if !self.scopes.is_empty() {
valid_data.push((
"scope",
self.scopes.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(" "),
));
}
if bits != 0 {
valid_data.push(("permissions", bits.to_string()));
}
if self.guild_id.0 != 0 {
valid_data.push(("guild", self.guild_id.0.to_string()));
}
if self.disable_guild_select {
valid_data.push(("disable_guild_select", self.disable_guild_select.to_string()));
}
let url = Url::parse_with_params("https://discord.com/api/oauth2/authorize", &valid_data)
.expect("failed to construct URL");
url.to_string()
}
pub fn client_id<U: Into<UserId>>(&mut self, client_id: U) -> &mut Self {
self.client_id = client_id.into();
self
}
pub async fn auto_client_id(&mut self, http: impl AsRef<Http>) -> Result<&mut Self> {
self.client_id = http.as_ref().get_current_application_info().await.map(|v| v.id)?;
Ok(self)
}
pub fn scopes(&mut self, scopes: &[OAuth2Scope]) -> &mut Self {
self.scopes = scopes.to_vec();
self
}
pub fn permissions(&mut self, permissions: Permissions) -> &mut Self {
self.permissions = permissions;
self
}
pub fn guild_id<G: Into<GuildId>>(&mut self, guild_id: G) -> &mut Self {
self.guild_id = guild_id.into();
self
}
pub fn disable_guild_select(&mut self, disable: bool) -> &mut Self {
self.disable_guild_select = disable;
self
}
}