mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 03:29:51 +02:00
feat: random localizations
This commit is contained in:
1 parent
796aca21cf
commit
3325536bbc
17 files changed
+423
-166
No files matched your search
@@ -1,14 +1,33 @@
|
||||
import { SlashCommandStringOption } from 'discord.js';
|
||||
import { REGION_NAMES } from '../../constants/regions';
|
||||
import {
|
||||
APIApplicationCommandOptionChoice,
|
||||
Locale,
|
||||
SlashCommandStringOption,
|
||||
} from 'discord.js';
|
||||
import { REGIONS } from '../../constants/regions';
|
||||
import { translator } from '../localization/translator';
|
||||
import { localizationObject } from './localizationObject';
|
||||
|
||||
export default function addRegionChoices(option: SlashCommandStringOption) {
|
||||
const { t, translate } = translator(Locale.EnglishUS);
|
||||
|
||||
return option
|
||||
.setName('region')
|
||||
.setDescription('The Blitz region you are in')
|
||||
.setName(t`bot.common.options.region`)
|
||||
.setNameLocalizations(localizationObject('bot.common.options.region'))
|
||||
.setDescription(t`bot.common.options.region.description`)
|
||||
.setDescriptionLocalizations(
|
||||
localizationObject('bot.common.options.region.description'),
|
||||
)
|
||||
.addChoices(
|
||||
{ name: REGION_NAMES.com, value: 'com' },
|
||||
{ name: REGION_NAMES.eu, value: 'eu' },
|
||||
{ name: REGION_NAMES.asia, value: 'asia' },
|
||||
...REGIONS.map(
|
||||
(region) =>
|
||||
({
|
||||
value: region,
|
||||
name: translate(`common.regions.normal.${region}`),
|
||||
name_localizations: localizationObject(
|
||||
`common.regions.normal.${region}`,
|
||||
),
|
||||
}) satisfies APIApplicationCommandOptionChoice,
|
||||
),
|
||||
)
|
||||
.setRequired(true);
|
||||
}
|
||||
@@ -3,8 +3,8 @@ import {
|
||||
AutocompleteInteraction,
|
||||
CacheType,
|
||||
} from 'discord.js';
|
||||
import { REGION_NAMES_SHORT } from '../../constants/regions';
|
||||
import searchClansAcrossRegions from '../blitz/searchClansAcrossRegions';
|
||||
import { translator } from '../localization/translator';
|
||||
|
||||
export const DISCORD_CHOICES_MAX_NAME_SIZE = 25;
|
||||
export const OVERFLOW_SUFFIX = '…';
|
||||
@@ -12,6 +12,7 @@ export const OVERFLOW_SUFFIX = '…';
|
||||
export default async function autocompleteClan(
|
||||
interaction: AutocompleteInteraction<CacheType>,
|
||||
) {
|
||||
const { translate } = translator(interaction.locale);
|
||||
const focusedOption = interaction.options.getFocused(true);
|
||||
if (focusedOption.name !== 'clan') return;
|
||||
if (focusedOption.value.length < 2) return interaction.respond([]);
|
||||
@@ -20,7 +21,9 @@ export default async function autocompleteClan(
|
||||
await interaction.respond(
|
||||
clans
|
||||
? clans.map((clan) => {
|
||||
let name = `${clan.name} (${REGION_NAMES_SHORT[clan.region]})`;
|
||||
let name = `${clan.name} (${translate(
|
||||
`common.regions.short.${clan.region}`,
|
||||
)})`;
|
||||
|
||||
if (name.length > DISCORD_CHOICES_MAX_NAME_SIZE) {
|
||||
const overSize = name.length - DISCORD_CHOICES_MAX_NAME_SIZE;
|
||||
@@ -28,7 +31,9 @@ export default async function autocompleteClan(
|
||||
name = `${clan.name.slice(
|
||||
0,
|
||||
clan.name.length - overSize - OVERFLOW_SUFFIX.length,
|
||||
)}${OVERFLOW_SUFFIX} (${REGION_NAMES_SHORT[clan.region]})`;
|
||||
)}${OVERFLOW_SUFFIX} (${translate(
|
||||
`common.regions.short.${clan.region}`,
|
||||
)})`;
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -3,8 +3,8 @@ import {
|
||||
AutocompleteInteraction,
|
||||
CacheType,
|
||||
} from 'discord.js';
|
||||
import { REGION_NAMES_SHORT } from '../../constants/regions';
|
||||
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
|
||||
import { translator } from '../localization/translator';
|
||||
import {
|
||||
DISCORD_CHOICES_MAX_NAME_SIZE,
|
||||
OVERFLOW_SUFFIX,
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
export default async function autocompleteUsername(
|
||||
interaction: AutocompleteInteraction<CacheType>,
|
||||
) {
|
||||
const { translate } = translator(interaction.locale);
|
||||
const focusedOption = interaction.options.getFocused(true);
|
||||
if (focusedOption.name !== 'username') return;
|
||||
const players = await searchPlayersAcrossRegions(focusedOption.value);
|
||||
@@ -21,9 +22,9 @@ export default async function autocompleteUsername(
|
||||
await interaction.respond(
|
||||
players
|
||||
? players.map((player) => {
|
||||
let name = `${player.nickname} (${
|
||||
REGION_NAMES_SHORT[player.region]
|
||||
})`;
|
||||
let name = `${player.nickname} (${translate(
|
||||
`common.regions.short.${player.region}`,
|
||||
)})`;
|
||||
|
||||
if (name.length > DISCORD_CHOICES_MAX_NAME_SIZE) {
|
||||
const overSize = name.length - DISCORD_CHOICES_MAX_NAME_SIZE;
|
||||
@@ -31,7 +32,9 @@ export default async function autocompleteUsername(
|
||||
name = `${player.nickname.slice(
|
||||
0,
|
||||
player.nickname.length - overSize - OVERFLOW_SUFFIX.length,
|
||||
)}${OVERFLOW_SUFFIX} (${REGION_NAMES_SHORT[player.region]})`;
|
||||
)}${OVERFLOW_SUFFIX} (${translate(
|
||||
`common.regions.short.${player.region}`,
|
||||
)})`;
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -2,13 +2,13 @@ import { Locale } from 'discord.js';
|
||||
import { locales } from '../localization/strings';
|
||||
import { translator } from '../localization/translator';
|
||||
|
||||
export function localizationObject(path: string) {
|
||||
export function localizationObject(path: string, literals?: string[]) {
|
||||
return locales.reduce<Partial<Record<Locale, string>>>(
|
||||
(localizations, locale) => {
|
||||
const { translate } = translator(locale);
|
||||
return {
|
||||
...localizations,
|
||||
[locale]: translate(path),
|
||||
[locale]: translate(path, literals),
|
||||
};
|
||||
},
|
||||
{},
|
||||
|
||||
Reference in new issue
Block a user