From 42a4065fe0d825966caf8f5bfbfe90e49a3abeb0 Mon Sep 17 00:00:00 2001 From: hwangsihu Date: Sat, 28 Sep 2024 22:07:44 +0900 Subject: [PATCH] Fixed default language bug --- src/commands/config/Language.ts | 10 +++++++--- src/structures/Context.ts | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/commands/config/Language.ts b/src/commands/config/Language.ts index f19f553bd..e9e5f4ff0 100644 --- a/src/commands/config/Language.ts +++ b/src/commands/config/Language.ts @@ -1,4 +1,5 @@ import type { AutocompleteInteraction } from 'discord.js'; +import { env } from '../../env'; import { Command, type Context, type Lavamusic } from '../../structures/index'; import { Language, LocaleFlags } from '../../types'; @@ -60,10 +61,13 @@ export default class LanguageCommand extends Command { } else { subCommand = args.shift(); } + + const defaultLanguage = env.DEFAULT_LANGUAGE || Language.EnglishUS; + if (subCommand === 'set') { const embed = client.embed().setColor(this.client.color.main); - const locale = await client.db.getLanguage(ctx.guild!.id); + const locale = (await client.db.getLanguage(ctx.guild!.id)) || defaultLanguage; let lang: string; @@ -123,8 +127,8 @@ export default class LanguageCommand extends Command { }); } - await client.db.updateLanguage(ctx.guild!.id, Language.EnglishUS); - ctx.guildLocale = Language.EnglishUS; + await client.db.updateLanguage(ctx.guild!.id, defaultLanguage); + ctx.guildLocale = defaultLanguage; return ctx.sendMessage({ embeds: [embed.setDescription(ctx.locale('cmd.language.reset'))], diff --git a/src/structures/Context.ts b/src/structures/Context.ts index 02bae690f..0bb045dab 100644 --- a/src/structures/Context.ts +++ b/src/structures/Context.ts @@ -15,6 +15,7 @@ import { type TextChannel, type User, } from 'discord.js'; +import { env } from '../env'; import { T } from './I18n'; import type { Lavamusic } from './index'; @@ -54,7 +55,8 @@ export default class Context { } private async setUpLocale(): Promise { - this.guildLocale = this.guild ? await this.client.db.getLanguage(this.guild.id) : 'en'; + const defaultLanguage = env.DEFAULT_LANGUAGE || 'EnglishUS'; + this.guildLocale = this.guild ? await this.client.db.getLanguage(this.guild.id) : defaultLanguage; } public get isInteraction(): boolean { @@ -105,7 +107,7 @@ export default class Context { } public locale(key: string, ...args: any) { - if (!this.guildLocale) this.guildLocale = 'EnglishUs'; + if (!this.guildLocale) this.guildLocale = env.DEFAULT_LANGUAGE || 'EnglishUS'; return T(this.guildLocale, key, ...args); } @@ -124,6 +126,7 @@ export default class Context { public get deferred(): boolean | undefined { return this.isInteraction ? this.interaction?.deferred : !!this.msg; } + options = { getRole: (name: string, required = true) => { return this.interaction?.options.get(name, required)?.role;