refactor: only cache commands from own user (#6161)

This commit is contained in:
Tiemen
2021-07-23 21:19:17 +02:00
committed by GitHub
parent 4f8ca2936a
commit 4886ae23ab
6 changed files with 26 additions and 36 deletions

View File

@@ -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
*/

View File

@@ -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
*/

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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}

1
typings/index.d.ts vendored
View File

@@ -125,6 +125,7 @@ export abstract class Application extends Base {
export class ApplicationCommand<PermissionsFetchType = {}> 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;