Skip to content

Commit

Permalink
feat: add support for application command events (#5596)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporoxx committed May 7, 2021
1 parent 452ac55 commit 9f74f95
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js
@@ -0,0 +1,22 @@
'use strict';

const { Events } = require('../../../util/Constants');

module.exports = (client, { d: data }) => {
let command;

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

/**
* Emitted when an application command is created.
* @event Client#applicationCommandCreate
* @param {ApplicationCommand} command The command which was created
*/
client.emit(Events.APPLICATION_COMMAND_CREATE, command);
};
24 changes: 24 additions & 0 deletions src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js
@@ -0,0 +1,24 @@
'use strict';

const { Events } = require('../../../util/Constants');

module.exports = (client, { d: data }) => {
let command;

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

/**
* Emitted when an application command is deleted.
* @event Client#applicationCommandDelete
* @param {ApplicationCommand} command The command which was deleted
*/
client.emit(Events.APPLICATION_COMMAND_DELETE, command);
};
26 changes: 26 additions & 0 deletions src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js
@@ -0,0 +1,26 @@
'use strict';

const { Events } = require('../../../util/Constants');

module.exports = (client, { d: data }) => {
let oldCommand;
let newCommand;

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

/**
* Emitted when an application command is updated.
* @event Client#applicationCommandUpdate
* @param {?ApplicationCommand} oldCommand The command before the update
* @param {ApplicationCommand} newCommand The command after the update
*/
client.emit(Events.APPLICATION_COMMAND_UPDATE, oldCommand, newCommand);
};
9 changes: 9 additions & 0 deletions src/util/Constants.js
Expand Up @@ -228,6 +228,9 @@ exports.Events = {
RATE_LIMIT: 'rateLimit',
INVALID_REQUEST_WARNING: 'invalidRequestWarning',
CLIENT_READY: 'ready',
APPLICATION_COMMAND_CREATE: 'applicationCommandCreate',
APPLICATION_COMMAND_DELETE: 'applicationCommandDelete',
APPLICATION_COMMAND_UPDATE: 'applicationCommandUpdate',
GUILD_CREATE: 'guildCreate',
GUILD_DELETE: 'guildDelete',
GUILD_UPDATE: 'guildUpdate',
Expand Down Expand Up @@ -310,6 +313,9 @@ exports.PartialTypes = keyMirror(['USER', 'CHANNEL', 'GUILD_MEMBER', 'MESSAGE',
* The type of a websocket message event, e.g. `MESSAGE_CREATE`. Here are the available events:
* * READY
* * RESUMED
* * APPLICATION_COMMAND_CREATE
* * APPLICATION_COMMAND_DELETE
* * APPLICATION_COMMAND_UPDATE
* * GUILD_CREATE
* * GUILD_DELETE
* * GUILD_UPDATE
Expand Down Expand Up @@ -350,6 +356,9 @@ exports.PartialTypes = keyMirror(['USER', 'CHANNEL', 'GUILD_MEMBER', 'MESSAGE',
exports.WSEvents = keyMirror([
'READY',
'RESUMED',
'APPLICATION_COMMAND_CREATE',
'APPLICATION_COMMAND_DELETE',
'APPLICATION_COMMAND_UPDATE',
'GUILD_CREATE',
'GUILD_DELETE',
'GUILD_UPDATE',
Expand Down
9 changes: 9 additions & 0 deletions typings/index.d.ts
Expand Up @@ -478,6 +478,9 @@ declare module 'discord.js' {
INVALID_REQUEST_WARNING: 'invalidRequestWarning';
CLIENT_READY: 'ready';
RESUMED: 'resumed';
APPLICATION_COMMAND_CREATE: 'applicationCommandCreate';
APPLICATION_COMMAND_DELETE: 'applicationCommandDelete';
APPLICATION_COMMAND_UPDATE: 'applicationCommandUpdate';
GUILD_CREATE: 'guildCreate';
GUILD_DELETE: 'guildDelete';
GUILD_UPDATE: 'guildUpdate';
Expand Down Expand Up @@ -2543,6 +2546,9 @@ declare module 'discord.js' {
type ChannelResolvable = Channel | Snowflake;

interface ClientEvents {
applicationCommandCreate: [command: ApplicationCommand];
applicationCommandDelete: [command: ApplicationCommand];
applicationCommandUpdate: [oldCommand: ApplicationCommand | null, newCommand: ApplicationCommand];
channelCreate: [channel: GuildChannel];
channelDelete: [channel: DMChannel | GuildChannel];
channelPinsUpdate: [channel: Channel | PartialDMChannel, date: Date];
Expand Down Expand Up @@ -3589,6 +3595,9 @@ declare module 'discord.js' {
type WSEventType =
| 'READY'
| 'RESUMED'
| 'APPLICATION_COMMAND_CREATE'
| 'APPLICATION_COMMAND_DELETE'
| 'APPLICATION_COMMAND_UPDATE'
| 'GUILD_CREATE'
| 'GUILD_DELETE'
| 'GUILD_UPDATE'
Expand Down

0 comments on commit 9f74f95

Please sign in to comment.