Skip to content

Commit

Permalink
feat(InteractionCreate): move to an Action handler (#5906)
Browse files Browse the repository at this point in the history
  • Loading branch information
monbrey authored Jun 24, 2021
1 parent ee025b0 commit ea49f7c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/client/actions/ActionsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
46 changes: 46 additions & 0 deletions src/client/actions/InteractionCreate.js
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 2 additions & 33 deletions src/client/websocket/handlers/INTERACTION_CREATE.js
Original file line number Diff line number Diff line change
@@ -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);
};
3 changes: 2 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ea49f7c

Please sign in to comment.