diff --git a/README.md b/README.md index 519acb2..2d66706 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,16 @@ services: build: https://git.kennel.ml/ConfrerieDuKassoulait/Botanique.git#main container_name: Botanique environment: - - TOKEN_DISCORD=your-token-goes-here + - TOKEN_DISCORD=ton-token-va-ici restart: unless-stopped ``` +## Variables d'environnements +| Nom | Commentaire | Valeur par défaut +| :-----------: | :-----------: | :-: +| TOKEN_DISCORD | Token Discord | Aucune +| LANGUAGE | Langue du bot | en + --- ### Références [Photo de profil](https://picrew.me/image_maker/1497656) diff --git a/src/commands/misc/ping.ts b/src/commands/misc/ping.ts index b121102..bbe8903 100644 --- a/src/commands/misc/ping.ts +++ b/src/commands/misc/ping.ts @@ -7,11 +7,13 @@ export default { .setDescription('Pong!'), interaction: async (interaction: CommandInteraction, client: Client) => { + const loc = client.locale; + const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message; interaction.editReply( - `Roundtrip latency: \ + `${loc.get('c_ping1')}: \ ${sent.createdTimestamp - interaction.createdTimestamp}ms -Websocket heartbeat: ${client.ws.ping}ms.`); +${loc.get('c_ping2')}: ${client.ws.ping}ms.`); }, }; diff --git a/src/events/client/ready.ts b/src/events/client/ready.ts index 4621ee6..2682d88 100644 --- a/src/events/client/ready.ts +++ b/src/events/client/ready.ts @@ -1,6 +1,8 @@ +import { Client } from 'discord.js'; + export const once = true; /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */ -export default async () => { - console.log('Connected to Discord!'); +export default async (client: Client) => { + console.log(client.locale.get('e_ready')); }; diff --git a/src/events/interactions/interactionCreate.ts b/src/events/interactions/interactionCreate.ts index 70caa33..22a93a6 100644 --- a/src/events/interactions/interactionCreate.ts +++ b/src/events/interactions/interactionCreate.ts @@ -6,7 +6,7 @@ export default (interaction: Interaction, client: Client) => { const command = client.commands.get(interaction.commandName); if (!command) { return interaction.reply({ - content: 'Sorry, the command probably no longer exists...', + content: client.locale.get('e_interacreate'), ephemeral: true, }); } diff --git a/src/index.ts b/src/index.ts index 1457e74..0d2cf04 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,7 +40,7 @@ const run = async () => { throw logStart(commands_name, false); }); - console.log(`Botanique "${client.user?.username}" v${client.config.version} started!`); + console.log(`Botanique "${client.user?.username}" v${client.config.version} ${client.locale.get('started')}!`); }) .catch(() => { throw logStart(client_name, false); diff --git a/src/locales/en.json b/src/locales/en.json new file mode 100644 index 0000000..47be44e --- /dev/null +++ b/src/locales/en.json @@ -0,0 +1,10 @@ +{ + "started": "started", + + "e_interacreate": "Sorry, the command probably no longer exists...", + + "e_ready": "Connected to Discord!", + + "c_ping1": "Roundtrip latency", + "c_ping2": "Websocket heartbeat" +} diff --git a/src/locales/fr.json b/src/locales/fr.json new file mode 100644 index 0000000..df1a332 --- /dev/null +++ b/src/locales/fr.json @@ -0,0 +1,10 @@ +{ + "started": "démarré ", + + "e_interacreate": "Désolé, la commande n'existe plus...", + + "e_ready": "Connecté à Discord !", + + "c_ping1": "Latence totale", + "c_ping2": "Latence du Websocket" +} diff --git a/src/utils/client.ts b/src/utils/client.ts index 9a60300..c2f778d 100644 --- a/src/utils/client.ts +++ b/src/utils/client.ts @@ -1,5 +1,5 @@ import { Client, Collection, Intents } from 'discord.js'; -import { readFileSync } from 'fs'; +import { readFileSync, existsSync } from 'fs'; import { SlashCommandBuilder } from '@discordjs/builders'; const { version } = JSON.parse(readFileSync('./package.json').toString()); @@ -9,7 +9,8 @@ declare module 'discord.js' { export interface Client { config: { version: string, - token_discord: string | undefined + token_discord: string | undefined, + lang: string }, commands: Collection< string, @@ -17,7 +18,8 @@ declare module 'discord.js' { data: SlashCommandBuilder, interaction: (interaction: CommandInteraction, client: Client) => unknown } - > + >, + locale: Map } } @@ -33,10 +35,45 @@ export default async () => { client.config = { version: version, token_discord: process.env.TOKEN_DISCORD, + lang: process.env.LANGUAGE ?? '', }; // Store the commands available client.commands = new Collection(); + // Store all the strings + client.locale = await getLocale(client); + return client; }; + + +/** + * Get all the strings corresponding to the locales specified in env variable + * @returns Collection of strings + */ +const getLocale = async (client: Client) => { + const default_lang = 'en'; + + // Check if there is a defined valid value + const lang = client.config.lang === undefined ? default_lang : existsSync( + `./src/locales/${client.config.lang}.json`, + ) ? client.config.lang : default_lang; + + // Fallback to default language in case lang isn't valid + if (client.config.lang !== lang) { + client.config.lang = lang; + } + + const file: { + [key: string]: string + } = await import( + `../locales/${lang}.json` + ); + + return new Map( + Object.keys(file).map(key => { + return [key, file[key]]; + }), + ); +};