Skip to content

Commit

Permalink
refactor: only cache commands from own user (#6161)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaTiemsz committed Jul 23, 2021
1 parent 4f8ca29 commit 4886ae2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
13 changes: 4 additions & 9 deletions src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js
Expand Up @@ -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
*/
Expand Down
17 changes: 6 additions & 11 deletions src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js
Expand Up @@ -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
*/
Expand Down
17 changes: 5 additions & 12 deletions src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/managers/ApplicationCommandManager.js
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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(),
);
}
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/structures/ApplicationCommand.js
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 4886ae2

Please sign in to comment.