From 335f4bb84e0f47431bb9bb7f8e1db4146c8f5523 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Thu, 31 Aug 2023 11:03:04 +0200 Subject: [PATCH] WIP few changes to class --- src/interactions/commands/info/help.ts | 70 ++++++++++-------------- src/interactions/commands/player/test.ts | 26 +++++---- src/types/interactionTypes.ts | 22 +++++--- 3 files changed, 57 insertions(+), 61 deletions(-) diff --git a/src/interactions/commands/info/help.ts b/src/interactions/commands/info/help.ts index d1cc392..2f98c3e 100644 --- a/src/interactions/commands/info/help.ts +++ b/src/interactions/commands/info/help.ts @@ -1,38 +1,22 @@ -import config from 'config'; +import { EmbedBuilder, SlashCommandBuilder, SlashCommandNumberOption, SlashCommandStringOption } from 'discord.js'; import { - EmbedBuilder, - SharedSlashCommandOptions, - SlashCommandBuilder, - SlashCommandNumberOption, - SlashCommandStringOption -} from 'discord.js'; -import { Logger } from 'pino'; -import loggerModule from '../../../services/logger'; -import { BotOptions, EmbedOptions } from '../../../types/configTypes'; -import { BaseSlashCommandInteraction } from '../../../types/interactionTypes'; + BaseSlashCommandInteraction, + BaseSlashCommandParams, + BaseSlashCommandReturnType +} from '../../../types/interactionTypes'; -const embedOptions: EmbedOptions = config.get('embedOptions'); -const botOptions: BotOptions = config.get('botOptions'); +class HelpCommand extends BaseSlashCommandInteraction { + constructor() { + const data = new SlashCommandBuilder() + .setName('help') + .setDescription('Show a list of commands and their usage.'); + super(data); + } -const command: BaseSlashCommandInteraction = { - isNew: false, - isBeta: false, - data: new SlashCommandBuilder() - .setName('help') - .setDescription('Show a list of commands and their usage.') - .setDMPermission(false) - .setNSFW(false), - execute: async ({ interaction, client, executionId }) => { - const logger: Logger = loggerModule.child({ - source: 'help.js', - module: 'slashCommand', - name: '/help', - executionId: executionId, - shardId: interaction.guild?.shardId, - guildId: interaction.guild?.id - }); + async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { + const { executionId, client, interaction } = params; + const logger = this.getLogger('help.js', executionId, interaction); - // TODO: Create interface for command list const commandList = client! .commands!.filter((command: BaseSlashCommandInteraction) => { // don't include system commands @@ -47,18 +31,22 @@ const command: BaseSlashCommandInteraction = { if (option instanceof SlashCommandNumberOption || option instanceof SlashCommandStringOption) { params = `**\`${option.name}\`**` + ' '; } - const beta: string = command.isBeta ? `${embedOptions.icons.beta} ` : ''; - const newCommand: string = command.isNew ? `${embedOptions.icons.new} ` : ''; + const beta: string = command.isBeta ? `${this.embedOptions.icons.beta} ` : ''; + const newCommand: string = command.isNew ? `${this.embedOptions.icons.new} ` : ''; return `- **\`/${command.data.name}\`** ${params}- ${beta}${newCommand}${command.data.description}`; }); const commandListString: string = commandList.join('\n'); - const supportServerString: string = botOptions.serverInviteUrl - ? `${embedOptions.icons.support} **Support server**\nJoin the support server for help or to suggest improvements: \n**${botOptions.serverInviteUrl}**\n\n` + const supportServerString: string = this.botOptions.serverInviteUrl + ? `${this.embedOptions.icons.support} **Support server**\n` + + 'Join the support server for help or to suggest improvements: \n' + + `**${this.botOptions.serverInviteUrl}**\n\n` : ''; - const addBotString: string = botOptions.botInviteUrl - ? `${embedOptions.icons.bot} **Enjoying ${botOptions.name}?**\nAdd me to another server: \n**[Click me!](${botOptions.botInviteUrl})**` + const addBotString: string = this.botOptions.botInviteUrl + ? `${this.embedOptions.icons.bot} **Enjoying ${this.botOptions.name}?**\n` + + 'Add me to another server: \n' + + `**[Click me!](${this.botOptions.botInviteUrl})**` : ''; logger.debug('Responding with info embed.'); @@ -66,15 +54,15 @@ const command: BaseSlashCommandInteraction = { embeds: [ new EmbedBuilder() .setDescription( - `${embedOptions.icons.rule} **List of commands**\n` + + `${this.embedOptions.icons.rule} **List of commands**\n` + `${commandListString}\n\n` + supportServerString + addBotString ) - .setColor(embedOptions.colors.info) + .setColor(this.embedOptions.colors.info) ] }); } -}; +} -export default command; +export default new HelpCommand(); diff --git a/src/interactions/commands/player/test.ts b/src/interactions/commands/player/test.ts index 7bda8b7..4912529 100644 --- a/src/interactions/commands/player/test.ts +++ b/src/interactions/commands/player/test.ts @@ -1,25 +1,27 @@ -import { EmbedBuilder, Message, SlashCommandBuilder } from 'discord.js'; -import { BaseSlashCommandInteraction, BaseSlashCommandParams } from '../../../types/interactionTypes'; +import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; +import { + BaseSlashCommandInteraction, + BaseSlashCommandParams, + BaseSlashCommandReturnType +} from '../../../types/interactionTypes'; -export class TestCommandInteraction extends BaseSlashCommandInteraction { +class TestCommand extends BaseSlashCommandInteraction { constructor() { - const data = new SlashCommandBuilder().setName('test').setDescription('Test command'); + const data = new SlashCommandBuilder() + .setName('test') + .setDescription('This is a test command description lorem ipsum.'); super(data); } - async execute(params: BaseSlashCommandParams): Promise | void> { + async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType { const { executionId, interaction } = params; 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.'); + logger.debug('Responding with success embed.'); return await interaction.editReply({ - embeds: [new EmbedBuilder().setDescription('It works!').setColor(this.embedOptions.colors.success)] + embeds: [new EmbedBuilder().setDescription(`Success reply from command '${this.commandName}'.`).setColor(this.embedOptions.colors.success)] }); } } -export default new TestCommandInteraction(); +export default new TestCommand(); diff --git a/src/types/interactionTypes.ts b/src/types/interactionTypes.ts index 8b0168f..7c19c31 100644 --- a/src/types/interactionTypes.ts +++ b/src/types/interactionTypes.ts @@ -11,7 +11,7 @@ import { import { Logger } from 'pino'; import loggerModule from '../services/logger'; import { ExtendedClient } from './clientTypes'; -import { EmbedOptions } from './configTypes'; +import { BotOptions, EmbedOptions } from './configTypes'; interface BaseInteractionParams { executionId: string; @@ -22,6 +22,8 @@ export interface BaseSlashCommandParams extends BaseInteractionParams { client?: ExtendedClient; } +export type BaseSlashCommandReturnType = Promise | void>; + interface BaseAutocompleteParams extends BaseInteractionParams { interaction: AutocompleteInteraction; } @@ -86,24 +88,28 @@ export abstract class BaseSlashCommandInteraction extends BaseInteraction { isNew: boolean; isBeta: boolean; embedOptions: EmbedOptions; + botOptions: BotOptions; + commandName: string; constructor( data: Omit, - isSystemCommand?: boolean, - isNew?: boolean, - isBeta?: boolean + isSystemCommand: boolean = false, + isNew: boolean = false, + isBeta: boolean = false ) { super(); this.data = data.setDMPermission(false).setNSFW(false); - this.isSystemCommand = isSystemCommand || false; - this.isNew = isNew || false; - this.isBeta = isBeta || false; + this.isSystemCommand = isSystemCommand; + this.isNew = isNew; + this.isBeta = isBeta; this.embedOptions = config.get('embedOptions'); + this.botOptions = config.get('botOptions'); + this.commandName = data.name; } protected getLogger(source: string, executionId: string, interaction: Interaction): Logger { return super.getLoggerBase('slashCommand', source, executionId, interaction); } - abstract execute(params: BaseSlashCommandParams): Promise | void>; + abstract execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType; }