reduce the amount of cache lookups

This commit is contained in:
Dmytro Meleshko 2021-01-29 21:12:53 +02:00
parent 0cba164f3d
commit 303e81cc37
3 changed files with 14 additions and 10 deletions

View File

@ -158,8 +158,7 @@ export default new Command({
permission: Command.PERMISSIONS.BOT_SUPPORT, permission: Command.PERMISSIONS.BOT_SUPPORT,
async run($: CommonLibrary): Promise<any> { async run($: CommonLibrary): Promise<any> {
const nickName = $.args.join(" "); const nickName = $.args.join(" ");
const trav = $.guild?.members.cache.find((member) => member.id === $.client.user?.id); await $.guild?.me?.setNickname(nickName);
await trav?.setNickname(nickName);
if (botHasPermission($.guild, Permissions.FLAGS.MANAGE_MESSAGES)) if (botHasPermission($.guild, Permissions.FLAGS.MANAGE_MESSAGES))
$.message.delete({timeout: 5000}).catch($.handler.bind($)); $.message.delete({timeout: 5000}).catch($.handler.bind($));
$.channel.send(`Nickname set to \`${nickName}\``).then((m) => m.delete({timeout: 5000})); $.channel.send(`Nickname set to \`${nickName}\``).then((m) => m.delete({timeout: 5000}));

View File

@ -76,10 +76,13 @@ export default new Command({
description: "Displays info about the current guild.", description: "Displays info about the current guild.",
async run($: CommonLibrary): Promise<any> { async run($: CommonLibrary): Promise<any> {
if ($.guild) { if ($.guild) {
const members = await $.guild.members.fetch({
withPresences: true,
force: true
});
const roles = $.guild.roles.cache const roles = $.guild.roles.cache
.sort((a, b) => b.position - a.position) .sort((a, b) => b.position - a.position)
.map((role) => role.toString()); .map((role) => role.toString());
const members = $.guild.members.cache;
const channels = $.guild.channels.cache; const channels = $.guild.channels.cache;
const emojis = $.guild.emojis.cache; const emojis = $.guild.emojis.cache;
const iconURL = $.guild.iconURL({dynamic: true}); const iconURL = $.guild.iconURL({dynamic: true});

View File

@ -172,7 +172,7 @@ export function formatUTCTimestamp(now = new Date()) {
} }
export function botHasPermission(guild: Guild | null, permission: number): boolean { export function botHasPermission(guild: Guild | null, permission: number): boolean {
return !!(guild?.me?.hasPermission(permission)); return !!guild?.me?.hasPermission(permission);
} }
export function updateGlobalEmoteRegistry(): void { export function updateGlobalEmoteRegistry(): void {
@ -216,20 +216,22 @@ $.paginate = async (
callback(page); callback(page);
}; };
const BACKWARDS_EMOJI = "⬅️";
const FORWARDS_EMOJI = "➡️";
const handle = (emote: string, reacterID: string) => { const handle = (emote: string, reacterID: string) => {
switch (emote) { switch (emote) {
case "⬅️": case BACKWARDS_EMOJI:
turn(-1); turn(-1);
break; break;
case "➡️": case FORWARDS_EMOJI:
turn(1); turn(1);
break; break;
} }
}; };
// Listen for reactions and call the handler. // Listen for reactions and call the handler.
await message.react("⬅️"); let backwardsReaction = await message.react(BACKWARDS_EMOJI);
await message.react("➡️"); let forwardsReaction = await message.react(FORWARDS_EMOJI);
eventListeners.set(message.id, handle); eventListeners.set(message.id, handle);
await message.awaitReactions( await message.awaitReactions(
(reaction, user) => { (reaction, user) => {
@ -248,8 +250,8 @@ $.paginate = async (
); );
// When time's up, remove the bot's own reactions. // When time's up, remove the bot's own reactions.
eventListeners.delete(message.id); eventListeners.delete(message.id);
message.reactions.cache.get("⬅️")?.users.remove(message.author); backwardsReaction.users.remove(message.author);
message.reactions.cache.get("➡️")?.users.remove(message.author); forwardsReaction.users.remove(message.author);
}; };
// Waits for the sender to either confirm an action or let it pass (and delete the message). // Waits for the sender to either confirm an action or let it pass (and delete the message).