Skip to content

Commit

Permalink
Merge pull request #490 from aternosorg/id
Browse files Browse the repository at this point in the history
ugrade discord.js use ban pagination for id
  • Loading branch information
JulianVennen authored May 18, 2022
2 parents 962ae9d + 108a31a commit 25e95a4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
"@google-cloud/logging": "^9.1.1",
"@googleapis/youtube": "^2.0.0",
"diff": "^4.0.2",
"discord.js": "^13.5.0",
"discord.js": "^13.7.0",
"fuse.js": "^6.4.6",
"got": "^11.8.2",
"mysql2": "^2.3.3",
"string-similarity": "^4.0.4",
"turndown": "^7.1.1"
},
"devDependencies": {
"@types/node": "^17.0.34",
"eslint": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jest": "^25.3.0",
Expand Down
73 changes: 54 additions & 19 deletions src/commands/utility/IDCommand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Discord bans fetched per page
* @type {number}
*/
const BAN_PAGE_SIZE = 1000;

const Command = require('../Command');
const {
User,
Expand Down Expand Up @@ -30,28 +36,19 @@ class IDCommand extends Command {

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

/**
* @type {Collection<Snowflake, GuildMember|GuildBan>}
*/
let users = await this.source.getGuild().members.fetch({query});
const users = /** @type {Collection<Snowflake, GuildMember>} */ await this.source.getGuild().members.fetch({query});

if (users.size !== 0) {
await this.reply(this._generateResultEmbed(query, Array.from(users.values()))
await this.editReply(this._generateResultEmbed(query, Array.from(users.values()))
.setFooter({text: 'I\'m still searching the ban, on big guilds this can take a while...'}));

let bans = await this.source.getGuild().bans.fetch();
bans = bans.filter(banInfo => this._matches(banInfo.user, user, discrim));
users = users.concat(bans);
await this.editReply(this._generateResultEmbed(query, Array.from(users.values())));
} else {
let bans = await this.source.getGuild().bans.fetch();
bans = bans.filter(banInfo => this._matches(banInfo.user, user, discrim));
if (bans.size === 0) {
return this.sendError('No users found');
}
else {
await this.editReply(this._generateResultEmbed(query, Array.from(bans.values())));
}
}
const bans = await this._fetchAndFilterBans(user, discrim);
if (bans.size === 0) {
return this.sendError('No users found');
}
else {
// noinspection JSCheckFunctionSignatures
await this.editReply(this._generateResultEmbed(query, Array.from(users.concat(bans).values())));
}
}

Expand Down Expand Up @@ -82,6 +79,44 @@ class IDCommand extends Command {
.setColor(util.color.green);
}

/**
*
* @param {string} user
* @param {string} discrim
* @return {Promise<Collection<Snowflake, GuildBan>>}
* @private
*/
async _fetchAndFilterBans(user, discrim) {
return (await this._fetchAllBans()).filter(banInfo => this._matches(banInfo.user, user, discrim));
}

/**
* fetch all bans
* @return {Promise<Collection<Snowflake, GuildBan>>}
* @private
*/
async _fetchAllBans() {
let bans = new Collection();
let done = false, previous = null;
while (!done) {
const options = {
limit: BAN_PAGE_SIZE
};
if (previous !== null) {
options.after = previous;
}

const newBans = /** @type {Collection<Snowflake, GuildBan>}*/ await this.source.getGuild().bans.fetch(options);
bans = bans.concat(newBans);
previous = newBans.lastKey();

if (newBans.size < BAN_PAGE_SIZE) {
done = true;
}
}
return bans;
}

/**
*
* @param {User} user
Expand Down

0 comments on commit 25e95a4

Please sign in to comment.