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 {
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();
+14 -12
View File
@@ -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<Message<boolean> | 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();
+14 -8
View File
@@ -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<Message<boolean> | 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<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>,
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<Message<boolean> | void>;
abstract execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType;
}