WIP few changes to class

This commit is contained in:
Marius Begby committed 2023-08-31 11:03:04 +02:00
1 parent 6d7a931f16
commit 335f4bb84e
3 files changed
+57 -61

No files matched your search

+29 -41
View File
@@ -1,38 +1,22 @@
import config from 'config'; import { EmbedBuilder, SlashCommandBuilder, SlashCommandNumberOption, SlashCommandStringOption } from 'discord.js';
import { import {
EmbedBuilder, BaseSlashCommandInteraction,
SharedSlashCommandOptions, BaseSlashCommandParams,
SlashCommandBuilder, BaseSlashCommandReturnType
SlashCommandNumberOption, } from '../../../types/interactionTypes';
SlashCommandStringOption
} from 'discord.js';
import { Logger } from 'pino';
import loggerModule from '../../../services/logger';
import { BotOptions, EmbedOptions } from '../../../types/configTypes';
import { BaseSlashCommandInteraction } from '../../../types/interactionTypes';
const embedOptions: EmbedOptions = config.get('embedOptions'); class HelpCommand extends BaseSlashCommandInteraction {
const botOptions: BotOptions = config.get('botOptions'); constructor() {
const data = new SlashCommandBuilder()
const command: BaseSlashCommandInteraction = {
isNew: false,
isBeta: false,
data: new SlashCommandBuilder()
.setName('help') .setName('help')
.setDescription('Show a list of commands and their usage.') .setDescription('Show a list of commands and their usage.');
.setDMPermission(false) super(data);
.setNSFW(false), }
execute: async ({ interaction, client, executionId }) => {
const logger: Logger = loggerModule.child({ async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType {
source: 'help.js', const { executionId, client, interaction } = params;
module: 'slashCommand', const logger = this.getLogger('help.js', executionId, interaction);
name: '/help',
executionId: executionId,
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
// TODO: Create interface for command list
const commandList = client! const commandList = client!
.commands!.filter((command: BaseSlashCommandInteraction) => { .commands!.filter((command: BaseSlashCommandInteraction) => {
// don't include system commands // don't include system commands
@@ -47,18 +31,22 @@ const command: BaseSlashCommandInteraction = {
if (option instanceof SlashCommandNumberOption || option instanceof SlashCommandStringOption) { if (option instanceof SlashCommandNumberOption || option instanceof SlashCommandStringOption) {
params = `**\`${option.name}\`**` + ' '; params = `**\`${option.name}\`**` + ' ';
} }
const beta: string = command.isBeta ? `${embedOptions.icons.beta} ` : ''; const beta: string = command.isBeta ? `${this.embedOptions.icons.beta} ` : '';
const newCommand: string = command.isNew ? `${embedOptions.icons.new} ` : ''; const newCommand: string = command.isNew ? `${this.embedOptions.icons.new} ` : '';
return `- **\`/${command.data.name}\`** ${params}- ${beta}${newCommand}${command.data.description}`; return `- **\`/${command.data.name}\`** ${params}- ${beta}${newCommand}${command.data.description}`;
}); });
const commandListString: string = commandList.join('\n'); const commandListString: string = commandList.join('\n');
const supportServerString: string = botOptions.serverInviteUrl const supportServerString: string = this.botOptions.serverInviteUrl
? `${embedOptions.icons.support} **Support server**\nJoin the support server for help or to suggest improvements: \n**${botOptions.serverInviteUrl}**\n\n` ? `${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 const addBotString: string = this.botOptions.botInviteUrl
? `${embedOptions.icons.bot} **Enjoying ${botOptions.name}?**\nAdd me to another server: \n**[Click me!](${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.'); logger.debug('Responding with info embed.');
@@ -66,15 +54,15 @@ const command: BaseSlashCommandInteraction = {
embeds: [ embeds: [
new EmbedBuilder() new EmbedBuilder()
.setDescription( .setDescription(
`${embedOptions.icons.rule} **List of commands**\n` + `${this.embedOptions.icons.rule} **List of commands**\n` +
`${commandListString}\n\n` + `${commandListString}\n\n` +
supportServerString + supportServerString +
addBotString addBotString
) )
.setColor(embedOptions.colors.info) .setColor(this.embedOptions.colors.info)
] ]
}); });
} }
}; }
export default command; export default new HelpCommand();
+14 -12
View File
@@ -1,25 +1,27 @@
import { EmbedBuilder, Message, SlashCommandBuilder } from 'discord.js'; import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { BaseSlashCommandInteraction, BaseSlashCommandParams } from '../../../types/interactionTypes'; import {
BaseSlashCommandInteraction,
BaseSlashCommandParams,
BaseSlashCommandReturnType
} from '../../../types/interactionTypes';
export class TestCommandInteraction extends BaseSlashCommandInteraction { class TestCommand extends BaseSlashCommandInteraction {
constructor() { 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); super(data);
} }
async execute(params: BaseSlashCommandParams): Promise<Message<boolean> | void> { async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType {
const { executionId, interaction } = params; const { executionId, interaction } = params;
const logger = this.getLogger('test.js', executionId, interaction); const logger = this.getLogger('test.js', executionId, interaction);
logger.info('test command executed'); logger.debug('Responding with success embed.');
logger.info(`${interaction.user.tag} executed test command`);
logger.info(`Execution ID: ${executionId}`);
logger.info(this.embedOptions.icons.success + ' Icon success from config.');
return await interaction.editReply({ 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();
+14 -8
View File
@@ -11,7 +11,7 @@ import {
import { Logger } from 'pino'; import { Logger } from 'pino';
import loggerModule from '../services/logger'; import loggerModule from '../services/logger';
import { ExtendedClient } from './clientTypes'; import { ExtendedClient } from './clientTypes';
import { EmbedOptions } from './configTypes'; import { BotOptions, EmbedOptions } from './configTypes';
interface BaseInteractionParams { interface BaseInteractionParams {
executionId: string; executionId: string;
@@ -22,6 +22,8 @@ export interface BaseSlashCommandParams extends BaseInteractionParams {
client?: ExtendedClient; client?: ExtendedClient;
} }
export type BaseSlashCommandReturnType = Promise<Message<boolean> | void>;
interface BaseAutocompleteParams extends BaseInteractionParams { interface BaseAutocompleteParams extends BaseInteractionParams {
interaction: AutocompleteInteraction; interaction: AutocompleteInteraction;
} }
@@ -86,24 +88,28 @@ export abstract class BaseSlashCommandInteraction extends BaseInteraction {
isNew: boolean; isNew: boolean;
isBeta: boolean; isBeta: boolean;
embedOptions: EmbedOptions; embedOptions: EmbedOptions;
botOptions: BotOptions;
commandName: string;
constructor( constructor(
data: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>, data: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>,
isSystemCommand?: boolean, isSystemCommand: boolean = false,
isNew?: boolean, isNew: boolean = false,
isBeta?: boolean isBeta: boolean = false
) { ) {
super(); super();
this.data = data.setDMPermission(false).setNSFW(false); this.data = data.setDMPermission(false).setNSFW(false);
this.isSystemCommand = isSystemCommand || false; this.isSystemCommand = isSystemCommand;
this.isNew = isNew || false; this.isNew = isNew;
this.isBeta = isBeta || false; this.isBeta = isBeta;
this.embedOptions = config.get('embedOptions'); this.embedOptions = config.get('embedOptions');
this.botOptions = config.get('botOptions');
this.commandName = data.name;
} }
protected getLogger(source: string, executionId: string, interaction: Interaction): Logger { protected getLogger(source: string, executionId: string, interaction: Interaction): Logger {
return super.getLoggerBase('slashCommand', source, executionId, interaction); return super.getLoggerBase('slashCommand', source, executionId, interaction);
} }
abstract execute(params: BaseSlashCommandParams): Promise<Message<boolean> | void>; abstract execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType;
} }