From fba2b579fbca83604579ee7283f43091cdcd00fd Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 27 Apr 2021 11:34:34 +0100 Subject: [PATCH] Update i18n (and fix commands) --- package.json | 2 +- pnpm-lock.yaml | 8 ++++---- src/commands/blacklist.js | 4 ++-- src/commands/close.js | 4 ++-- src/commands/new.js | 4 ++-- src/commands/settings.js | 4 ++-- src/index.js | 13 ++++++++++++- src/modules/commands/command.js | 2 +- src/modules/commands/manager.js | 7 +++---- src/modules/tickets/manager.js | 4 ++-- 10 files changed, 31 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 21879c8..612bac4 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "homepage": "https://discordtickets.app", "funding": "https://github.com/discord-tickets/bot/?sponsor=1", "dependencies": { - "@eartharoid/i18n": "^0.0.1", + "@eartharoid/i18n": "^1.0.0", "boxen": "^5.0.0", "cryptr": "^6.0.2", "discord.js": "^12.5.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5645cc..4fcd4f3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,7 +1,7 @@ lockfileVersion: 5.3 specifiers: - '@eartharoid/i18n': ^0.0.1 + '@eartharoid/i18n': ^1.0.0 boxen: ^5.0.0 cryptr: ^6.0.2 discord.js: ^12.5.1 @@ -22,7 +22,7 @@ specifiers: terminal-link: ^2.1.1 dependencies: - '@eartharoid/i18n': 0.0.1 + '@eartharoid/i18n': 1.0.0 boxen: 5.0.0 cryptr: 6.0.2 discord.js: 12.5.1 @@ -130,8 +130,8 @@ packages: resolution: {integrity: sha512-e3mR8JY6Uuy1Zj89iYEZfvK6s81GlWEPP4gO8NzfItzF4xFGFTVTwRjZ6sCVBmbhj3ouX2pyvx8O9snx4r5Xrg==} dev: false - /@eartharoid/i18n/0.0.1: - resolution: {integrity: sha512-RXm2+aRBAa2cqp/UEPtlO1AkEJlv5/bANvuPNKA4MSp/UNRKgsJ2tRQt7YintINtgp1ZmaJMH8pxcaLUzwfKUw==} + /@eartharoid/i18n/1.0.0: + resolution: {integrity: sha512-jJlVTQxHdkuA6prAJ/7kkQhfiTTLioaRT0AQtOcSsOZZzZfzlkKvpcvUtz+qlyrL1oen+KylG0ivlCJNnRYtVA==} dev: false /@eslint/eslintrc/0.4.0: diff --git a/src/commands/blacklist.js b/src/commands/blacklist.js index 268cb3e..990fde4 100644 --- a/src/commands/blacklist.js +++ b/src/commands/blacklist.js @@ -3,7 +3,7 @@ const Command = require('../modules/commands/command'); module.exports = class BlacklistCommand extends Command { constructor(client) { - const i18n = client.i18n.get(client.config.locale); + const i18n = client.i18n.getLocale(client.config.locale); super(client, { internal: true, name: i18n('commands.blacklist.name'), @@ -27,7 +27,7 @@ module.exports = class BlacklistCommand extends Command { async execute(message, args) { let settings = await message.guild.settings; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); let member = message.mentions.members.first(); diff --git a/src/commands/close.js b/src/commands/close.js index 600a0ae..bb1b932 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -4,7 +4,7 @@ const { footer } = require('../utils/discord'); module.exports = class CloseCommand extends Command { constructor(client) { - const i18n = client.i18n.get(client.config.locale); + const i18n = client.i18n.getLocale(client.config.locale); super(client, { internal: true, name: i18n('commands.close.name'), @@ -27,7 +27,7 @@ module.exports = class CloseCommand extends Command { async execute(message, args) { let settings = await message.guild.settings; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); } }; \ No newline at end of file diff --git a/src/commands/new.js b/src/commands/new.js index 97dcb5a..2399a68 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -6,7 +6,7 @@ const { wait } = require('../utils'); module.exports = class NewCommand extends Command { constructor(client) { - const i18n = client.i18n.get(client.config.locale); + const i18n = client.i18n.getLocale(client.config.locale); super(client, { internal: true, name: i18n('commands.new.name'), @@ -30,7 +30,7 @@ module.exports = class NewCommand extends Command { async execute(message, args) { let settings = await message.guild.settings; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); const editOrSend = async (msg, content) => { if (msg) return await msg.edit(content); diff --git a/src/commands/settings.js b/src/commands/settings.js index cfd5521..2cbd130 100644 --- a/src/commands/settings.js +++ b/src/commands/settings.js @@ -4,7 +4,7 @@ const { MessageAttachment } = require('discord.js'); module.exports = class SettingsCommand extends Command { constructor(client) { - const i18n = client.i18n.get(client.config.locale); + const i18n = client.i18n.getLocale(client.config.locale); super(client, { internal: true, name: i18n('commands.settings.name'), @@ -19,7 +19,7 @@ module.exports = class SettingsCommand extends Command { async execute(message) { let settings = await message.guild.settings; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); let attachments = [ ...message.attachments.values() ]; diff --git a/src/index.js b/src/index.js index 72c31ec..7e69009 100644 --- a/src/index.js +++ b/src/index.js @@ -130,8 +130,19 @@ class Bot extends Client { /** A [Cryptr](https://www.npmjs.com/package/cryptr) instance */ this.cryptr = new Cryptr(process.env.DB_ENCRYPTION_KEY); + let locales = {}; + fs.readdirSync(path('./src/locales')) + .filter(file => file.endsWith('.json')) + .forEach(file => { + let data = fs.readFileSync(path(`./src/locales/${file}`), { + encoding: 'utf8' + }); + let name = file.slice(0, file.length - 5); + locales[name] = JSON.parse(data); + }); + /** An [@eartharoid/i18n](https://github.com/eartharoid/i18n) instance */ - this.i18n = new I18n(path('./src/locales'), 'en-GB'); + this.i18n = new I18n('en-GB', locales); /** A sequelize instance */ this.db = await require('./database')(this), // this.db.models.Ticket... diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index 0f06230..e4422cc 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -128,7 +128,7 @@ module.exports = class Command { if (!cmd_name) cmd_name = this.name; const prefix = settings.command_prefix; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); const addArgs = (embed, arg) => { let required = arg.required ? '`❗` ' : ''; diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index e12c5c8..2fdaf47 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -61,6 +61,9 @@ module.exports = class CommandManager { * @param {Message} message - Command message */ async handle(message) { + let settings = await message.guild.settings; + const i18n = this.client.i18n.getLocale(settings.locale); + let is_blacklisted = false; if (settings.blacklist.includes(message.author.id)) { is_blacklisted = true; @@ -82,10 +85,6 @@ module.exports = class CommandManager { } } - let settings = await message.guild.settings; - - const i18n = this.client.i18n.get(settings.locale); - const prefix = settings.command_prefix; const escaped_prefix = prefix.toLowerCase().replace(/(?=\W)/g, '\\'); // (lazy) escape every character so it can be used in a RexExp const client_mention = `<@!?${this.client.user.id}>`; diff --git a/src/modules/tickets/manager.js b/src/modules/tickets/manager.js index 7f9e2ee..a934ebb 100644 --- a/src/modules/tickets/manager.js +++ b/src/modules/tickets/manager.js @@ -30,7 +30,7 @@ module.exports = class TicketManager extends EventEmitter { let guild = this.client.guilds.cache.get(cat_row.guild); let settings = await guild.settings; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); let member = guild.members.cache.get(t_row.creator); let t_channel = this.client.channels.cache.get(t_row.id); @@ -218,7 +218,7 @@ module.exports = class TicketManager extends EventEmitter { let guild = this.client.guilds.cache.get(t_row.guild); let settings = await guild.settings; - const i18n = this.client.i18n.get(settings.locale); + const i18n = this.client.i18n.getLocale(settings.locale); let channel = await this.client.channels.fetch(t_row.channel); if (closer_id) {