From 4886ae23abaff1f4f1e7f5d15c4f2670a9de54be Mon Sep 17 00:00:00 2001 From: Tiemen Date: Fri, 23 Jul 2021 21:19:17 +0200 Subject: [PATCH] refactor: only cache commands from own user (#6161) --- .../handlers/APPLICATION_COMMAND_CREATE.js | 13 ++++--------- .../handlers/APPLICATION_COMMAND_DELETE.js | 17 ++++++----------- .../handlers/APPLICATION_COMMAND_UPDATE.js | 17 +++++------------ src/managers/ApplicationCommandManager.js | 8 ++++---- src/structures/ApplicationCommand.js | 6 ++++++ typings/index.d.ts | 1 + 6 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js b/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js index f81a2b5204da..158033b5dcff 100644 --- a/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js +++ b/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js @@ -3,18 +3,13 @@ const { Events } = require('../../../util/Constants'); module.exports = (client, { d: data }) => { - let command; + const commandManager = data.guild_id ? client.guilds.cache.get(data.guild_id)?.commands : client.application.commands; + if (!commandManager) return; - if (data.guild_id) { - const guild = client.guilds.cache.get(data.guild_id); - if (!guild) return; - command = guild.commands._add(data); - } else { - command = client.application.commands._add(data); - } + const command = commandManager._add(data, data.application_id === client.application.id); /** - * Emitted when an application command is created. + * Emitted when a guild application command is created. * @event Client#applicationCommandCreate * @param {ApplicationCommand} command The command which was created */ diff --git a/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js b/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js index 99cc9352649d..d046461a4951 100644 --- a/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js +++ b/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js @@ -3,20 +3,15 @@ const { Events } = require('../../../util/Constants'); module.exports = (client, { d: data }) => { - let command; + const commandManager = data.guild_id ? client.guilds.cache.get(data.guild_id)?.commands : client.application.commands; + if (!commandManager) return; - if (data.guild_id) { - const guild = client.guilds.cache.get(data.guild_id); - if (!guild) return; - command = guild.commands._add(data); - guild.commands.cache.delete(data.id); - } else { - command = client.application.commands._add(data); - client.application.commands.cache.delete(data.id); - } + const isOwn = data.application_id === client.application.id; + const command = commandManager._add(data, isOwn); + if (isOwn) commandManager.cache.delete(data.id); /** - * Emitted when an application command is deleted. + * Emitted when a guild application command is deleted. * @event Client#applicationCommandDelete * @param {ApplicationCommand} command The command which was deleted */ diff --git a/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js b/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js index 252ed56fb250..d959b503d336 100644 --- a/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js +++ b/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js @@ -3,21 +3,14 @@ const { Events } = require('../../../util/Constants'); module.exports = (client, { d: data }) => { - let oldCommand; - let newCommand; + const commandManager = data.guild_id ? client.guilds.cache.get(data.guild_id)?.commands : client.application.commands; + if (!commandManager) return; - if (data.guild_id) { - const guild = client.guilds.cache.get(data.guild_id); - if (!guild) return; - oldCommand = guild.commands.cache.get(data.id)?._clone() ?? null; - newCommand = guild.commands._add(data); - } else { - oldCommand = client.application.commands.cache.get(data.id)?._clone() ?? null; - newCommand = client.application.commands._add(data); - } + const oldCommand = commandManager.cache.get(data.id)?._clone() ?? null; + const newCommand = commandManager._add(data, data.application_id === client.application.id); /** - * Emitted when an application command is updated. + * Emitted when a guild application command is updated. * @event Client#applicationCommandUpdate * @param {?ApplicationCommand} oldCommand The command before the update * @param {ApplicationCommand} newCommand The command after the update diff --git a/src/managers/ApplicationCommandManager.js b/src/managers/ApplicationCommandManager.js index 7b7c8dc70040..6195c2fadc3e 100644 --- a/src/managers/ApplicationCommandManager.js +++ b/src/managers/ApplicationCommandManager.js @@ -116,7 +116,7 @@ class ApplicationCommandManager extends CachedManager { const data = await this.commandPath({ guildId }).post({ data: this.constructor.transformCommand(command), }); - return this._add(data, undefined, guildId); + return this._add(data, !guildId, guildId); } /** @@ -146,7 +146,7 @@ class ApplicationCommandManager extends CachedManager { data: commands.map(c => this.constructor.transformCommand(c)), }); return data.reduce( - (coll, command) => coll.set(command.id, this._add(command, undefined, guildId)), + (coll, command) => coll.set(command.id, this._add(command, !guildId, guildId)), new Collection(), ); } @@ -171,7 +171,7 @@ class ApplicationCommandManager extends CachedManager { if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable'); const patched = await this.commandPath({ id, guildId }).patch({ data: this.constructor.transformCommand(data) }); - return this._add(patched, undefined, guildId); + return this._add(patched, !guildId, guildId); } /** @@ -193,7 +193,7 @@ class ApplicationCommandManager extends CachedManager { await this.commandPath({ id, guildId }).delete(); const cached = this.cache.get(id); - this.cache.delete(id); + if (!guildId) this.cache.delete(id); return cached ?? null; } diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index 64cf50369444..c167748cc521 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -19,6 +19,12 @@ class ApplicationCommand extends Base { */ this.id = data.id; + /** + * The parent application's id + * @type {Snowflake} + */ + this.applicationId = data.application_id; + /** * The guild this command is part of * @type {?Guild} diff --git a/typings/index.d.ts b/typings/index.d.ts index 5eb87edbf08b..5b85087b1f4f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -125,6 +125,7 @@ export abstract class Application extends Base { export class ApplicationCommand extends Base { public constructor(client: Client, data: unknown, guild?: Guild, guildId?: Snowflake); + public applicationId: Snowflake; public readonly createdAt: Date; public readonly createdTimestamp: number; public defaultPermission: boolean;