From 706dceecb572a7b2bf444749474ff4bef45b3be3 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Sun, 27 Mar 2022 09:57:34 +0200 Subject: [PATCH] feat: localizations --- src/lib/structures/Argument.ts | 11 ++++++++++ src/lib/structures/Command.ts | 15 ++++++++++++-- src/lib/util/common.ts | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/lib/util/common.ts diff --git a/src/lib/structures/Argument.ts b/src/lib/structures/Argument.ts index dd5600897..44d9dd1f3 100644 --- a/src/lib/structures/Argument.ts +++ b/src/lib/structures/Argument.ts @@ -2,6 +2,7 @@ import type { AutocompleteContext } from './contexts/AutocompleteContext'; import { Logger } from '../util/logger/Logger'; import { z } from 'zod'; import type { ApplicationCommandOptionType } from 'discord-api-types/v9'; +import type { LocaleString } from '../util/common'; export enum ArgumentType { 'SUB_COMMAND' = 1, @@ -36,7 +37,9 @@ export interface ArgumentChoice { export interface ArgumentOptions { name: string; + nameLocalizations?: Record; description: string; + descriptionLocalizations?: Record; type: | ArgumentType | keyof typeof ArgumentType @@ -62,7 +65,9 @@ const validationSchema = z .string() .max(32) .regex(/^[a-zA-Z1-9]/), + nameLocalizations: z.any().optional(), description: z.string().max(100), + descriptionLocalizations: z.any().optional(), type: z .union([z.string(), z.nativeEnum(ArgumentType)]) .transform((arg) => @@ -93,7 +98,9 @@ const validationSchema = z export class Argument { public name: string; + public nameLocalizations?: Record; public description: string; + public descriptionLocalizations?: Record; public type: ArgumentType | keyof typeof ArgumentType; public required?: boolean; public choices?: Array; @@ -120,7 +127,9 @@ export class Argument { .parseAsync({ ...options, ...this }) .then((options) => { this.name = options.name; + this.nameLocalizations = options.nameLocalizations; this.description = options.description; + this.descriptionLocalizations = options.descriptionLocalizations; this.type = options.type; this.required = options.required; this.choices = options.choices as Array; @@ -152,7 +161,9 @@ export class Argument { return { name: this.name, + name_localizations: this.nameLocalizations, description: this.description, + description_localizations: this.descriptionLocalizations, type: this.type, required: this.required, choices: this.choices, diff --git a/src/lib/structures/Command.ts b/src/lib/structures/Command.ts index e2aa22477..40f6a6654 100644 --- a/src/lib/structures/Command.ts +++ b/src/lib/structures/Command.ts @@ -4,6 +4,7 @@ import type { CommandContext } from './contexts/CommandContext'; import { Commands } from '../managers/CommandManager'; import { Logger } from '../util/logger/Logger'; import { z } from 'zod'; +import type { LocaleString } from '../util/common'; export enum CommandType { 'MESSAGE' = 0, @@ -17,8 +18,10 @@ export type CommandInhibitors = Array<{ run: CommandInhibitor } | CommandInhibit export interface CommandOptions { name: string; - type: Array; + nameLocalizations?: Record; description?: string; + descriptionLocalizations?: Record; + type: Array; arguments?: Array; inhibitors?: CommandInhibitors; guildId?: string; @@ -35,13 +38,15 @@ const validationSchema = z .string() .max(32) .regex(/^[aA-zZ1-9]/), + nameLocalizations: z.any().optional(), + description: z.string().max(100).optional(), + descriptionLocalizations: z.any().optional(), type: z .union([z.string(), z.nativeEnum(CommandType)]) .transform((arg) => (typeof arg === 'string' && Object.keys(CommandType).includes(arg) ? CommandType[arg] : arg)) .array() .nonempty(), arguments: z.any().array().optional(), - description: z.string().max(100).optional(), inhibitors: z.any().array().optional(), guildId: z.string().optional(), cooldown: z.string().optional(), @@ -58,7 +63,9 @@ const validationSchema = z export class Command { public client: GClient; public name: string; + public nameLocalizations?: Record; public description?: string; + public descriptionLocalizations?: Record; public type: Array; public arguments?: Array; public inhibitors: CommandInhibitors; @@ -80,7 +87,9 @@ export class Command { .parseAsync({ ...options, ...this }) .then((options) => { this.name = options.name || Command.defaults?.name; + this.nameLocalizations = options.nameLocalizations || Command.defaults?.nameLocalizations; this.description = options.description || Command.defaults?.description; + this.descriptionLocalizations = options.descriptionLocalizations || Command.defaults.descriptionLocalizations; this.type = options.type || Command.defaults?.type; this.arguments = options.arguments?.map((argument) => { if (argument instanceof Argument) return argument; @@ -151,7 +160,9 @@ export class Command { if (type === CommandType.SLASH) return { name: this.name, + nameLocalizations: this.nameLocalizations, description: this.description, + descriptionLocalizations: this.descriptionLocalizations, options: this.arguments?.map((argument) => argument.toJSON()), type: type, }; diff --git a/src/lib/util/common.ts b/src/lib/util/common.ts new file mode 100644 index 000000000..4116c42c8 --- /dev/null +++ b/src/lib/util/common.ts @@ -0,0 +1,38 @@ +/** + * TODO: Replace with discord-api-types + * Grabbed from discord-api-types (We can't use it from the package because it's not in a package version 0.26.1) + */ + export enum Locale { + EnglishUS = 'en-US', + EnglishGB = 'en-GB', + Bulgarian = 'bg', + ChineseCN = 'zh-CN', + ChineseTW = 'zh-TW', + Croatian = 'hr', + Czech = 'cs', + Danish = 'da', + Dutch = 'nl', + Finnish = 'fi', + French = 'fr', + German = 'de', + Greek = 'el', + Hindi = 'hi', + Hungarian = 'hu', + Italian = 'it', + Japanese = 'ja', + Korean = 'ko', + Lithuanian = 'lt', + Norwegian = 'no', + Polish = 'pl', + PortugueseBR = 'pt-BR', + Romanian = 'ro', + Russian = 'ru', + SpanishES = 'es-ES', + Swedish = 'sv-SE', + Thai = 'th', + Turkish = 'tr', + Ukrainian = 'uk', + Vietnamese = 'vi', +} + +export type LocaleString = `${Locale}`; \ No newline at end of file