forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GuildMemberManager.js
394 lines (362 loc) · 14.9 KB
/
GuildMemberManager.js
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
'use strict';
const BaseManager = require('./BaseManager');
const { Error, TypeError, RangeError } = require('../errors');
const GuildMember = require('../structures/GuildMember');
const Role = require('../structures/Role');
const Collection = require('../util/Collection');
const { Events, OPCodes } = require('../util/Constants');
const SnowflakeUtil = require('../util/SnowflakeUtil');
/**
* Manages API methods for GuildMembers and stores their cache.
* @extends {BaseManager}
*/
class GuildMemberManager extends BaseManager {
constructor(guild, iterable) {
super(guild.client, iterable, GuildMember);
/**
* The guild this manager belongs to
* @type {Guild}
*/
this.guild = guild;
}
/**
* The cache of this Manager
* @type {Collection<Snowflake, GuildMember>}
* @name GuildMemberManager#cache
*/
add(data, cache = true) {
return super.add(data, cache, { id: data.user.id, extras: [this.guild] });
}
/**
* Data that resolves to give a GuildMember object. This can be:
* * A GuildMember object
* * A User resolvable
* @typedef {GuildMember|UserResolvable} GuildMemberResolvable
*/
/**
* Resolves a GuildMemberResolvable to a GuildMember object.
* @param {GuildMemberResolvable} member The user that is part of the guild
* @returns {?GuildMember}
*/
resolve(member) {
const memberResolvable = super.resolve(member);
if (memberResolvable) return memberResolvable;
const userResolvable = this.client.users.resolveID(member);
if (userResolvable) return super.resolve(userResolvable);
return null;
}
/**
* Resolves a GuildMemberResolvable to a member ID string.
* @param {GuildMemberResolvable} member The user that is part of the guild
* @returns {?Snowflake}
*/
resolveID(member) {
const memberResolvable = super.resolveID(member);
if (memberResolvable) return memberResolvable;
const userResolvable = this.client.users.resolveID(member);
return this.cache.has(userResolvable) ? userResolvable : null;
}
/**
* Options used to fetch a single member from a guild.
* @typedef {Object} FetchMemberOptions
* @property {UserResolvable} user The user to fetch
* @property {boolean} [cache=true] Whether or not to cache the fetched member
* @property {boolean} [force=false] Whether to skip the cache check and request the API
*/
/**
* Options used to fetch multiple members from a guild.
* @typedef {Object} FetchMembersOptions
* @property {UserResolvable|UserResolvable[]} user The user(s) to fetch
* @property {?string} query Limit fetch to members with similar usernames
* @property {number} [limit=0] Maximum number of members to request
* @property {boolean} [withPresences=false] Whether or not to include the presences
* @property {number} [time=120e3] Timeout for receipt of members
* @property {?string} nonce Nonce for this request (32 characters max - default to base 16 now timestamp)
* @property {boolean} [force=false] Whether to skip the cache check and request the API
*/
/**
* Fetches member(s) from Discord, even if they're offline.
* @param {UserResolvable|FetchMemberOptions|FetchMembersOptions} [options] If a UserResolvable, the user to fetch.
* If undefined, fetches all members.
* If a query, it limits the results to users with similar usernames.
* @returns {Promise<GuildMember>|Promise<Collection<Snowflake, GuildMember>>}
* @example
* // Fetch all members from a guild
* guild.members.fetch()
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch a single member
* guild.members.fetch('66564597481480192')
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch a single member without checking cache
* guild.members.fetch({ user, force: true })
* .then(console.log)
* .catch(console.error)
* @example
* // Fetch a single member without caching
* guild.members.fetch({ user, cache: false })
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch by an array of users including their presences
* guild.members.fetch({ user: ['66564597481480192', '191615925336670208'], withPresences: true })
* .then(console.log)
* .catch(console.error);
* @example
* // Fetch by query
* guild.members.fetch({ query: 'hydra', limit: 1 })
* .then(console.log)
* .catch(console.error);
*/
fetch(options) {
if (!options) return this._fetchMany();
const user = this.client.users.resolveID(options);
if (user) return this._fetchSingle({ user, cache: true });
if (options.user) {
if (Array.isArray(options.user)) {
options.user = options.user.map(u => this.client.users.resolveID(u));
return this._fetchMany(options);
} else {
options.user = this.client.users.resolveID(options.user);
}
if (!options.limit && !options.withPresences) return this._fetchSingle(options);
}
return this._fetchMany(options);
}
/**
* Search for members in the guild based on a query.
* @param {Object} options Search options
* @property {string} options.query Filter members whose username or nickname start with this query
* @property {number} [options.limit=1] Maximum number of members to search
* @property {boolean} [options.cache=true] Whether or not to cache the fetched member(s)
* @returns {Promise<Collection<Snowflake, GuildMember>>}
*/
async search({ query, limit = 1, cache = true } = {}) {
const data = await this.client.api.guilds(this.guild.id).members.search.get({ query: { query, limit } });
return data.reduce((col, member) => col.set(member.user.id, this.add(member, cache)), new Collection());
}
/**
* Edits a member of the guild.
* <info>The user must be a member of the guild</info>
* @param {UserResolvable} member The member to edit
* @param {GuildMemberEditData} data The data to edit the member with
* @param {string} [reason] Reason for editing this user
* @returns {Promise<GuildMember>}
*/
async edit(member, data, reason) {
const id = this.client.users.resolveID(member);
if (!id) throw new TypeError('INVALID_TYPE', 'user', 'UserResolvable');
// Clone the data object for immutability
const _data = { ...data };
if (_data.channel) {
_data.channel = this.guild.channels.resolve(_data.channel);
if (!_data.channel || _data.channel.type !== 'voice') {
throw new Error('GUILD_VOICE_CHANNEL_RESOLVE');
}
_data.channel_id = _data.channel.id;
_data.channel = undefined;
} else if (_data.channel === null) {
_data.channel_id = null;
_data.channel = undefined;
}
if (_data.roles) _data.roles = _data.roles.map(role => (role instanceof Role ? role.id : role));
let endpoint = this.client.api.guilds(this.guild.id);
if (id === this.client.user.id) {
const keys = Object.keys(_data);
if (keys.length === 1 && keys[0] === 'nick') endpoint = endpoint.members('@me').nick;
else endpoint = endpoint.members(id);
} else {
endpoint = endpoint.members(id);
}
const d = await endpoint.patch({ data: _data, reason });
const clone = this.cache.get(id)?._clone();
clone?.patch(d);
return clone ?? this.add(d, false);
}
/**
* Prunes members from the guild based on how long they have been inactive.
* <info>It's recommended to set options.count to `false` for large guilds.</info>
* @param {Object} [options] Prune options
* @param {number} [options.days=7] Number of days of inactivity required to kick
* @param {boolean} [options.dry=false] Get number of users that will be kicked, without actually kicking them
* @param {boolean} [options.count=true] Whether or not to return the number of users that have been kicked.
* @param {RoleResolvable[]} [options.roles=[]] Array of roles to bypass the "...and no roles" constraint when pruning
* @param {string} [options.reason] Reason for this prune
* @returns {Promise<number|null>} The number of members that were/will be kicked
* @example
* // See how many members will be pruned
* guild.members.prune({ dry: true })
* .then(pruned => console.log(`This will prune ${pruned} people!`))
* .catch(console.error);
* @example
* // Actually prune the members
* guild.members.prune({ days: 1, reason: 'too many people!' })
* .then(pruned => console.log(`I just pruned ${pruned} people!`))
* .catch(console.error);
* @example
* // Include members with a specified role
* guild.members.prune({ days: 7, roles: ['657259391652855808'] })
* .then(pruned => console.log(`I just pruned ${pruned} people!`))
* .catch(console.error);
*/
prune({ days = 7, dry = false, count: compute_prune_count = true, roles = [], reason } = {}) {
if (typeof days !== 'number') throw new TypeError('PRUNE_DAYS_TYPE');
const query = { days };
const resolvedRoles = [];
for (const role of roles) {
const resolvedRole = this.guild.roles.resolveID(role);
if (!resolvedRole) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles', 'Array of Roles or Snowflakes', true));
}
resolvedRoles.push(resolvedRole);
}
if (resolvedRoles.length) {
query.include_roles = dry ? resolvedRoles.join(',') : resolvedRoles;
}
const endpoint = this.client.api.guilds(this.guild.id).prune;
if (dry) {
return endpoint.get({ query, reason }).then(data => data.pruned);
}
return endpoint
.post({
data: { ...query, compute_prune_count },
reason,
})
.then(data => data.pruned);
}
/**
* Kicks a user from the guild.
* <info>The user must be a member of the guild</info>
* @param {UserResolvable} user The member to kick
* @param {string} [reason] Reason for kicking
* @returns {Promise<GuildMember|User|Snowflake>} Result object will be resolved as specifically as possible.
* If the GuildMember cannot be resolved, the User will instead be attempted to be resolved. If that also cannot
* be resolved, the user ID will be the result.
* @example
* // Kick a user by ID (or with a user/guild member object)
* guild.members.kick('84484653687267328')
* .then(user => console.log(`Kicked ${user.username || user.id || user} from ${guild.name}`))
* .catch(console.error);
*/
async kick(user, reason) {
const id = this.client.users.resolveID(user);
if (!id) return Promise.reject(new TypeError('INVALID_TYPE', 'user', 'UserResolvable'));
await this.client.api.guilds(this.guild.id).members(id).delete({ reason });
return this.resolve(user) ?? this.client.users.resolve(user) ?? id;
}
/**
* Bans a user from the guild.
* @param {UserResolvable} user The user to ban
* @param {Object} [options] Options for the ban
* @param {number} [options.days=0] Number of days of messages to delete, must be between 0 and 7, inclusive
* @param {string} [options.reason] Reason for banning
* @returns {Promise<GuildMember|User|Snowflake>} Result object will be resolved as specifically as possible.
* If the GuildMember cannot be resolved, the User will instead be attempted to be resolved. If that also cannot
* be resolved, the user ID will be the result.
* @example
* // Ban a user by ID (or with a user/guild member object)
* guild.members.ban('84484653687267328')
* .then(user => console.log(`Banned ${user.username || user.id || user} from ${guild.name}`))
* .catch(console.error);
*/
async ban(user, options = { days: 0 }) {
if (typeof options !== 'object') return Promise.reject(new TypeError('INVALID_TYPE', 'options', 'object', true));
if (options.days) options.delete_message_days = options.days;
const id = this.client.users.resolveID(user);
if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID', true));
await this.client.api.guilds(this.guild.id).bans[id].put({ data: options });
return this.resolve(user) ?? this.client.users.resolve(user) ?? id;
}
/**
* Unbans a user from the guild.
* @param {UserResolvable} user The user to unban
* @param {string} [reason] Reason for unbanning user
* @returns {Promise<User>}
* @example
* // Unban a user by ID (or with a user/guild member object)
* guild.members.unban('84484653687267328')
* .then(user => console.log(`Unbanned ${user.username} from ${guild.name}`))
* .catch(console.error);
*/
async unban(user, reason) {
const id = this.client.users.resolveID(user);
if (!id) return Promise.reject(new Error('BAN_RESOLVE_ID'));
await this.client.api.guilds(this.guild.id).bans[id].delete({ reason });
return this.client.users.resolve(user);
}
_fetchSingle({ user, cache, force = false }) {
if (!force) {
const existing = this.cache.get(user);
if (existing && !existing.partial) return Promise.resolve(existing);
}
return this.client.api
.guilds(this.guild.id)
.members(user)
.get()
.then(data => this.add(data, cache));
}
_fetchMany({
limit = 0,
withPresences: presences = false,
user: user_ids,
query,
time = 120e3,
nonce = SnowflakeUtil.generate(),
force = false,
} = {}) {
return new Promise((resolve, reject) => {
if (this.guild.memberCount === this.cache.size && !query && !limit && !presences && !user_ids && !force) {
resolve(this.cache);
return;
}
if (!query && !user_ids) query = '';
if (nonce.length > 32) throw new RangeError('MEMBER_FETCH_NONCE_LENGTH');
this.guild.shard.send({
op: OPCodes.REQUEST_GUILD_MEMBERS,
d: {
guild_id: this.guild.id,
presences,
user_ids,
query,
nonce,
limit,
},
});
const fetchedMembers = new Collection();
const option = query || limit || presences || user_ids;
let i = 0;
const handler = (members, _, chunk) => {
timeout.refresh();
if (chunk.nonce !== nonce) return;
i++;
for (const member of members.values()) {
if (option) fetchedMembers.set(member.id, member);
}
if (
this.guild.memberCount <= this.cache.size ||
(option && members.size < 1000) ||
(limit && fetchedMembers.size >= limit) ||
i === chunk.count
) {
this.client.clearTimeout(timeout);
this.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler);
this.client.decrementMaxListeners();
let fetched = option ? fetchedMembers : this.cache;
if (user_ids && !Array.isArray(user_ids) && fetched.size) fetched = fetched.first();
resolve(fetched);
}
};
const timeout = this.client.setTimeout(() => {
this.client.removeListener(Events.GUILD_MEMBERS_CHUNK, handler);
this.client.decrementMaxListeners();
reject(new Error('GUILD_MEMBERS_TIMEOUT'));
}, time);
this.client.incrementMaxListeners();
this.client.on(Events.GUILD_MEMBERS_CHUNK, handler);
});
}
}
module.exports = GuildMemberManager;