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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
const TextBasedChannel = require('./interface/TextBasedChannel');
const Role = require('./Role');
const EvaluatedPermissions = require('./EvaluatedPermissions');
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const Presence = require('./Presence').Presence;
/**
* Represents a member of a guild on Discord
* @implements {TextBasedChannel}
*/
class GuildMember {
constructor(guild, data) {
/**
* The Client that instantiated this GuildMember
* @name GuildMember#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: guild.client });
/**
* The guild that this member is part of
* @type {Guild}
*/
this.guild = guild;
/**
* The user that this guild member instance Represents
* @type {User}
*/
this.user = {};
this._roles = [];
if (data) this.setup(data);
/**
* The ID of the last message sent by the member in their guild, if one was sent.
* @type {?string}
*/
this.lastMessageID = null;
}
setup(data) {
/**
* Whether this member is deafened server-wide
* @type {boolean}
*/
this.serverDeaf = data.deaf;
/**
* Whether this member is muted server-wide
* @type {boolean}
*/
this.serverMute = data.mute;
/**
* Whether this member is self-muted
* @type {boolean}
*/
this.selfMute = data.self_mute;
/**
* Whether this member is self-deafened
* @type {boolean}
*/
this.selfDeaf = data.self_deaf;
/**
* The voice session ID of this member, if any
* @type {?string}
*/
this.voiceSessionID = data.session_id;
/**
* The voice channel ID of this member, if any
* @type {?string}
*/
this.voiceChannelID = data.channel_id;
/**
* Whether this member is speaking
* @type {boolean}
*/
this.speaking = false;
/**
* The nickname of this guild member, if they have one
* @type {?string}
*/
this.nickname = data.nick || null;
/**
* The timestamp the member joined the guild at
* @type {number}
*/
this.joinedTimestamp = new Date(data.joined_at).getTime();
this.user = data.user;
this._roles = data.roles;
}
/**
* The time the member joined the guild
* @type {Date}
* @readonly
*/
get joinedAt() {
return new Date(this.joinedTimestamp);
}
/**
* The presence of this guild member
* @type {Presence}
* @readonly
*/
get presence() {
return this.frozenPresence || this.guild.presences.get(this.id) || new Presence();
}
/**
* A list of roles that are applied to this GuildMember, mapped by the role ID.
* @type {Collection<string, Role>}
* @readonly
*/
get roles() {
const list = new Collection();
const everyoneRole = this.guild.roles.get(this.guild.id);
if (everyoneRole) list.set(everyoneRole.id, everyoneRole);
for (const roleID of this._roles) {
const role = this.guild.roles.get(roleID);
if (role) list.set(role.id, role);
}
return list;
}
/**
* The role of the member with the highest position.
* @type {Role}
* @readonly
*/
get highestRole() {
return this.roles.reduce((prev, role) => !prev || role.comparePositionTo(prev) > 0 ? role : prev);
}
/**
* Whether this member is muted in any way
* @type {boolean}
* @readonly
*/
get mute() {
return this.selfMute || this.serverMute;
}
/**
* Whether this member is deafened in any way
* @type {boolean}
* @readonly
*/
get deaf() {
return this.selfDeaf || this.serverDeaf;
}
/**
* The voice channel this member is in, if any
* @type {?VoiceChannel}
* @readonly
*/
get voiceChannel() {
return this.guild.channels.get(this.voiceChannelID);
}
/**
* The ID of this user
* @type {string}
* @readonly
*/
get id() {
return this.user.id;
}
/**
* The nickname of the member, or their username if they don't have one
* @type {string}
* @readonly
*/
get displayName() {
return this.nickname || this.user.username;
}
/**
* The overall set of permissions for the guild member, taking only roles into account
* @type {EvaluatedPermissions}
* @readonly
*/
get permissions() {
if (this.user.id === this.guild.ownerID) return new EvaluatedPermissions(this, Constants.ALL_PERMISSIONS);
let permissions = 0;
const roles = this.roles;
for (const role of roles.values()) permissions |= role.permissions;
const admin = Boolean(permissions & Constants.PermissionFlags.ADMINISTRATOR);
if (admin) permissions = Constants.ALL_PERMISSIONS;
return new EvaluatedPermissions(this, permissions);
}
/**
* Whether the member is kickable by the client user.
* @type {boolean}
* @readonly
*/
get kickable() {
if (this.user.id === this.guild.ownerID) return false;
if (this.user.id === this.client.user.id) return false;
const clientMember = this.guild.member(this.client.user);
if (!clientMember.hasPermission(Constants.PermissionFlags.KICK_MEMBERS)) return false;
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
}
/**
* Whether the member is bannable by the client user.
* @type {boolean}
* @readonly
*/
get bannable() {
if (this.user.id === this.guild.ownerID) return false;
if (this.user.id === this.client.user.id) return false;
const clientMember = this.guild.member(this.client.user);
if (!clientMember.hasPermission(Constants.PermissionFlags.BAN_MEMBERS)) return false;
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
}
/**
* Returns `channel.permissionsFor(guildMember)`. Returns evaluated permissions for a member in a guild channel.
* @param {ChannelResolvable} channel Guild channel to use as context
* @returns {?EvaluatedPermissions}
*/
permissionsIn(channel) {
channel = this.client.resolver.resolveChannel(channel);
if (!channel || !channel.guild) throw new Error('Could not resolve channel to a guild channel.');
return channel.permissionsFor(this);
}
/**
* Checks if any of the member's roles have a permission.
* @param {PermissionResolvable} permission The permission to check for
* @param {boolean} [explicit=false] Whether to require the roles to explicitly have the exact permission
* @returns {boolean}
*/
hasPermission(permission, explicit = false) {
if (!explicit && this.user.id === this.guild.ownerID) return true;
return this.roles.some(r => r.hasPermission(permission, explicit));
}
/**
* Checks whether the roles of the member allows them to perform specific actions.
* @param {PermissionResolvable[]} permissions The permissions to check for
* @param {boolean} [explicit=false] Whether to require the member to explicitly have the exact permissions
* @returns {boolean}
*/
hasPermissions(permissions, explicit = false) {
if (!explicit && this.user.id === this.guild.ownerID) return true;
return permissions.every(p => this.hasPermission(p, explicit));
}
/**
* Checks whether the roles of the member allows them to perform specific actions, and lists any missing permissions.
* @param {PermissionResolvable[]} permissions The permissions to check for
* @param {boolean} [explicit=false] Whether to require the member to explicitly have the exact permissions
* @returns {PermissionResolvable[]}
*/
missingPermissions(permissions, explicit = false) {
return permissions.filter(p => !this.hasPermission(p, explicit));
}
/**
* Edit a guild member
* @param {GuildmemberEditData} data The data to edit the member with
* @returns {Promise<GuildMember>}
*/
edit(data) {
return this.client.rest.methods.updateGuildMember(this, data);
}
/**
* Mute/unmute a user
* @param {boolean} mute Whether or not the member should be muted
* @returns {Promise<GuildMember>}
*/
setMute(mute) {
return this.edit({ mute });
}
/**
* Deafen/undeafen a user
* @param {boolean} deaf Whether or not the member should be deafened
* @returns {Promise<GuildMember>}
*/
setDeaf(deaf) {
return this.edit({ deaf });
}
/**
* Moves the guild member to the given channel.
* @param {ChannelResolvable} channel The channel to move the member to
* @returns {Promise<GuildMember>}
*/
setVoiceChannel(channel) {
return this.edit({ channel });
}
/**
* Sets the roles applied to the member.
* @param {Collection<string, Role>|Role[]|string[]} roles The roles or role IDs to apply
* @returns {Promise<GuildMember>}
*/
setRoles(roles) {
return this.edit({ roles });
}
/**
* Adds a single role to the member.
* @param {Role|string} role The role or ID of the role to add
* @returns {Promise<GuildMember>}
*/
addRole(role) {
if (!(role instanceof Role)) role = this.guild.roles.get(role);
return this.client.rest.methods.addMemberRole(this, role);
}
/**
* Adds multiple roles to the member.
* @param {Collection<string, Role>|Role[]|string[]} roles The roles or role IDs to add
* @returns {Promise<GuildMember>}
*/
addRoles(roles) {
let allRoles;
if (roles instanceof Collection) {
allRoles = this._roles.slice();
for (const role of roles.values()) allRoles.push(role.id);
} else {
allRoles = this._roles.concat(roles);
}
return this.edit({ roles: allRoles });
}
/**
* Removes a single role from the member.
* @param {Role|string} role The role or ID of the role to remove
* @returns {Promise<GuildMember>}
*/
removeRole(role) {
if (!(role instanceof Role)) role = this.guild.roles.get(role);
return this.client.rest.methods.removeMemberRole(this, role);
}
/**
* Removes multiple roles from the member.
* @param {Collection<string, Role>|Role[]|string[]} roles The roles or role IDs to remove
* @returns {Promise<GuildMember>}
*/
removeRoles(roles) {
const allRoles = this._roles.slice();
if (roles instanceof Collection) {
for (const role of roles.values()) {
const index = allRoles.indexOf(role.id);
if (index >= 0) allRoles.splice(index, 1);
}
} else {
for (const role of roles) {
const index = allRoles.indexOf(role instanceof Role ? role.id : role);
if (index >= 0) allRoles.splice(index, 1);
}
}
return this.edit({ roles: allRoles });
}
/**
* Set the nickname for the guild member
* @param {string} nick The nickname for the guild member
* @returns {Promise<GuildMember>}
*/
setNickname(nick) {
return this.edit({ nick });
}
/**
* Deletes any DMs with this guild member
* @returns {Promise<DMChannel>}
*/
deleteDM() {
return this.client.rest.methods.deleteChannel(this);
}
/**
* Kick this member from the guild
* @returns {Promise<GuildMember>}
*/
kick() {
return this.client.rest.methods.kickGuildMember(this.guild, this);
}
/**
* Ban this guild member
* @param {number} [deleteDays=0] The amount of days worth of messages from this member that should
* also be deleted. Between `0` and `7`.
* @returns {Promise<GuildMember>}
* @example
* // ban a guild member
* guildMember.ban(7);
*/
ban(deleteDays = 0) {
return this.client.rest.methods.banGuildMember(this.guild, this, deleteDays);
}
/**
* When concatenated with a string, this automatically concatenates the user's mention instead of the Member object.
* @returns {string}
* @example
* // logs: Hello from <@123456789>!
* console.log(`Hello from ${member}!`);
*/
toString() {
return `<@${this.nickname ? '!' : ''}${this.user.id}>`;
}
// These are here only for documentation purposes - they are implemented by TextBasedChannel
send() { return; }
sendMessage() { return; }
sendEmbed() { return; }
sendFile() { return; }
sendCode() { return; }
}
TextBasedChannel.applyToClass(GuildMember);
module.exports = GuildMember;
|