fix: use stricter localizaiton

This commit is contained in:
tresabhi committed 2024-12-19 11:04:48 -06:00
1 parent 768c420ae4
commit f396d6c4a1
16 files changed
+101 -36

No files matched your search

+5 -1
View File
@@ -24,7 +24,11 @@ export const inactiveCommand = new Promise<CommandRegistry>((resolve) => {
return option return option
.setName('threshold') .setName('threshold')
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.commands.inactive.options.threshold'), localizationObject(
'bot.commands.inactive.options.threshold',
undefined,
true,
),
) )
.setDescription( .setDescription(
translate('bot.commands.inactive.options.threshold.description'), translate('bot.commands.inactive.options.threshold.description'),
@@ -63,6 +63,8 @@ export const ratingLeaderboardCommand = new Promise<CommandRegistry>(
.setNameLocalizations( .setNameLocalizations(
localizationObject( localizationObject(
'bot.commands.rating_leaderboard.options.limit', 'bot.commands.rating_leaderboard.options.limit',
undefined,
true,
), ),
) )
.setDescription( .setDescription(
@@ -87,6 +89,8 @@ export const ratingLeaderboardCommand = new Promise<CommandRegistry>(
.setNameLocalizations( .setNameLocalizations(
localizationObject( localizationObject(
'bot.commands.rating_leaderboard.options.season', 'bot.commands.rating_leaderboard.options.season',
undefined,
true,
), ),
) )
.setDescription( .setDescription(
@@ -148,6 +152,8 @@ export const ratingLeaderboardCommand = new Promise<CommandRegistry>(
.setNameLocalizations( .setNameLocalizations(
localizationObject( localizationObject(
'bot.commands.rating_leaderboard.options.league', 'bot.commands.rating_leaderboard.options.league',
undefined,
true,
), ),
) )
.setDescription( .setDescription(
+10 -2
View File
@@ -36,7 +36,11 @@ export const researchCommand = new Promise<CommandRegistry>((resolve) => {
option option
.setName(t`bot.commands.research.options.target_tank`) .setName(t`bot.commands.research.options.target_tank`)
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.commands.research.options.target_tank'), localizationObject(
'bot.commands.research.options.target_tank',
undefined,
true,
),
) )
.setDescription( .setDescription(
t`bot.commands.research.options.target_tank.description`, t`bot.commands.research.options.target_tank.description`,
@@ -53,7 +57,11 @@ export const researchCommand = new Promise<CommandRegistry>((resolve) => {
option option
.setName(t`bot.commands.research.options.starting_tank`) .setName(t`bot.commands.research.options.starting_tank`)
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.commands.research.options.starting_tank'), localizationObject(
'bot.commands.research.options.starting_tank',
undefined,
true,
),
) )
.setDescription( .setDescription(
t`bot.commands.research.options.starting_tank.description`, t`bot.commands.research.options.starting_tank.description`,
+5 -1
View File
@@ -24,7 +24,11 @@ export const searchClansCommand = new Promise<CommandRegistry>((resolve) => {
return option return option
.setName(t`bot.commands.search_clans.options.limit`) .setName(t`bot.commands.search_clans.options.limit`)
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.commands.search_clans.options.limit'), localizationObject(
'bot.commands.search_clans.options.limit',
undefined,
true,
),
) )
.setDescription( .setDescription(
translate('bot.commands.search_clans.options.limit.description', [ translate('bot.commands.search_clans.options.limit.description', [
+5 -1
View File
@@ -29,7 +29,11 @@ export const searchPlayersCommand = new Promise<CommandRegistry>((resolve) => {
option option
.setName(t`bot.commands.search_players.options.limit`) .setName(t`bot.commands.search_players.options.limit`)
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.commands.search_players.options.limit'), localizationObject(
'bot.commands.search_players.options.limit',
undefined,
true,
),
) )
.setDescription( .setDescription(
translate('bot.commands.search_players.options.limit.description', [ translate('bot.commands.search_players.options.limit.description', [
@@ -7,7 +7,9 @@ export function addClanChoices(option: SlashCommandStringOption) {
return option return option
.setName('clan') .setName('clan')
.setNameLocalizations(localizationObject('bot.common.options.clan')) .setNameLocalizations(
localizationObject('bot.common.options.clan', undefined, true),
)
.setDescription(translate('bot.common.options.clan.description')) .setDescription(translate('bot.common.options.clan.description'))
.setDescriptionLocalizations( .setDescriptionLocalizations(
localizationObject('bot.common.options.clan.description'), localizationObject('bot.common.options.clan.description'),
@@ -25,7 +25,11 @@ export function addFilterOptions<
option option
.setName(translate('bot.common.options.filters.nation')) .setName(translate('bot.common.options.filters.nation'))
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.common.options.filters.nation'), localizationObject(
'bot.common.options.filters.nation',
undefined,
true,
),
) )
.setDescription( .setDescription(
translate('bot.common.options.filters.nation.description'), translate('bot.common.options.filters.nation.description'),
@@ -52,7 +56,11 @@ export function addFilterOptions<
option option
.setName(translate('bot.common.options.filters.class')) .setName(translate('bot.common.options.filters.class'))
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.common.options.filters.class'), localizationObject(
'bot.common.options.filters.class',
undefined,
true,
),
) )
.setDescription( .setDescription(
translate('bot.common.options.filters.class.description'), translate('bot.common.options.filters.class.description'),
@@ -78,7 +86,11 @@ export function addFilterOptions<
option option
.setName(translate('bot.common.options.filters.type')) .setName(translate('bot.common.options.filters.type'))
.setNameLocalizations( .setNameLocalizations(
localizationObject('bot.common.options.filters.type'), localizationObject(
'bot.common.options.filters.type',
undefined,
true,
),
) )
.setDescription( .setDescription(
translate('bot.common.options.filters.type.description'), translate('bot.common.options.filters.type.description'),
@@ -18,12 +18,20 @@ export function addPeriodSubCommands(
) { ) {
const { translate } = translator(Locale.EnglishUS); const { translate } = translator(Locale.EnglishUS);
const subcommandNameLocalizations = { const subcommandNameLocalizations = {
today: localizationObject('bot.common.subcommands.today'), today: localizationObject('bot.common.subcommands.today', undefined, true),
30: localizationObject('bot.common.subcommands.30'), 30: localizationObject('bot.common.subcommands.30', undefined, true),
60: localizationObject('bot.common.subcommands.60'), 60: localizationObject('bot.common.subcommands.60', undefined, true),
90: localizationObject('bot.common.subcommands.90'), 90: localizationObject('bot.common.subcommands.90', undefined, true),
career: localizationObject('bot.common.subcommands.career'), career: localizationObject(
custom: localizationObject('bot.common.subcommands.custom'), 'bot.common.subcommands.career',
undefined,
true,
),
custom: localizationObject(
'bot.common.subcommands.custom',
undefined,
true,
),
}; };
const subcommandDescriptionLocalizations = { const subcommandDescriptionLocalizations = {
today: localizationObject('bot.common.subcommands.today.description'), today: localizationObject('bot.common.subcommands.today.description'),
@@ -12,7 +12,9 @@ export function addRegionChoices(option: SlashCommandStringOption) {
return option return option
.setName(t`bot.common.options.region`) .setName(t`bot.common.options.region`)
.setNameLocalizations(localizationObject('bot.common.options.region')) .setNameLocalizations(
localizationObject('bot.common.options.region', undefined, true),
)
.setDescription(t`bot.common.options.region.description`) .setDescription(t`bot.common.options.region.description`)
.setDescriptionLocalizations( .setDescriptionLocalizations(
localizationObject('bot.common.options.region.description'), localizationObject('bot.common.options.region.description'),
@@ -7,7 +7,9 @@ export function addTankChoices(option: SlashCommandStringOption) {
return option return option
.setName(translate('bot.common.options.tank')) .setName(translate('bot.common.options.tank'))
.setNameLocalizations(localizationObject('bot.common.options.tank')) .setNameLocalizations(
localizationObject('bot.common.options.tank', undefined, true),
)
.setDescription(translate('bot.common.options.tank.description')) .setDescription(translate('bot.common.options.tank.description'))
.setDescriptionLocalizations( .setDescriptionLocalizations(
localizationObject('bot.common.options.tank.description'), localizationObject('bot.common.options.tank.description'),
@@ -13,7 +13,9 @@ export function addTierChoices(option: SlashCommandStringOption) {
return option return option
.setName(t`bot.common.options.tier`) .setName(t`bot.common.options.tier`)
.setNameLocalizations(localizationObject('bot.common.options.tier')) .setNameLocalizations(
localizationObject('bot.common.options.tier', undefined, true),
)
.setDescription(t`bot.common.options.tier.description`) .setDescription(t`bot.common.options.tier.description`)
.setDescriptionLocalizations( .setDescriptionLocalizations(
localizationObject('bot.common.options.tier.description'), localizationObject('bot.common.options.tier.description'),
@@ -7,7 +7,9 @@ export function addUsernameChoices(option: SlashCommandStringOption) {
return option return option
.setName(t`bot.common.options.username`) .setName(t`bot.common.options.username`)
.setNameLocalizations(localizationObject('bot.common.options.username')) .setNameLocalizations(
localizationObject('bot.common.options.username', undefined, true),
)
.setDescription(t`bot.common.options.username.description`) .setDescription(t`bot.common.options.username.description`)
.setDescriptionLocalizations( .setDescriptionLocalizations(
localizationObject('bot.common.options.username.description'), localizationObject('bot.common.options.username.description'),
@@ -27,7 +27,9 @@ export function createLocalizedCommand(
const { translate } = translator(Locale.EnglishUS); const { translate } = translator(Locale.EnglishUS);
const slashCommand = new SlashCommandBuilder() const slashCommand = new SlashCommandBuilder()
.setName(command) .setName(command)
.setNameLocalizations(localizationObject(`bot.commands.${commandPathItem}`)) .setNameLocalizations(
localizationObject(`bot.commands.${commandPathItem}`, undefined, true),
)
.setDescription(translate(`bot.commands.${commandPathItem}.description`)) .setDescription(translate(`bot.commands.${commandPathItem}.description`))
.setDescriptionLocalizations( .setDescriptionLocalizations(
localizationObject(`bot.commands.${commandPathItem}.description`), localizationObject(`bot.commands.${commandPathItem}.description`),
@@ -38,6 +40,8 @@ export function createLocalizedCommand(
if ('subcommand' in subItem) { if ('subcommand' in subItem) {
const subcommandNameLocalizations = localizationObject( const subcommandNameLocalizations = localizationObject(
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}`, `bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}`,
undefined,
true,
); );
const subcommandDescriptionLocalizations = localizationObject( const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}.description`, `bot.commands.${commandPathItem}.subcommands.${subItem.subcommand}.description`,
@@ -59,6 +63,8 @@ export function createLocalizedCommand(
} else { } else {
const subcommandNameLocalizations = localizationObject( const subcommandNameLocalizations = localizationObject(
`bot.commands.${commandPathItem}.groups.${subItem.group}`, `bot.commands.${commandPathItem}.groups.${subItem.group}`,
undefined,
true,
); );
const subcommandDescriptionLocalizations = localizationObject( const subcommandDescriptionLocalizations = localizationObject(
`bot.commands.${commandPathItem}.groups.${subItem.group}.description`, `bot.commands.${commandPathItem}.groups.${subItem.group}.description`,
@@ -2,13 +2,29 @@ import { Locale } from 'discord.js';
import { SUPPORTED_LOCALES } from '../localization/strings/constants'; import { SUPPORTED_LOCALES } from '../localization/strings/constants';
import { translator } from '../localization/translator'; import { translator } from '../localization/translator';
export function localizationObject(path: string, literals?: string[]) { const validNameRegex = /^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u;
export function localizationObject(
path: string,
literals?: string[],
strict = false,
) {
return SUPPORTED_LOCALES.reduce<Partial<Record<Locale, string>>>( return SUPPORTED_LOCALES.reduce<Partial<Record<Locale, string>>>(
(localizations, locale) => { (localizations, locale) => {
const { translate } = translator(locale); const { translate } = translator(locale);
const translation = translate(path, literals);
const isValid = !strict || validNameRegex.test(translation);
if (!isValid) {
console.warn(
`Invalid localization for ${locale} (${path}): ${translation}; skipping...`,
);
return localizations;
}
return { return {
...localizations, ...localizations,
[locale]: translate(path, literals), [locale]: translation,
}; };
}, },
{}, {},
@@ -126,9 +126,7 @@ commands.then((awaitedCommands) => {
rest.put( rest.put(
Routes.applicationCommands(assertSecret(import.meta.env.DISCORD_CLIENT_ID)), Routes.applicationCommands(assertSecret(import.meta.env.DISCORD_CLIENT_ID)),
{ { body },
body,
},
); );
}); });
-11
View File
@@ -7,19 +7,8 @@ import { manager } from './core/discord/manager';
console.log('pre manager event listeners and spawn'); console.log('pre manager event listeners and spawn');
let isFirst = true;
const interval = setInterval(() => {
if (!isFirst) {
console.warn('respawning all shards because manager died');
manager.respawnAll();
}
isFirst = false;
}, 5000);
const shards = await manager const shards = await manager
.on('shardCreate', (shard) => { .on('shardCreate', (shard) => {
clearInterval(interval);
console.log(`🟡 Launching shard ${shard.id}`); console.log(`🟡 Launching shard ${shard.id}`);
shard.on('ready', () => console.log(`🟢 Launched shard ${shard.id}`)); shard.on('ready', () => console.log(`🟢 Launched shard ${shard.id}`));
}) })