From 8703be8cd9176de1bfd2bda298e2f3fe84913460 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 1 Nov 2024 21:16:37 +0100 Subject: [PATCH] aux function for retrieving correct message according to locales --- src/commands/misc/help.ts | 23 +++++++++++++---------- src/utils/commands/help.ts | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 src/utils/commands/help.ts diff --git a/src/commands/misc/help.ts b/src/commands/misc/help.ts index ddd4b0f..6806d6f 100644 --- a/src/commands/misc/help.ts +++ b/src/commands/misc/help.ts @@ -9,6 +9,7 @@ import { import "../../modules/string"; import { getLocale, getLocalizations } from "../../utils/locales"; import { getFilename } from "../../utils/misc"; +import { goodDescription, goodName } from "../../utils/commands/help"; export default { scope: () => [], @@ -63,12 +64,16 @@ export default { // Add subcommands const all_commands: string[] = []; commands_name.forEach((command) => { - const json = client.commands.list.get(command)?.data.toJSON(); - all_commands.push(command); + const data = client.commands.list.get(command)?.data; + const name = goodName(data!, interaction.locale); + all_commands.push(name); - json?.options - ?.filter((option) => option.type === ApplicationCommandOptionType.Subcommand) - .forEach((subcommand) => all_commands.push(command + " " + subcommand.name)); + data + ?.toJSON() + .options?.filter((option) => option.type === ApplicationCommandOptionType.Subcommand) + .forEach((subcommand) => + all_commands.push(name + " " + goodName(subcommand, interaction.locale)), + ); }); const commands = all_commands.reduce( @@ -111,12 +116,10 @@ export default { embeds: [ new EmbedBuilder() .setColor(Colors.Blurple) - .setTitle("`/" + command.data.name + "`") + .setTitle("`/" + goodName(command.data, interaction.locale) + "`") .setDescription( - // Loads the description - // according to the user's locals - command.data.description_localizations?.[interaction.locale] ?? - command.data.description, + // Loads the description according to the user's locals + goodDescription(command.data, interaction.locale), ), ], }); diff --git a/src/utils/commands/help.ts b/src/utils/commands/help.ts new file mode 100644 index 0000000..2ff72c9 --- /dev/null +++ b/src/utils/commands/help.ts @@ -0,0 +1,23 @@ +import { APIApplicationCommandSubcommandOption, Locale, SlashCommandBuilder } from "discord.js"; + +/** + * Find the name of the command, trying to get the correct locale + * @param data Command data + * @param locale Locale wanted + * @returns Command's name + */ +export const goodName = ( + data: SlashCommandBuilder | APIApplicationCommandSubcommandOption, + locale: Locale, +) => data.name_localizations?.[locale] ?? data.name; + +/** + * Find the description of the command, trying to get the correct locale + * @param data Command data + * @param locale Locale wanted + * @returns Command's description + */ +export const goodDescription = ( + data: SlashCommandBuilder | APIApplicationCommandSubcommandOption, + locale: Locale, +) => data.description_localizations?.[locale] ?? data.description;