From 3be362f0968a8e3139aa04fc1a03ae9f5c346496 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Wed, 30 Aug 2023 22:16:26 +0200 Subject: [PATCH] WIP working example --- src/interactions/commands/player/test.ts | 10 ++++++---- src/types/interactionTypes.ts | 15 ++++----------- src/utils/deploySlashCommands.ts | 9 +-------- src/utils/registerClientCommands.ts | 23 ++++++----------------- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/interactions/commands/player/test.ts b/src/interactions/commands/player/test.ts index 2c13c88..af93a28 100644 --- a/src/interactions/commands/player/test.ts +++ b/src/interactions/commands/player/test.ts @@ -1,4 +1,4 @@ -import { SlashCommandBuilder } from 'discord.js'; +import { EmbedBuilder, Message, SlashCommandBuilder } from 'discord.js'; import { BaseCommandInteraction, BaseSlashCommandParams } from '../../../types/interactionTypes'; export class TestCommandInteraction extends BaseCommandInteraction { @@ -7,16 +7,18 @@ export class TestCommandInteraction extends BaseCommandInteraction { super(data); } - async execute(params: BaseSlashCommandParams): Promise { + async execute(params: BaseSlashCommandParams): Promise | void> { const { executionId, interaction } = params; - const logger = this.getLogger('test.ts', 'test', executionId, interaction); + const logger = this.getLogger('test.js', executionId, interaction); logger.info('test command executed'); logger.info(`${interaction.user.tag} executed test command`); logger.info(`Execution ID: ${executionId}`); logger.info(this.embedOptions.icons.success + ' Icon success from config.'); - return; + return await interaction.editReply({ + embeds: [new EmbedBuilder().setDescription('It works!').setColor(this.embedOptions.colors.success)] + }); } } diff --git a/src/types/interactionTypes.ts b/src/types/interactionTypes.ts index 89f392f..a039df7 100644 --- a/src/types/interactionTypes.ts +++ b/src/types/interactionTypes.ts @@ -72,17 +72,10 @@ export class CustomError extends Error { } abstract class BaseInteraction { - protected getLoggerBase( - module: string, - source: string, - name: string, - executionId: string, - interaction: Interaction - ): Logger { + protected getLoggerBase(module: string, source: string, executionId: string, interaction: Interaction): Logger { return loggerModule.child({ source: source, module: module, - name: name, executionId: executionId, shardId: interaction.guild?.shardId, guildId: interaction.guild?.id @@ -108,15 +101,15 @@ export abstract class BaseCommandInteraction extends BaseInteraction { isBeta?: boolean ) { super(); - this.data = data; + this.data = data.setDMPermission(false).setNSFW(false); this.isSystemCommand = isSystemCommand || false; this.isNew = isNew || false; this.isBeta = isBeta || false; this.embedOptions = config.get('embedOptions'); } - protected getLogger(source: string, name: string, executionId: string, interaction: Interaction): Logger { - return super.getLoggerBase('slashCommand', source, name, executionId, interaction); + protected getLogger(source: string, executionId: string, interaction: Interaction): Logger { + return super.getLoggerBase('slashCommand', source, executionId, interaction); } abstract execute(params: BaseSlashCommandParams): Promise | void>; diff --git a/src/utils/deploySlashCommands.ts b/src/utils/deploySlashCommands.ts index 1db4a73..8c27480 100644 --- a/src/utils/deploySlashCommands.ts +++ b/src/utils/deploySlashCommands.ts @@ -51,7 +51,6 @@ const rest: REST = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_ process.exit(1); } - /* for (const folder of commandFolders) { const commandFiles: string[] = fs .readdirSync(path.resolve(`./dist/interactions/commands/${folder}`)) @@ -59,18 +58,12 @@ const rest: REST = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_ for (const file of commandFiles) { // TODO: create commandModule type - const commandModule = await import(`../interactions/commands/${folder}/${file}`); - const command = commandModule.default; + const { default: command } = await import(`../interactions/commands/${folder}/${file}`); command.isSystemCommand ? systemCommands.push(command.data.toJSON()) : slashCommands.push(command.data.toJSON()); } } - */ - - const commandModule = await import('../interactions/commands/player/test.js'); - const command = commandModule.default; - slashCommands.push(command.data.toJSON()); try { logger.debug(`Bot user slash commands found: ${slashCommands.map((command) => `/${command.name}`).join(', ')}`); diff --git a/src/utils/registerClientCommands.ts b/src/utils/registerClientCommands.ts index f251f1f..cee2ed5 100644 --- a/src/utils/registerClientCommands.ts +++ b/src/utils/registerClientCommands.ts @@ -18,26 +18,23 @@ export const registerClientCommands = async ({ client, executionId }: RegisterCl logger.debug('Registering client commands...'); client.commands = new Collection(); - /* const commandFolders: string[] = fs.readdirSync(path.resolve('./dist/interactions/commands')); for (const folder of commandFolders) { logger.trace(`Registering client commands for folder '${folder}'...`); - + const commandFiles: string[] = fs .readdirSync(path.resolve(`./dist/interactions/commands/${folder}`)) .filter((file) => file.endsWith('.js')); - + for (const file of commandFiles) { try { // delete command from require cache delete require.cache[require.resolve(`../interactions/commands/${folder}/${file}`)]; - // register command - //const command = require(`../interactions/commands/${folder}/${file}`); - const commandModule = await import(`../interactions/commands/${folder}/${file}`); - - client.commands.delete(commandModule.default.data.name); - client.commands.set(commandModule.default.data.name, commandModule.default); + // load command and register it to client.commands collection + const { default: command } = await import(`../interactions/commands/${folder}/${file}`); + client.commands.delete(command.data.name); + client.commands.set(command.data.name, command); } catch (error) { if (error instanceof Error) { logger.error(`Error registering command ${folder}/${file}: ${error.message}`); @@ -46,15 +43,7 @@ export const registerClientCommands = async ({ client, executionId }: RegisterCl } } } - } - */ - - const commandModule = await import('../interactions/commands/player/test.js'); - const command = commandModule.default; - - logger.info(command.data); - client.commands.set('test', command); logger.trace('Registering client commands complete.'); };