Skip to content

Commit

Permalink
fix: command localizing
Browse files Browse the repository at this point in the history
  • Loading branch information
espimarisa committed May 5, 2024
1 parent 77376ee commit e99fd01
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
55 changes: 55 additions & 0 deletions packages/bot/src/commands/xkcd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { HibikiCommand, type HibikiCommandOptions } from "$classes/Command.ts";
import { HibikiColors } from "$shared/constants.ts";
import { t } from "$shared/i18n.ts";
import { ApplicationCommandOptionType } from "discord-api-types/v10";
import type { ChatInputCommandInteraction } from "discord.js";

export class XKCDCommand extends HibikiCommand {
options = [
{
// The XKCD number to get
type: ApplicationCommandOptionType.Integer,
required: false,
},
] satisfies HibikiCommandOptions[];

async runCommand(interaction: ChatInputCommandInteraction) {
// Fetches the default/daily comic
const res = await fetch("https://xkcd.com/info.0.json");
const body = await res?.json();

if (!(res && body)) {
await interaction.followUp({
embeds: [
{
title: t("errors:ERROR", { lng: interaction.locale }),
description: t("errors:ERROR_IMAGE", { lng: interaction.locale }),
color: HibikiColors.ERROR,
footer: {
text: t("errors:ERROR_FOUND_A_BUG", { lng: interaction.locale }),
icon_url: this.bot.user?.displayAvatarURL(),
},
},
],
});

return;
}

console.table(body);

// Sends the image
await interaction.followUp({
embeds: [
{
title: t("errors:ERROR"),
color: HibikiColors.GENERAL,
footer: {
text: "dwqmkokewf",
icon_url: this.bot.user?.displayAvatarURL(),
},
},
],
});
}
}
14 changes: 8 additions & 6 deletions packages/bot/src/utils/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ export async function generateInteractionRESTData(bot: HibikiClient) {

for (const command of bot.commands.values()) {
// Gets a list of all command name and description localizations
const commandNameString = `COMMAND_${command.name.toUpperCase()}_NAME` as keyof typeof commands;
const commandDescriptionString = `COMMAND_${command.name.toUpperCase()}_DESCRIPTION` as keyof typeof commands;
const commandNameString = `commands:COMMAND_${command.name.toUpperCase()}_NAME` as keyof typeof commands;
const commandDescriptionString =
`commands:COMMAND_${command.name.toUpperCase()}_DESCRIPTION` as keyof typeof commands;
const localizedCommandNames = await getLocalizationsForKey(commandNameString, true);
const localizedCommandDescriptions = await getLocalizationsForKey(commandDescriptionString);

Expand All @@ -160,9 +161,10 @@ export async function generateInteractionRESTData(bot: HibikiClient) {
// Localizes options
for (const [index, option] of (command.options as APIApplicationCommandOption[])?.entries() ?? []) {
// Gets a list of all option name and option description localizations
const optionNameString = `COMMAND_${command.name.toUpperCase()}_OPTION_${index}_NAME` as keyof typeof commands;
const optionNameString =
`commands:COMMAND_${command.name.toUpperCase()}_OPTION_${index}_NAME` as keyof typeof commands;
const optionDescString =
`COMMAND_${command.name.toUpperCase()}_OPTION_${index}_DESCRIPTION` as keyof typeof commands;
`commands:COMMAND_${command.name.toUpperCase()}_OPTION_${index}_DESCRIPTION` as keyof typeof commands;

const localizedOptionNames = await getLocalizationsForKey(optionNameString, true, optionRegex);
const localizedOptionDescriptions = await getLocalizationsForKey(optionDescString, false, optionRegex);
Expand Down Expand Up @@ -191,10 +193,10 @@ export async function generateInteractionRESTData(bot: HibikiClient) {
if (option.type === ApplicationCommandOptionType.SubcommandGroup) {
for (const [optIndex, subOpt] of option.options?.entries() ?? []) {
const subOptNameString =
`COMMAND_${command.name.toUpperCase()}_SUBCOMMAND_${index}_OPTION_${optIndex}_NAME` as keyof typeof commands;
`commands:COMMAND_${command.name.toUpperCase()}_SUBCOMMAND_${index}_OPTION_${optIndex}_NAME` as keyof typeof commands;

const subOptDescString =
`COMMAND_${command.name.toUpperCase()}_SUBCOMMAND_${index}_OPTION_${optIndex}_DESCRIPTION` as keyof typeof commands;
`commands:COMMAND_${command.name.toUpperCase()}_SUBCOMMAND_${index}_OPTION_${optIndex}_DESCRIPTION` as keyof typeof commands;

// Generates localizations for subcommand option names
const localizedOptNames = await getLocalizationsForKey(subOptNameString, true, subCommandOptionNameRegex);
Expand Down

0 comments on commit e99fd01

Please sign in to comment.