Skip to content

Commit

Permalink
feat: localizations
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Mar 27, 2022
1 parent 3af40ec commit 706dcee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/lib/structures/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -36,7 +37,9 @@ export interface ArgumentChoice {

export interface ArgumentOptions {
name: string;
nameLocalizations?: Record<LocaleString, string>;
description: string;
descriptionLocalizations?: Record<LocaleString, string>;
type:
| ArgumentType
| keyof typeof ArgumentType
Expand All @@ -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) =>
Expand Down Expand Up @@ -93,7 +98,9 @@ const validationSchema = z

export class Argument {
public name: string;
public nameLocalizations?: Record<LocaleString, string>;
public description: string;
public descriptionLocalizations?: Record<LocaleString, string>;
public type: ArgumentType | keyof typeof ArgumentType;
public required?: boolean;
public choices?: Array<ArgumentChoice>;
Expand All @@ -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<ArgumentChoice>;
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,8 +18,10 @@ export type CommandInhibitors = Array<{ run: CommandInhibitor } | CommandInhibit

export interface CommandOptions {
name: string;
type: Array<CommandType | keyof typeof CommandType>;
nameLocalizations?: Record<LocaleString, string>;
description?: string;
descriptionLocalizations?: Record<LocaleString, string>;
type: Array<CommandType | keyof typeof CommandType>;
arguments?: Array<Argument | ArgumentOptions>;
inhibitors?: CommandInhibitors;
guildId?: string;
Expand All @@ -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(),
Expand All @@ -58,7 +63,9 @@ const validationSchema = z
export class Command {
public client: GClient;
public name: string;
public nameLocalizations?: Record<LocaleString, string>;
public description?: string;
public descriptionLocalizations?: Record<LocaleString, string>;
public type: Array<CommandType | keyof typeof CommandType>;
public arguments?: Array<Argument>;
public inhibitors: CommandInhibitors;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
};
Expand Down
38 changes: 38 additions & 0 deletions src/lib/util/common.ts
Original file line number Diff line number Diff line change
@@ -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}`;

0 comments on commit 706dcee

Please sign in to comment.