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
100
101
102
103
104
105
106
107
108
#[cfg(feature = "http")]
use super::Builder;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
/// A builder to add parameters when using [`GuildId::add_member`].
///
/// [Discord docs](https://discord.com/developers/docs/resources/guild#add-guild-member).
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct AddMember {
access_token: String,
#[serde(skip_serializing_if = "Option::is_none")]
nick: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
roles: Vec<RoleId>,
#[serde(skip_serializing_if = "Option::is_none")]
mute: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
deaf: Option<bool>,
}
impl AddMember {
/// Constructs a new builder with the given access token, leaving all other fields empty.
pub fn new(access_token: String) -> Self {
Self {
access_token,
nick: None,
roles: Vec::new(),
mute: None,
deaf: None,
}
}
/// Sets the OAuth2 access token for this request, replacing the current one.
///
/// Requires the access token to have the `guilds.join` scope granted.
pub fn access_token(mut self, access_token: impl Into<String>) -> Self {
self.access_token = access_token.into();
self
}
/// Sets the member's nickname.
///
/// Requires the [Manage Nicknames] permission.
///
/// [Manage Nicknames]: crate::model::permissions::Permissions::MANAGE_NICKNAMES
pub fn nickname(mut self, nickname: impl Into<String>) -> Self {
self.nick = Some(nickname.into());
self
}
/// Sets the list of roles that the member should have.
///
/// Requires the [Manage Roles] permission.
///
/// [Manage Roles]: crate::model::permissions::Permissions::MANAGE_ROLES
pub fn roles(mut self, roles: impl IntoIterator<Item = impl Into<RoleId>>) -> Self {
self.roles = roles.into_iter().map(Into::into).collect();
self
}
/// Whether to mute the member.
///
/// Requires the [Mute Members] permission.
///
/// [Mute Members]: crate::model::permissions::Permissions::MUTE_MEMBERS
pub fn mute(mut self, mute: bool) -> Self {
self.mute = Some(mute);
self
}
/// Whether to deafen the member.
///
/// Requires the [Deafen Members] permission.
///
/// [Deafen Members]: crate::model::permissions::Permissions::DEAFEN_MEMBERS
pub fn deafen(mut self, deafen: bool) -> Self {
self.deaf = Some(deafen);
self
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for AddMember {
type Context<'ctx> = (GuildId, UserId);
type Built = Option<Member>;
/// Adds a [`User`] to this guild with a valid OAuth2 access token.
///
/// Returns the created [`Member`] object, or nothing if the user is already a member of the
/// guild.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
async fn execute(
self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
cache_http.http().add_guild_member(ctx.0, ctx.1, &self).await
}
}