From 524d02e62c4bd506205ee0cd4ff9bcb8c6fd543d Mon Sep 17 00:00:00 2001 From: Larsundso Date: Tue, 26 Dec 2023 02:30:04 +0100 Subject: [PATCH] [PATCH] Updating cmd and cmd perm cache --- src/BaseClient/ClientHelperModules/cache.ts | 5 +- .../cache/discord/commands.ts | 59 +++++++++++++++++++ .../ClientHelperModules/getCustomCommand.ts | 3 +- .../helpHelpers/getEmbeds.ts | 5 +- .../commands/bulkOverwriteGlobalCommands.ts | 11 ++-- .../commands/bulkOverwriteGuildCommands.ts | 12 ++-- .../commands/createGlobalCommand.ts | 11 +--- .../commands/createGuildCommand.ts | 9 +-- .../commands/deleteGlobalCommand.ts | 12 +--- .../commands/deleteGuildCommand.ts | 12 +--- .../commands/editGlobalCommand.ts | 10 +--- .../commands/editGuildCommand.ts | 20 ++----- .../commands/editGuildCommandPermissions.ts | 4 ++ .../commands/getGlobalCommand.ts | 10 ++-- .../commands/getGlobalCommands.ts | 10 ++-- .../commands/getGuildCommand.ts | 9 +-- .../commands/getGuildCommandPermissions.ts | 6 +- .../commands/getGuildCommands.ts | 10 ++-- .../commands/getGuildCommandsPermissions.ts | 8 +++ src/Commands/ButtonCommands/rp/toggle.ts | 10 +++- src/Events/guildEvents/guildDelete/cache.ts | 2 +- .../messageCreate/commandHandler.ts | 8 ++- .../startupTasks/customBotCommands.ts | 4 +- 23 files changed, 149 insertions(+), 101 deletions(-) create mode 100644 src/BaseClient/ClientHelperModules/cache/discord/commands.ts diff --git a/src/BaseClient/ClientHelperModules/cache.ts b/src/BaseClient/ClientHelperModules/cache.ts index e680f8bd8..4106e455f 100644 --- a/src/BaseClient/ClientHelperModules/cache.ts +++ b/src/BaseClient/ClientHelperModules/cache.ts @@ -12,6 +12,7 @@ import webhooks, { Webhooks } from './cache/discord/webhooks.js'; import welcomeScreens, { WelcomeScreens } from './cache/discord/welcomeScreens.js'; import onboarding, { Onboarding } from './cache/discord/onboarding.js'; import invites, { Invites } from './cache/discord/invites.js'; +import commands, { Commands } from './cache/discord/commands.js'; import giveawayClaimTimeout, { GiveawayClaimTimeout } from './cache/bot/giveawayClaimTimeout.js'; import mutes, { Mutes } from './cache/bot/mutes.js'; @@ -58,7 +59,7 @@ const cache: { stickyTimeouts: StickyTimeouts; deleteThreads: DeleteThreads; apis: Map; - commands: Map; + commands: Commands; punishments: Set; antispam: Map[]>; deleteSuggestions: DeleteSuggestions; @@ -103,7 +104,7 @@ const cache: { auditLogs, deleteThreads, apis: new Map(), - commands: new Map(), + commands, punishments: new Set(), antispam: new Map(), deleteSuggestions, diff --git a/src/BaseClient/ClientHelperModules/cache/discord/commands.ts b/src/BaseClient/ClientHelperModules/cache/discord/commands.ts new file mode 100644 index 000000000..ad5853425 --- /dev/null +++ b/src/BaseClient/ClientHelperModules/cache/discord/commands.ts @@ -0,0 +1,59 @@ +import * as Discord from 'discord.js'; + +/** + * Interface for managing commands for a Discord guild. + */ +export interface Commands { + /** + * Retrieves the commands for a given guild and command ID. + * @param guild The Discord guild to retrieves for. + * @param commandId The ID of the command to retrieves for. + * @returns A Promise that resolves to an array of ApplicationCommands, + * or undefined if no commands are found. + */ + get: (guild: Discord.Guild, commandId: string) => Promise; + + /** + * Sets the command s for a given guild and command ID. + * @param guildId The ID of the guild to sets for. + * @param commandId The ID of the command to sets for. + * @param command An array of ApplicationCommands to set. + */ + set: (guildId: string, commandId: string, command: Discord.ApplicationCommand) => void; + + /** + * Deletes the commands for a given guild and command ID. + * @param guildId The ID of the guild to deletes for. + * @param commandId The ID of the command to deletes for. + */ + delete: (guildId: string, commandId: string) => void; + + /** + * A cache of commands, keyed by guild ID and then by command ID. + */ + cache: Map>; +} + +const self: Commands = { + get: async (guild, commandId) => { + const cached = self.cache.get(guild.id)?.get(commandId); + if (cached) return cached; + + const requestHandler = (await import('../../requestHandler.js')).request; + const fetched = await requestHandler.commands.getGuildCommands(guild); + if ('message' in fetched) return undefined; + + return fetched?.find((f) => f.id === commandId); + }, + set: (guildId, commandId, command) => { + if (!self.cache.get(guildId)) self.cache.set(guildId, new Map()); + self.cache.get(guildId)?.set(commandId, command); + }, + delete: (guildId, commandId) => { + if (self.cache.get(guildId)?.size === 1) self.cache.delete(guildId); + else self.cache.get(guildId)?.delete(commandId); + }, + cache: new Map(), +}; + +export default self; diff --git a/src/BaseClient/ClientHelperModules/getCustomCommand.ts b/src/BaseClient/ClientHelperModules/getCustomCommand.ts index 958431b52..38ff66155 100644 --- a/src/BaseClient/ClientHelperModules/getCustomCommand.ts +++ b/src/BaseClient/ClientHelperModules/getCustomCommand.ts @@ -19,6 +19,7 @@ export default async (guild: Discord.Guild | undefined | null, name: CommandName client.application?.commands.cache.find((c) => c.name === name); return guild - ? cache.commands.get(guild.id)?.find((c) => c.name === name) ?? clientCommand + ? [...(cache.commands.cache.get(guild.id)?.values() ?? [])]?.find((c) => c.name === name) ?? + clientCommand : clientCommand; }; diff --git a/src/BaseClient/ClientHelperModules/helpHelpers/getEmbeds.ts b/src/BaseClient/ClientHelperModules/helpHelpers/getEmbeds.ts index afdc1e4ab..448c74256 100644 --- a/src/BaseClient/ClientHelperModules/helpHelpers/getEmbeds.ts +++ b/src/BaseClient/ClientHelperModules/helpHelpers/getEmbeds.ts @@ -34,7 +34,10 @@ export default ( ) => { const lan = language.slashCommands.help; const fetchedCommands = cmd.guildId - ? cache.commands.get(cmd.guildId) ?? cmd.client.application.commands.cache.map((c) => c) + ? [ + ...(cache.commands.cache.get(cmd.guildId)?.values() ?? + cmd.client.application.commands.cache.map((c) => c)), + ] : cmd.client.application.commands.cache.map((c) => c); const embed: Discord.APIEmbed = { diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGlobalCommands.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGlobalCommands.ts index 94e860ddc..811c8eee6 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGlobalCommands.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGlobalCommands.ts @@ -16,10 +16,13 @@ export default async (guild: Discord.Guild, body: Discord.RESTPutAPIApplicationC .bulkOverwriteGlobalCommands(await getBotIdFromGuild(guild), body) .then((cmds) => { const parsed = cmds.map((cmd) => new Classes.ApplicationCommand(guild.client, cmd)); - if (cache.apis.get(guild.id)) { - cache.commands.set(guild.id, parsed); - return parsed; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + parsed.forEach((p) => { + cache.commands.cache.get(guild.id)?.set(p.id, p); + + if (cache.apis.get(guild.id)) return; + guild.commands.cache.set(p.id, p); + }); parsed.forEach((p) => guild.client.application.commands.cache.set(p.id, p)); return parsed; diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGuildCommands.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGuildCommands.ts index 3605931ac..9d42aabe0 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGuildCommands.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/bulkOverwriteGuildCommands.ts @@ -21,12 +21,14 @@ export default async ( const parsed = cmds.map( (cmd) => new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id), ); - if (cache.apis.get(guild.id)) { - cache.commands.set(guild.id, parsed); - return parsed; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + parsed.forEach((p) => { + cache.commands.cache.get(guild.id)?.set(p.id, p); + + if (cache.apis.get(guild.id)) return; + guild.commands.cache.set(p.id, p); + }); - parsed.forEach((p) => guild.commands.cache.set(p.id, p)); return parsed; }) .catch((e) => { diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/createGlobalCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/createGlobalCommand.ts index fa825c35d..9e0eb5958 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/createGlobalCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/createGlobalCommand.ts @@ -17,15 +17,10 @@ export default async (guild: Discord.Guild, body: Discord.RESTPostAPIApplication .createGlobalCommand(await getBotIdFromGuild(guild), body) .then((cmd) => { const parsed = new Classes.ApplicationCommand(guild.client, cmd); + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + cache.commands.cache.get(guild.id)?.set(parsed.id, parsed); - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed)); - - return parsed; - } - - if (guild.client.application.commands.cache.get(parsed.id)) return parsed; + if (cache.apis.get(guild.id)) return parsed; guild.client.application.commands.cache.set(parsed.id, parsed); return parsed; }) diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/createGuildCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/createGuildCommand.ts index 94c1a1e0b..0234245b1 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/createGuildCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/createGuildCommand.ts @@ -19,13 +19,10 @@ export default async ( .createGuildCommand(await getBotIdFromGuild(guild), guild.id, body) .then((cmd) => { const parsed = new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id); - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed)); - - return parsed; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + cache.commands.cache.get(guild.id)?.set(parsed.id, parsed); + if (cache.apis.get(guild.id)) return parsed; guild.commands.cache.set(cmd.id, parsed); return parsed; }) diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGlobalCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGlobalCommand.ts index 5e2615bcf..c58a93540 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGlobalCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGlobalCommand.ts @@ -16,17 +16,9 @@ export default async (guild: Discord.Guild, commandId: string) => (cache.apis.get(guild.id) ?? API).applicationCommands .deleteGlobalCommand(await getBotIdFromGuild(guild), commandId) .then(() => { - if (cache.apis.get(guild.id)) { - cache.commands.set( - guild.id, - cache.commands.get(guild.id)!.filter((c) => c.id !== commandId), - ); - - if (cache.commands.get(guild.id)!.length > 0) return; - cache.commands.delete(guild.id); - return; - } + cache.commands.delete(guild.id, commandId); + if (cache.apis.get(guild.id)) return; guild.client.application.commands.cache.delete(commandId); }) .catch((e) => { diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGuildCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGuildCommand.ts index 1c2b7da81..054c35675 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGuildCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/deleteGuildCommand.ts @@ -15,17 +15,9 @@ export default async (guild: Discord.Guild, commandId: string) => (cache.apis.get(guild.id) ?? API).applicationCommands .deleteGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId) .then(() => { - if (cache.apis.get(guild.id)) { - cache.commands.set( - guild.id, - cache.commands.get(guild.id)!.filter((c) => c.id !== commandId), - ); - - if (cache.commands.get(guild.id)!.length > 0) return; - cache.commands.delete(guild.id); - return; - } + cache.commands.delete(guild.id, commandId); + if (cache.apis.get(guild.id)) return; guild.commands.cache.delete(commandId); }) .catch((e) => { diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/editGlobalCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/editGlobalCommand.ts index d93d21c60..6dedd2719 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/editGlobalCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/editGlobalCommand.ts @@ -21,14 +21,10 @@ export default async ( .editGlobalCommand(await getBotIdFromGuild(guild), commandId, body) .then((cmd) => { const parsed = new Classes.ApplicationCommand(guild.client, cmd); + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + cache.commands.cache.get(guild.id)?.set(parsed.id, parsed); - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed)); - - return parsed; - } - + if (cache.apis.get(guild.id)) return parsed; if (guild.client.application.commands.cache.get(parsed.id)) return parsed; guild.client.application.commands.cache.set(parsed.id, parsed); return parsed; diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommand.ts index 9f8579085..cbaaff1bb 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommand.ts @@ -1,9 +1,9 @@ import * as Discord from 'discord.js'; import { API } from '../../../Client.js'; -import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js'; -import cache from '../../cache.js'; import * as Classes from '../../../Other/classes.js'; +import cache from '../../cache.js'; import error from '../../error.js'; +import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js'; /** * Edits a guild command for a given guild. @@ -21,20 +21,10 @@ export default async ( .editGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId, body) .then((cmd) => { const parsed = new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id); - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]); - else { - cache.commands.set( - guild.id, - cache.commands - .get(guild.id)! - .filter((c) => c.id !== parsed.id)! - .concat(parsed), - ); - } - return parsed; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + cache.commands.cache.get(guild.id)?.set(parsed.id, parsed); + if (cache.apis.get(guild.id)) return parsed; guild.commands.cache.set(parsed.id, parsed); return parsed; }) diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommandPermissions.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommandPermissions.ts index b49e42868..1ef07ceea 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommandPermissions.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/editGuildCommandPermissions.ts @@ -21,6 +21,10 @@ export default async ( ) => (cache.apis.get(guild.id) ?? API).applicationCommands .editGuildCommandPermissions(userToken, await getBotIdFromGuild(guild), guild.id, commandId, body) + .then((res) => { + cache.commandPermissions.set(guild.id, commandId, res.permissions); + return res.permissions; + }) .catch((e) => { error(guild, new Error((e as Discord.DiscordAPIError).message)); return e as Discord.DiscordAPIError; diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommand.ts index 8472d1cdd..e7f1ca910 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommand.ts @@ -13,17 +13,15 @@ import error from '../../error.js'; */ export default async (guild: Discord.Guild, commandId: string) => guild.client.application.commands.cache.get(commandId) ?? + cache.commands.cache.get(guild.id)?.get(commandId) ?? (cache.apis.get(guild.id) ?? API).applicationCommands .getGlobalCommand(await getBotIdFromGuild(guild), commandId) .then((cmd) => { const parsed = new Classes.ApplicationCommand(guild.client, cmd); - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed)); - - return parsed; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + cache.commands.cache.get(guild.id)?.set(parsed.id, parsed); + if (cache.apis.get(guild.id)) return parsed; if (guild.client.application.commands.cache.get(parsed.id)) return parsed; guild.client.application.commands.cache.set(parsed.id, parsed); return parsed; diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommands.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommands.ts index cb55562b4..5088cf3f6 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommands.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGlobalCommands.ts @@ -16,14 +16,12 @@ export default async (guild: Discord.Guild, query?: Discord.RESTGetAPIApplicatio .getGlobalCommands(await getBotIdFromGuild(guild), query) .then((cmds) => { const parsed = cmds.map((cmd) => new Classes.ApplicationCommand(guild.client, cmd)); - parsed.forEach((p) => { - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [p]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(p)); - return; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + parsed.forEach((p) => { + cache.commands.cache.get(guild.id)?.set(p.id, p); + if (cache.apis.get(guild.id)) return; if (guild.client.application.commands.cache.get(p.id)) return; guild.client.application.commands.cache.set(p.id, p); }); diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommand.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommand.ts index e0cc2598a..bd788f225 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommand.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommand.ts @@ -17,13 +17,10 @@ export default async (guild: Discord.Guild, commandId: string) => .getGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId) .then((cmd) => { const parsed = new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id); - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed)); - - return parsed; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + cache.commands.cache.get(guild.id)?.set(parsed.id, parsed); + if (cache.apis.get(guild.id)) return parsed; guild.commands.cache.set(cmd.id, parsed); return parsed; }) diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandPermissions.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandPermissions.ts index 000740a05..a24b18a81 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandPermissions.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandPermissions.ts @@ -1,8 +1,8 @@ import * as Discord from 'discord.js'; import { API } from '../../../Client.js'; -import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js'; import cache from '../../cache.js'; import error from '../../error.js'; +import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js'; /** * Retrieves the permissions for a specific command in a guild. @@ -13,6 +13,10 @@ import error from '../../error.js'; export default async (guild: Discord.Guild, commandId: string) => (cache.apis.get(guild.id) ?? API).applicationCommands .getGuildCommandPermissions(await getBotIdFromGuild(guild), guild.id, commandId) + .then((res) => { + cache.commandPermissions.set(guild.id, commandId, res.permissions); + return res.permissions; + }) .catch((e) => { error(guild, new Error((e as Discord.DiscordAPIError).message)); return e as Discord.DiscordAPIError; diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommands.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommands.ts index 3a3dcd9f3..e15c0293a 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommands.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommands.ts @@ -21,14 +21,12 @@ export default async ( const parsed = cmds.map( (cmd) => new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id), ); - parsed.forEach((p) => { - if (cache.apis.get(guild.id)) { - if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [p]); - else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(p)); - return; - } + if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map()); + parsed.forEach((p) => { + cache.commands.cache.get(guild.id)?.set(p.id, p); + if (cache.apis.get(guild.id)) return; if (guild.commands.cache.get(p.id)) return; guild.commands.cache.set(p.id, p); }); diff --git a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandsPermissions.ts b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandsPermissions.ts index 306debeb3..e0816f54a 100644 --- a/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandsPermissions.ts +++ b/src/BaseClient/ClientHelperModules/requestHandler/commands/getGuildCommandsPermissions.ts @@ -12,6 +12,14 @@ import error from '../../error.js'; export default async (guild: Discord.Guild) => (cache.apis.get(guild.id) ?? API).applicationCommands .getGuildCommandsPermissions(await getBotIdFromGuild(guild), guild.id) + .then((res) => { + res.forEach((r) => { + cache.commandPermissions.set(guild.id, r.id, r.permissions); + return r.permissions; + }); + + return res; + }) .catch((e) => { error(guild, new Error((e as Discord.DiscordAPIError).message)); return e as Discord.DiscordAPIError; diff --git a/src/Commands/ButtonCommands/rp/toggle.ts b/src/Commands/ButtonCommands/rp/toggle.ts index 34c28450c..29c0967a0 100644 --- a/src/Commands/ButtonCommands/rp/toggle.ts +++ b/src/Commands/ButtonCommands/rp/toggle.ts @@ -37,7 +37,10 @@ export default async (cmd: Discord.ButtonInteraction) => { const deleteAll = async (cmd: Discord.ButtonInteraction<'cached'>) => { await ch.request.commands.bulkOverwriteGuildCommands( cmd.guild, - (ch.cache.commands.get(cmd.guild.id) ?? cmd.guild.commands.cache.map((c) => c)) + ( + [...(ch.cache.commands.cache.get(cmd.guild.id)?.values() ?? [])] ?? + cmd.guild.commands.cache.map((c) => c) + ) .filter((c) => !!c.guildId) .map((c) => c.toJSON() as Discord.APIApplicationCommand) .filter((c) => !ch.constants.commands.interactions.find((i) => i.name === c.name)), @@ -100,7 +103,10 @@ export const create = async (guild: Discord.Guild) => { await ch.request.commands.bulkOverwriteGuildCommands(guild, [ ...registerCommands.map((c) => c.toJSON()), - ...(ch.cache.commands.get(guild.id) ?? guild.commands.cache.map((c) => c)) + ...( + [...(ch.cache.commands.cache.get(guild.id)?.values() ?? [])] ?? + guild.commands.cache.map((c) => c) + ) .map((c) => c.toJSON() as Discord.APIApplicationCommand) .filter((c) => !ch.constants.commands.interactions.find((i) => i.name === c.name)), ]); diff --git a/src/Events/guildEvents/guildDelete/cache.ts b/src/Events/guildEvents/guildDelete/cache.ts index 9155f18ad..2c19c3457 100644 --- a/src/Events/guildEvents/guildDelete/cache.ts +++ b/src/Events/guildEvents/guildDelete/cache.ts @@ -49,7 +49,7 @@ export default (guild: Discord.Guild) => { ch.cache.deleteThreads.cache.get(guild.id)?.forEach((i) => i.cancel()); ch.cache.deleteThreads.cache.delete(guild.id); ch.cache.apis.delete(guild.id); - ch.cache.commands.delete(guild.id); + ch.cache.commands.cache.delete(guild.id); ch.cache.deleteSuggestions.cache.get(guild.id)?.forEach((i) => i.cancel()); ch.cache.deleteSuggestions.cache.delete(guild.id); ch.cache.vcDeleteTimeout.cache.get(guild.id)?.forEach((i) => i.cancel()); diff --git a/src/Events/messageEvents/messageCreate/commandHandler.ts b/src/Events/messageEvents/messageCreate/commandHandler.ts index 51d9da786..148331b1f 100644 --- a/src/Events/messageEvents/messageCreate/commandHandler.ts +++ b/src/Events/messageEvents/messageCreate/commandHandler.ts @@ -208,7 +208,9 @@ export const checkCommandPermissions = ( commandName: string, ) => { const slashCommand = - ch.cache.commands.get(msg.guildId)?.find((c) => c.name === commandName) ?? + [...(ch.cache.commands.cache.get(msg.guildId)?.values() ?? [])].find( + (c) => c.name === commandName, + ) ?? client.application?.commands.cache.find((c) => c.name === commandName) ?? msg.guild.commands.cache.find((c) => c.name === commandName); @@ -266,7 +268,9 @@ const checkCommandIsEnabled = async ( command: CT.Command, ) => { const slashCommand = - ch.cache.commands.get(msg.guildId)?.find((c) => c.name === commandName) ?? + [...(ch.cache.commands.cache.get(msg.guildId)?.values() ?? [])].find( + (c) => c.name === commandName, + ) ?? client.application?.commands.cache.find((c) => c.name === commandName) ?? msg.guild.commands.cache.find((c) => c.name === commandName); diff --git a/src/Events/readyEvents/startupTasks/customBotCommands.ts b/src/Events/readyEvents/startupTasks/customBotCommands.ts index 6f457da32..74aabb862 100644 --- a/src/Events/readyEvents/startupTasks/customBotCommands.ts +++ b/src/Events/readyEvents/startupTasks/customBotCommands.ts @@ -24,7 +24,7 @@ export default async () => { return; } - ch.cache.commands.set(guild.id, [...commands, ...(ch.cache.commands.get(guild.id) ?? [])]); + commands.forEach((c) => ch.cache.commands.set(guild.id, c.id, c)); }; const guildCommands = async () => { @@ -37,7 +37,7 @@ export default async () => { return; } - ch.cache.commands.set(guild.id, [...commands, ...(ch.cache.commands.get(guild.id) ?? [])]); + commands.forEach((c) => ch.cache.commands.set(guild.id, c.id, c)); }; globalCommands();