fix: refactored help slash command

This commit is contained in:
Marius Begby committed 2023-09-05 23:12:25 +02:00
1 parent d0e929e284
commit 922337769d
2 files changed
+80 -34

No files matched your search

+79 -33
View File
@@ -1,6 +1,13 @@
import { EmbedBuilder, SlashCommandBuilder, SlashCommandNumberOption, SlashCommandStringOption } from 'discord.js';
import {
Collection,
EmbedBuilder,
SlashCommandBuilder,
SlashCommandNumberOption,
SlashCommandStringOption
} from 'discord.js';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { ExtendedClient } from '../../../types/clientTypes';
class HelpCommand extends BaseSlashCommandInteraction {
constructor() {
@@ -12,37 +19,11 @@ class HelpCommand extends BaseSlashCommandInteraction {
const { executionId, client, interaction } = params;
const logger = this.getLogger(this.name, executionId, interaction);
const commandList = client!
.slashCommandInteractions!.filter((command: BaseSlashCommandInteraction) => {
// don't include system commands
if (command.isSystemCommand) {
return false;
}
return true;
})
.map((command: BaseSlashCommandInteraction) => {
let params: string = '';
const option = command.data.options[0];
if (option instanceof SlashCommandNumberOption || option instanceof SlashCommandStringOption) {
params = `**\`${option.name}\`**` + ' ';
}
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 = 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 = this.botOptions.botInviteUrl
? `${this.embedOptions.icons.bot} **Enjoying ${this.botOptions.name}?**\n` +
'Add me to another server: \n' +
`**[Click me!](${this.botOptions.botInviteUrl})**`
: '';
const [commandEmbedString, supportServerString, addBotString] = await Promise.all([
this.getCommandEmbedString(client!),
this.getSupportServerString(),
this.getAddBotString()
]);
logger.debug('Responding with info embed.');
return await interaction.editReply({
@@ -50,7 +31,7 @@ class HelpCommand extends BaseSlashCommandInteraction {
new EmbedBuilder()
.setDescription(
`${this.embedOptions.icons.rule} **List of commands**\n` +
`${commandListString}\n\n` +
commandEmbedString +
supportServerString +
addBotString
)
@@ -58,6 +39,71 @@ class HelpCommand extends BaseSlashCommandInteraction {
]
});
}
private getNonSystemCommands(client: ExtendedClient): Collection<string, BaseSlashCommandInteraction> {
const clientSlashCommands = client.slashCommandInteractions;
const nonSystemCommands = clientSlashCommands?.filter((command) => !command.isSystemCommand);
if (!nonSystemCommands) {
throw new Error('No non-system commands found.');
}
return nonSystemCommands;
}
private async getCommandEmbedString(client: ExtendedClient): Promise<string> {
const commandCollection = this.getNonSystemCommands(client!);
const commandStringList = commandCollection.map((command: BaseSlashCommandInteraction) => {
return this.getCommandString(command);
});
const commandString = commandStringList.join('\n') + '\n';
return commandString;
}
private getCommandString(command: BaseSlashCommandInteraction): string {
const commandParams: string = this.getCommandParams(command);
const beta: string = command.isBeta ? `${this.embedOptions.icons.beta} ` : '';
const newCommand: string = command.isNew ? `${this.embedOptions.icons.new} ` : '';
return `- **\`/${command.data.name}\`** ${commandParams}- ${beta}${newCommand}${command.data.description}`;
}
private getCommandParams(command: BaseSlashCommandInteraction): string {
const option = command.data.options[0];
if (option instanceof SlashCommandNumberOption || option instanceof SlashCommandStringOption) {
return `**\`${option.name}\`** `;
}
return '';
}
private async getSupportServerString(): Promise<string> {
if (!this.botOptions.serverInviteUrl) {
return '';
}
const embedString = `
${this.embedOptions.icons.support} **Support server**
Join the support server for help or to suggest improvements:
**${this.botOptions.serverInviteUrl}**
`;
return embedString;
}
private async getAddBotString(): Promise<string> {
if (!this.botOptions.botInviteUrl) {
return '';
}
const embedString = `
${this.embedOptions.icons.bot} **Enjoying ${this.botOptions.name}?**
Add me to another server:
**[Click me!](${this.botOptions.botInviteUrl})**
`;
return embedString;
}
}
export default new HelpCommand();
+1 -1
View File
@@ -45,7 +45,7 @@ class LeaveCommand extends BaseSlashCommandInteraction {
});
}
protected async deleteQueue(queue: GuildQueue): Promise<void> {
private async deleteQueue(queue: GuildQueue): Promise<void> {
if (!queue.deleted) {
queue.delete();
}