Skip to content

Commit

Permalink
Fixed default language bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hwangsihu committed Sep 28, 2024
1 parent 62110b1 commit 42a4065
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/commands/config/Language.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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'))],
Expand Down
7 changes: 5 additions & 2 deletions src/structures/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -54,7 +55,8 @@ export default class Context {
}

private async setUpLocale(): Promise<void> {
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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down

0 comments on commit 42a4065

Please sign in to comment.