Skip to content

Commit 108a31a

Browse files
committed
ugrade discord.js use ban pagination for id
1 parent 9dcac27 commit 108a31a

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
"@google-cloud/logging": "^9.1.1",
2626
"@googleapis/youtube": "^2.0.0",
2727
"diff": "^4.0.2",
28-
"discord.js": "^13.5.0",
28+
"discord.js": "^13.7.0",
2929
"fuse.js": "^6.4.6",
3030
"got": "^11.8.2",
3131
"mysql": "^2.18.1",
3232
"string-similarity": "^4.0.4",
3333
"turndown": "^7.1.1"
3434
},
3535
"devDependencies": {
36+
"@types/node": "^17.0.34",
3637
"eslint": "^8.3.0",
3738
"eslint-plugin-import": "^2.25.3",
3839
"eslint-plugin-jest": "^25.3.0",

src/commands/utility/IDCommand.js

+54-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Discord bans fetched per page
3+
* @type {number}
4+
*/
5+
const BAN_PAGE_SIZE = 1000;
6+
17
const Command = require('../Command');
28
const {
39
User,
@@ -30,28 +36,19 @@ class IDCommand extends Command {
3036

3137
const [,user, discrim] = query.match(/([^#]*)#?(\d{4})?$/);
3238

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});
3740

3841
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()))
4043
.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())));
5552
}
5653
}
5754

@@ -82,6 +79,44 @@ class IDCommand extends Command {
8279
.setColor(util.color.green);
8380
}
8481

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+
85120
/**
86121
*
87122
* @param {User} user

0 commit comments

Comments
 (0)