Files
blitzkit/src/core/discord/createLocalizedCommand.ts
T

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-03-06 15:34:10 -06:00
import {
Locale,
SlashCommandBuilder,
SlashCommandSubcommandBuilder,
SlashCommandSubcommandGroupBuilder,
} from 'discord.js';
2024-03-05 09:59:46 -06:00
import { translator } from '../localization/translator';
import { localizationObject } from './localizationObject';
interface CreateLocalizedCommandSubcommand {
subcommand: string;
2024-03-06 15:34:10 -06:00
modify?: (option: SlashCommandSubcommandBuilder) => void;
}
interface CreateLocalizedCommandSubcommandGroup {
group: string;
modify?: (option: SlashCommandSubcommandGroupBuilder) => void;
2024-03-05 09:59:46 -06:00
}
2024-03-05 10:51:14 -06:00
export function createLocalizedCommand(
2024-03-05 09:59:46 -06:00
command: string,
2024-03-06 15:34:10 -06:00
sub?:
| CreateLocalizedCommandSubcommand[]
| CreateLocalizedCommandSubcommandGroup[],
2024-03-05 09:59:46 -06:00
) {
2024-03-07 13:49:38 -06:00
const commandPathItem = command.replaceAll('-', '_');
2024-03-05 10:51:14 -06:00
const { translate } = translator(Locale.EnglishUS);
2024-03-05 09:59:46 -06:00
const slashCommand = new SlashCommandBuilder()
.setName(command)
2024-03-07 13:49:38 -06:00
.setNameLocalizations(localizationObject(`bot.commands.${commandPathItem}`))
.setDescription(translate(`bot.commands.${commandPathItem}.description`))
2024-03-05 09:59:46 -06:00
.setDescriptionLocalizations(
2024-03-07 13:49:38 -06:00
localizationObject(`bot.commands.${commandPathItem}.description`),
2024-03-05 09:59:46 -06:00
);
2024-03-06 15:34:10 -06:00
if (sub) {
sub.map((subItem) => {
if ('subcommand' in subItem) {
const subcommandNameLocalizations = localizationObject(
2024-03-07 13:49:38 -06:00
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}`,
2024-03-06 15:34:10 -06:00
);
const subcommandDescriptionLocalizations = localizationObject(
2024-03-07 13:49:38 -06:00
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}.description`,
2024-03-06 15:34:10 -06:00
);
2024-03-05 09:59:46 -06:00
2024-03-06 15:34:10 -06:00
slashCommand.addSubcommand((option) => {
if (subItem.modify) subItem.modify(option);
return option
.setName(subItem.subcommand)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
2024-03-07 13:49:38 -06:00
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}.description`,
2024-03-06 15:34:10 -06:00
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations);
});
} else {
const subcommandNameLocalizations = localizationObject(
2024-03-07 13:49:38 -06:00
`bot.commands.${commandPathItem}.groups.${subItem.group}`,
2024-03-06 15:34:10 -06:00
);
const subcommandDescriptionLocalizations = localizationObject(
2024-03-07 13:49:38 -06:00
`bot.commands.${commandPathItem}.groups.${subItem.group}.description`,
2024-03-06 15:34:10 -06:00
);
slashCommand.addSubcommandGroup((option) => {
if (subItem.modify) subItem.modify(option);
return option
.setName(subItem.group)
.setNameLocalizations(subcommandNameLocalizations)
.setDescription(
translate(
2024-03-07 13:49:38 -06:00
`bot.commands.${commandPathItem}.groups.${subItem.group}.description`,
2024-03-06 15:34:10 -06:00
),
)
.setDescriptionLocalizations(subcommandDescriptionLocalizations);
});
}
2024-03-05 10:51:14 -06:00
});
2024-03-05 09:59:46 -06:00
}
return slashCommand;
}