|
| 1 | +/** |
| 2 | + * Discord bans fetched per page |
| 3 | + * @type {number} |
| 4 | + */ |
| 5 | +const BAN_PAGE_SIZE = 1000; |
| 6 | + |
1 | 7 | const Command = require('../Command');
|
2 | 8 | const {
|
3 | 9 | User,
|
@@ -30,28 +36,19 @@ class IDCommand extends Command {
|
30 | 36 |
|
31 | 37 | const [,user, discrim] = query.match(/([^#]*)#?(\d{4})?$/);
|
32 | 38 |
|
33 |
| - /** |
34 |
| - * @type {Collection<Snowflake, GuildMember|GuildBan>} |
35 |
| - */ |
36 |
| - let users = await this.source.getGuild().members.fetch({query}); |
| 39 | + const users = /** @type {Collection<Snowflake, GuildMember>} */ await this.source.getGuild().members.fetch({query}); |
37 | 40 |
|
38 | 41 | if (users.size !== 0) {
|
39 |
| - await this.reply(this._generateResultEmbed(query, Array.from(users.values())) |
| 42 | + await this.editReply(this._generateResultEmbed(query, Array.from(users.values())) |
40 | 43 | .setFooter({text: 'I\'m still searching the ban, on big guilds this can take a while...'}));
|
41 |
| - |
42 |
| - let bans = await this.source.getGuild().bans.fetch(); |
43 |
| - bans = bans.filter(banInfo => this._matches(banInfo.user, user, discrim)); |
44 |
| - users = users.concat(bans); |
45 |
| - await this.editReply(this._generateResultEmbed(query, Array.from(users.values()))); |
46 |
| - } else { |
47 |
| - let bans = await this.source.getGuild().bans.fetch(); |
48 |
| - bans = bans.filter(banInfo => this._matches(banInfo.user, user, discrim)); |
49 |
| - if (bans.size === 0) { |
50 |
| - return this.sendError('No users found'); |
51 |
| - } |
52 |
| - else { |
53 |
| - await this.editReply(this._generateResultEmbed(query, Array.from(bans.values()))); |
54 |
| - } |
| 44 | + } |
| 45 | + const bans = await this._fetchAndFilterBans(user, discrim); |
| 46 | + if (bans.size === 0) { |
| 47 | + return this.sendError('No users found'); |
| 48 | + } |
| 49 | + else { |
| 50 | + // noinspection JSCheckFunctionSignatures |
| 51 | + await this.editReply(this._generateResultEmbed(query, Array.from(users.concat(bans).values()))); |
55 | 52 | }
|
56 | 53 | }
|
57 | 54 |
|
@@ -82,6 +79,44 @@ class IDCommand extends Command {
|
82 | 79 | .setColor(util.color.green);
|
83 | 80 | }
|
84 | 81 |
|
| 82 | + /** |
| 83 | + * |
| 84 | + * @param {string} user |
| 85 | + * @param {string} discrim |
| 86 | + * @return {Promise<Collection<Snowflake, GuildBan>>} |
| 87 | + * @private |
| 88 | + */ |
| 89 | + async _fetchAndFilterBans(user, discrim) { |
| 90 | + return (await this._fetchAllBans()).filter(banInfo => this._matches(banInfo.user, user, discrim)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * fetch all bans |
| 95 | + * @return {Promise<Collection<Snowflake, GuildBan>>} |
| 96 | + * @private |
| 97 | + */ |
| 98 | + async _fetchAllBans() { |
| 99 | + let bans = new Collection(); |
| 100 | + let done = false, previous = null; |
| 101 | + while (!done) { |
| 102 | + const options = { |
| 103 | + limit: BAN_PAGE_SIZE |
| 104 | + }; |
| 105 | + if (previous !== null) { |
| 106 | + options.after = previous; |
| 107 | + } |
| 108 | + |
| 109 | + const newBans = /** @type {Collection<Snowflake, GuildBan>}*/ await this.source.getGuild().bans.fetch(options); |
| 110 | + bans = bans.concat(newBans); |
| 111 | + previous = newBans.lastKey(); |
| 112 | + |
| 113 | + if (newBans.size < BAN_PAGE_SIZE) { |
| 114 | + done = true; |
| 115 | + } |
| 116 | + } |
| 117 | + return bans; |
| 118 | + } |
| 119 | + |
85 | 120 | /**
|
86 | 121 | *
|
87 | 122 | * @param {User} user
|
|
0 commit comments