From 5669b0e19544c71b61b314fb7555a4f4af2ff971 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:59:27 -0400 Subject: [PATCH] description locales --- src/commands/applications/chat.ts | 6 ++++++ src/commands/handler.ts | 24 ++++++++++++++++++++++++ src/langs/handler.ts | 8 ++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/commands/applications/chat.ts b/src/commands/applications/chat.ts index 58ec7e8..cffa4a4 100644 --- a/src/commands/applications/chat.ts +++ b/src/commands/applications/chat.ts @@ -4,6 +4,7 @@ import type { APIApplicationCommandBasicOption, APIApplicationCommandOption, APIApplicationCommandSubcommandGroupOption, + FlatObjectKeys, LocaleString, PermissionStrings, } from '../../common'; @@ -13,6 +14,7 @@ import type { Groups, RegisteredMiddlewares } from '../decorators'; import type { OptionResolver } from '../optionresolver'; import type { CommandContext } from './chatcontext'; import type { + DefaultLocale, NextFunction, OKFunction, OnOptionsReturnObject, @@ -50,6 +52,10 @@ type Wrap = N extends description: string; description_localizations?: APIApplicationCommandBasicOption['description_localizations']; name_localizations?: APIApplicationCommandBasicOption['name_localizations']; + locales?: { + name?: FlatObjectKeys; + description?: FlatObjectKeys; + }; }; export type __TypeWrapper = Wrap; diff --git a/src/commands/handler.ts b/src/commands/handler.ts index 44b776f..1f8674b 100644 --- a/src/commands/handler.ts +++ b/src/commands/handler.ts @@ -135,6 +135,30 @@ export class CommandHandler extends BaseHandler { } } + for (const options of command.options ?? []) { + if (options instanceof SubCommand || !options.locales) continue; + options.name_localizations = {}; + options.description_localizations = {}; + for (const locale of Object.keys(client.langs.values)) { + const locales = this.client.langs.aliases.find(x => x[0] === locale)?.[1] ?? []; + if (Object.values(Locale).includes(locale)) locales.push(locale as LocaleString); + + if (options.locales.name) { + for (const i of locales) { + const valueName = client.langs.getKey(locale, options.locales.name!); + if (valueName) options.name_localizations[i] = valueName; + } + } + + if (options.locales.description) { + for (const i of locales) { + const valueKey = client.langs.getKey(locale, options.locales.description!); + if (valueKey) options.description_localizations[i] = valueKey; + } + } + } + } + if (command instanceof Command && command.__tGroups) { command.groups = {}; for (const locale of Object.keys(client.langs.values)) { diff --git a/src/langs/handler.ts b/src/langs/handler.ts index 54fc764..a60c04a 100644 --- a/src/langs/handler.ts +++ b/src/langs/handler.ts @@ -15,8 +15,12 @@ export class LangsHandler extends BaseHandler { getKey(lang: string, message: string) { let value = this.values[lang as string]; - for (const i of message.split('.')) { - value = value[i]; + try { + for (const i of message.split('.')) { + value = value[i]; + } + } catch { + return; } if (typeof value !== 'string') {