Add class for interactions

This commit is contained in:
Marius Begby committed 2023-08-31 17:36:16 +02:00
1 parent d0f1e940a5
commit b96a73df6f
37 files changed
+1038 -1012

No files matched your search

+17 -6
View File
@@ -1,20 +1,31 @@
import { AutocompleteInteraction } from 'discord.js';
import { Logger } from 'pino';
import loggerModule from '../services/logger';
import { ExtendedClient } from '../types/clientTypes';
import { BaseAutocompleteInteraction } from '../types/interactionTypes';
export const handleAutocomplete = async (interaction: AutocompleteInteraction, executionId: string) => {
export const handleAutocomplete = async (
interaction: AutocompleteInteraction,
client: ExtendedClient,
executionId: string,
interactionIdentifier: string
) => {
// TODO: Define TS Type for handlers, and require logger constant?
const logger: Logger = loggerModule.child({
source: 'interactionCutocompleteHandler.ts',
source: 'interactionAutocompleteHandler.ts',
module: 'handler',
name: 'interactionAutocompleteHandler',
executionId: executionId
});
// TODO: Create TS Type for autocompleteModule
const autocompleteModule = await import(`../interactions/autocomplete/${interaction.commandName}.js`);
const { default: autocomplete } = autocompleteModule;
const autocomplete: BaseAutocompleteInteraction = client.autocompleteInteractions!.get(
interactionIdentifier
) as BaseAutocompleteInteraction;
if (!autocomplete) {
logger.warn(`Interaction created but autocomplete '${interactionIdentifier}' was not found.`);
return;
}
logger.debug('Executing autocomplete interaction.');
logger.debug(`Executing autocomplete interaction '${interactionIdentifier}'.`);
await autocomplete.execute({ interaction, executionId });
};
+14 -11
View File
@@ -1,13 +1,15 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { Logger } from 'pino';
import loggerModule from '../services/logger';
import { Command, ExtendedClient } from '../types/clientTypes';
import { ExtendedClient } from '../types/clientTypes';
import { BaseSlashCommandInteraction } from '../types/interactionTypes';
import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator';
export const handleCommand = async (
interaction: ChatInputCommandInteraction,
client: ExtendedClient,
executionId: string
executionId: string,
interactionIdentifier: string
) => {
const logger: Logger = loggerModule.child({
source: 'interactionCommandHandler.ts',
@@ -19,17 +21,18 @@ export const handleCommand = async (
await interaction.deferReply();
logger.debug('Interaction deferred.');
// TODO: Update TS Type for command
const command: Command = client.commands?.get(interaction.commandName) as Command;
if (!command) {
logger.warn(`Interaction created but command '${interaction.commandName}' was not found.`);
return;
}
if (await cannotSendMessageInChannel({ interaction, executionId })) {
return;
}
logger.debug('Executing command interaction.');
await command.execute({ interaction, client, executionId });
const slashCommand: BaseSlashCommandInteraction = client.slashCommandInteractions!.get(
interactionIdentifier
) as BaseSlashCommandInteraction;
if (!slashCommand) {
logger.warn(`Interaction created but slash command '${interactionIdentifier}' was not found.`);
return;
}
logger.debug(`Executing slash command interaction '${interactionIdentifier}'.`);
await slashCommand.execute({ interaction, client, executionId });
};
+19 -8
View File
@@ -1,9 +1,16 @@
import { MessageComponentInteraction } from 'discord.js';
import { Logger } from 'pino';
import loggerModule from '../services/logger';
import { ExtendedClient } from '../types/clientTypes';
import { BaseComponentInteraction } from '../types/interactionTypes';
import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator';
export const handleComponent = async (interaction: MessageComponentInteraction, executionId: string) => {
export const handleComponent = async (
interaction: MessageComponentInteraction,
client: ExtendedClient,
executionId: string,
interactionIdentifier: string
) => {
const logger: Logger = loggerModule.child({
source: 'interactionComponentHandler.js',
module: 'handler',
@@ -14,19 +21,23 @@ export const handleComponent = async (interaction: MessageComponentInteraction,
await interaction.deferReply();
logger.debug('Interaction deferred.');
const componentId: string = interaction.customId.split('_')[0];
const referenceId: string = interaction.customId.split('_')[1];
const componentId: string = interactionIdentifier.split('_')[0];
const referenceId: string = interactionIdentifier.split('_')[1];
logger.debug(`Parsed componentId: ${componentId}`);
logger.debug(`Parsed componentId '${componentId}' from identifier '${interactionIdentifier}'.`);
if (await cannotSendMessageInChannel({ interaction, executionId })) {
return;
}
// TODO: Create TS Type for component
const componentModule = await import(`../interactions/components/${componentId}.js`);
const { default: component } = componentModule;
const component: BaseComponentInteraction = client.componentInteractions!.get(
componentId
) as BaseComponentInteraction;
if (!component) {
logger.warn(`Interaction created but component '${componentId}' was not found.`);
return;
}
logger.debug('Executing component interaction.');
logger.debug(`Executing component interaction '${componentId}'.`);
await component.execute({ interaction, referenceId, executionId });
};
+9 -3
View File
@@ -1,5 +1,11 @@
import config from 'config';
import { ChatInputCommandInteraction, EmbedBuilder, Interaction, InteractionReplyOptions, MessageComponentInteraction } from 'discord.js';
import {
ChatInputCommandInteraction,
EmbedBuilder,
Interaction,
InteractionReplyOptions,
MessageComponentInteraction
} from 'discord.js';
import { Logger } from 'pino';
import loggerModule from '../services/logger';
import { BotOptions, EmbedOptions } from '../types/configTypes';
@@ -12,8 +18,8 @@ const botOptions: BotOptions = config.get('botOptions');
export const handleError = async (
interaction: Interaction,
error: CustomError,
interactionIdentifier: string,
executionId: string
executionId: string,
interactionIdentifier: string
) => {
const logger: Logger = loggerModule.child({
source: 'interactionErrorHandler.ts',