From ea49f7ca74892495dd53f8d315086035c1814149 Mon Sep 17 00:00:00 2001 From: monbrey Date: Fri, 25 Jun 2021 05:42:56 +1000 Subject: [PATCH] feat(InteractionCreate): move to an Action handler (#5906) --- src/client/actions/ActionsManager.js | 1 + src/client/actions/InteractionCreate.js | 46 +++++++++++++++++++ .../websocket/handlers/INTERACTION_CREATE.js | 35 +------------- typings/index.d.ts | 3 +- 4 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 src/client/actions/InteractionCreate.js diff --git a/src/client/actions/ActionsManager.js b/src/client/actions/ActionsManager.js index 50178b0fafc5..856594b07ad6 100644 --- a/src/client/actions/ActionsManager.js +++ b/src/client/actions/ActionsManager.js @@ -17,6 +17,7 @@ class ActionsManager { this.register(require('./ChannelUpdate')); this.register(require('./GuildDelete')); this.register(require('./GuildUpdate')); + this.register(require('./InteractionCreate')); this.register(require('./InviteCreate')); this.register(require('./InviteDelete')); this.register(require('./GuildMemberRemove')); diff --git a/src/client/actions/InteractionCreate.js b/src/client/actions/InteractionCreate.js new file mode 100644 index 000000000000..3559963efe2c --- /dev/null +++ b/src/client/actions/InteractionCreate.js @@ -0,0 +1,46 @@ +'use strict'; + +const Action = require('./Action'); +const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants'); +const Structures = require('../../util/Structures'); + +class InteractionCreateAction extends Action { + handle(data) { + const client = this.client; + + // Resolve and cache partial channels for Interaction#channel getter + this.getChannel(data); + + let InteractionType; + switch (data.type) { + case InteractionTypes.APPLICATION_COMMAND: + InteractionType = Structures.get('CommandInteraction'); + break; + case InteractionTypes.MESSAGE_COMPONENT: + switch (data.data.component_type) { + case MessageComponentTypes.BUTTON: + InteractionType = Structures.get('ButtonInteraction'); + break; + default: + client.emit( + Events.DEBUG, + `[INTERACTION] Received component interaction with unknown type: ${data.data.component_type}`, + ); + return; + } + break; + default: + client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`); + return; + } + + /** + * Emitted when an interaction is created. + * @event Client#interaction + * @param {Interaction} interaction The interaction which was created + */ + client.emit(Events.INTERACTION_CREATE, new InteractionType(client, data)); + } +} + +module.exports = InteractionCreateAction; diff --git a/src/client/websocket/handlers/INTERACTION_CREATE.js b/src/client/websocket/handlers/INTERACTION_CREATE.js index 03ebc9a9213e..5bf30fcc7269 100644 --- a/src/client/websocket/handlers/INTERACTION_CREATE.js +++ b/src/client/websocket/handlers/INTERACTION_CREATE.js @@ -1,36 +1,5 @@ 'use strict'; -const { Events, InteractionTypes, MessageComponentTypes } = require('../../../util/Constants'); -const Structures = require('../../../util/Structures'); - -module.exports = (client, { d: data }) => { - let InteractionType; - switch (data.type) { - case InteractionTypes.APPLICATION_COMMAND: - InteractionType = Structures.get('CommandInteraction'); - break; - case InteractionTypes.MESSAGE_COMPONENT: - switch (data.data.component_type) { - case MessageComponentTypes.BUTTON: - InteractionType = Structures.get('ButtonInteraction'); - break; - default: - client.emit( - Events.DEBUG, - `[INTERACTION] Received component interaction with unknown type: ${data.data.component_type}`, - ); - return; - } - break; - default: - client.emit(Events.DEBUG, `[INTERACTION] Received interaction with unknown type: ${data.type}`); - return; - } - - /** - * Emitted when an interaction is created. - * @event Client#interaction - * @param {Interaction} interaction The interaction which was created - */ - client.emit(Events.INTERACTION_CREATE, new InteractionType(client, data)); +module.exports = (client, packet) => { + client.actions.InteractionCreate.handle(packet.d); }; diff --git a/typings/index.d.ts b/typings/index.d.ts index 6c1b6d72de96..6015ea9172dc 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -504,7 +504,7 @@ declare module 'discord.js' { export class CommandInteraction extends Interaction { public readonly command: ApplicationCommand | null; - public channel: TextChannel | DMChannel | NewsChannel; + public readonly channel: TextChannel | DMChannel | NewsChannel | PartialDMChannel | null; public channelID: Snowflake; public commandID: Snowflake; public commandName: string; @@ -1366,6 +1366,7 @@ declare module 'discord.js' { } export class MessageComponentInteraction extends Interaction { + public readonly channel: TextChannel | DMChannel | NewsChannel | PartialDMChannel | null; public componentType: MessageComponentType; public customID: string; public deferred: boolean;