Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
make improvements to languagemanager & add ping command
Browse files Browse the repository at this point in the history
  • Loading branch information
davipatricio committed Apr 27, 2022
1 parent 4792c30 commit 50c5445
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Command, CommandRunOptions } from '../../../structures/Command';

export default class TestCommand extends Command {
export default class PingCommand extends Command {
constructor() {
super();
this.rawName = 'TEST';
this.rawCategory = 'Test';
this.rawName = 'PING';
this.rawCategory = 'UTILS';
this.config = {
autoDefer: true,
ephemeral: false,
Expand All @@ -17,6 +17,6 @@ export default class TestCommand extends Command {
}

override run({ client, interaction }: CommandRunOptions) {
return { client, interaction };
return interaction.editReply(`${client.ws.ping}ms`);
}
}
19 changes: 13 additions & 6 deletions src/bot/managers/LanguageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
import { readdir } from 'node:fs/promises';
import type { DenkyClient } from '../../types/Client';

type LocaleCategories = 'command' | 'descriptions' | 'categories';
type LocaleCategories = 'command' | 'commandNames' | 'commandDescriptions' | 'commandCategories';
type SupportedLocales = 'en' | 'pt_BR';

type CommandKeys = keyof typeof import('../../locales/command/pt_BR/index').default;
type CommandLocaleKeys = keyof typeof import('../../locales/command/pt_BR/index').default;
type CommandNamesKeys = keyof typeof import('../../locales/commandNames/pt_BR/index').default;
type CommandDescriptionsKeys = keyof typeof import('../../locales/commandDescriptions/pt_BR/index').default;
type CommandCategoriesKeys = keyof typeof import('../../locales/commandCategories/pt_BR/index').default;

type AllLocaleKeys = CommandLocaleKeys | CommandNamesKeys | CommandDescriptionsKeys | CommandCategoriesKeys;

export class LanguageManager {
/** The client that instantiated this manager */
client: DenkyClient;
cache: Record<LocaleCategories, Record<SupportedLocales, Record<CommandKeys, string | ((...args: unknown[]) => string)>>>;
cache: Record<LocaleCategories, Record<SupportedLocales, Record<AllLocaleKeys, string | ((...args: unknown[]) => string)>>>;
constructor(client: DenkyClient) {
this.client = client;
}
Expand All @@ -27,11 +32,13 @@ export class LanguageManager {
}
}

get(lang: SupportedLocales, path: `command/${CommandKeys}`, ...args: unknown[]): string;
get(lang: SupportedLocales, path: `descriptions/${CommandKeys}`, ...args: unknown[]): string;
get(lang: SupportedLocales, path: `command/${CommandLocaleKeys}`, ...args: unknown[]): string;
get(lang: SupportedLocales, path: `descriptions/${CommandDescriptionsKeys}`, ...args: unknown[]): string;
get(lang: SupportedLocales, path: `categories/${CommandCategoriesKeys}`, ...args: unknown[]): string;
get(lang: SupportedLocales, path: `names/${CommandNamesKeys}`, ...args: unknown[]): string;
get(lang: SupportedLocales, path: string, ...args: unknown[]) {
const [category, key] = path.split('/');
const locale = this.cache[category as LocaleCategories][lang][key as CommandKeys];
const locale = this.cache[category as LocaleCategories][lang][key as AllLocaleKeys];
if (!locale) return `!!{${path}}!!`;

if (typeof locale === 'string') return locale;
Expand Down
3 changes: 3 additions & 0 deletions src/locales/commandNames/pt_BR/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
PING: 'ping',
} as const;

0 comments on commit 50c5445

Please sign in to comment.