diff --git a/src/plugins/Utility.ts b/src/plugins/Utility.ts index 6725d482..740d8f3c 100644 --- a/src/plugins/Utility.ts +++ b/src/plugins/Utility.ts @@ -49,6 +49,7 @@ const { performance } = require("perf_hooks"); const MAX_SEARCH_RESULTS = 15; const MAX_CLEAN_COUNT = 50; const CLEAN_COMMAND_DELETE_DELAY = 5000; +const MEMBER_REFRESH_FREQUENCY = 10 * 60 * 1000; // How often to do a full member refresh when using !search or !roles --counts const activeReloads: Map = new Map(); @@ -76,6 +77,8 @@ export class UtilityPlugin extends ZeppelinPlugin { protected savedMessages: GuildSavedMessages; protected archives: GuildArchives; + protected lastFullMemberRefresh = 0; + getDefaultOptions(): IPluginOptions { return { config: { @@ -133,6 +136,12 @@ export class UtilityPlugin extends ZeppelinPlugin { } } + protected async refreshMembersIfNeeded() { + if (Date.now() < this.lastFullMemberRefresh + MEMBER_REFRESH_FREQUENCY) return; + await this.guild.fetchAllMembers(); + this.lastFullMemberRefresh = Date.now(); + } + @d.command("roles", "[search:string$]", { options: [ { @@ -156,6 +165,8 @@ export class UtilityPlugin extends ZeppelinPlugin { } if (args.counts) { + this.refreshMembersIfNeeded(); + // If the user requested role member counts as well, calculate them and sort the roles by their member count const roleCounts: Map = Array.from(this.guild.members.values()).reduce((map, member) => { for (const roleId of member.roles) { @@ -272,6 +283,8 @@ export class UtilityPlugin extends ZeppelinPlugin { msg: Message, args: { query?: string; role?: string; page?: number; voice?: boolean; sort?: string; "case-sensitive"?: boolean }, ) { + this.refreshMembersIfNeeded(); + let matchingMembers = Array.from(this.guild.members.values()); if (args.role) {