From b2a7ad149e3738e2201637bb628db6af89ec8d1a Mon Sep 17 00:00:00 2001 From: Monbrey Date: Wed, 23 Jun 2021 08:39:29 +1000 Subject: [PATCH 1/4] feat(InteractionCreate): move to Action handlers for partial resolution --- src/client/actions/InteractionCreate.js | 44 +++++++++++++++++++ .../websocket/handlers/INTERACTION_CREATE.js | 35 +-------------- 2 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 src/client/actions/InteractionCreate.js diff --git a/src/client/actions/InteractionCreate.js b/src/client/actions/InteractionCreate.js new file mode 100644 index 000000000000..8b5611eac78b --- /dev/null +++ b/src/client/actions/InteractionCreate.js @@ -0,0 +1,44 @@ +'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; + 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); }; From 13d6cfa3e49608b244dc8f2eb0fcc5c7647e6e57 Mon Sep 17 00:00:00 2001 From: Monbrey Date: Wed, 23 Jun 2021 08:43:09 +1000 Subject: [PATCH 2/4] fix: register the action --- src/client/actions/ActionsManager.js | 1 + src/client/actions/InteractionCreate.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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 index 8b5611eac78b..9f9243e2abf4 100644 --- a/src/client/actions/InteractionCreate.js +++ b/src/client/actions/InteractionCreate.js @@ -1,8 +1,8 @@ 'use strict'; const Action = require('./Action'); -const { Events, InteractionTypes, MessageComponentTypes } = require('../../../util/Constants'); -const Structures = require('../../../util/Structures'); +const { Events, InteractionTypes, MessageComponentTypes } = require('../../util/Constants'); +const Structures = require('../../util/Structures'); class InteractionCreateAction extends Action { handle(data) { From 1a19687d2c3688f4f5709a815a476b072cf45174 Mon Sep 17 00:00:00 2001 From: Monbrey Date: Wed, 23 Jun 2021 19:49:50 +1000 Subject: [PATCH 3/4] chore: add comment explaining unused expression --- src/client/actions/InteractionCreate.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/actions/InteractionCreate.js b/src/client/actions/InteractionCreate.js index 9f9243e2abf4..3559963efe2c 100644 --- a/src/client/actions/InteractionCreate.js +++ b/src/client/actions/InteractionCreate.js @@ -7,6 +7,8 @@ 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; From 880ed97a4088694547c7d9477e1a7004a503fe65 Mon Sep 17 00:00:00 2001 From: Monbrey Date: Thu, 24 Jun 2021 07:37:50 +1000 Subject: [PATCH 4/4] types(Interaction): expend channel typings --- typings/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 64505d1e9461..0a51ca638a00 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -503,7 +503,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; @@ -1364,6 +1364,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;