feat: locales #27

Merged
Anri merged 26 commits from lang into main 2022-07-22 11:46:48 +02:00
8 changed files with 77 additions and 10 deletions
Showing only changes of commit 09cf5c1678 - Show all commits

View file

@ -27,10 +27,16 @@ services:
build: https://git.kennel.ml/ConfrerieDuKassoulait/Botanique.git#main build: https://git.kennel.ml/ConfrerieDuKassoulait/Botanique.git#main
container_name: Botanique container_name: Botanique
environment: environment:
- TOKEN_DISCORD=your-token-goes-here - TOKEN_DISCORD=ton-token-va-ici
restart: unless-stopped 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 ### Références
[Photo de profil](https://picrew.me/image_maker/1497656) [Photo de profil](https://picrew.me/image_maker/1497656)

View file

@ -7,11 +7,13 @@ export default {
.setDescription('Pong!'), .setDescription('Pong!'),
interaction: async (interaction: CommandInteraction, client: Client) => { interaction: async (interaction: CommandInteraction, client: Client) => {
const loc = client.locale;
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message; const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message;
interaction.editReply( interaction.editReply(
`Roundtrip latency: \ `${loc.get('c_ping1')}: \
${sent.createdTimestamp - interaction.createdTimestamp}ms ${sent.createdTimestamp - interaction.createdTimestamp}ms
Websocket heartbeat: ${client.ws.ping}ms.`); ${loc.get('c_ping2')}: ${client.ws.ping}ms.`);
}, },
}; };

View file

@ -1,6 +1,8 @@
import { Client } from 'discord.js';
export const once = true; export const once = true;
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */ /** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */
export default async () => { export default async (client: Client) => {
console.log('Connected to Discord!'); console.log(client.locale.get('e_ready'));
}; };

View file

@ -6,7 +6,7 @@ export default (interaction: Interaction, client: Client) => {
const command = client.commands.get(interaction.commandName); const command = client.commands.get(interaction.commandName);
if (!command) { if (!command) {
return interaction.reply({ return interaction.reply({
content: 'Sorry, the command probably no longer exists...', content: client.locale.get('e_interacreate'),
ephemeral: true, ephemeral: true,
}); });
} }

View file

@ -40,7 +40,7 @@ const run = async () => {
throw logStart(commands_name, false); 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(() => { .catch(() => {
throw logStart(client_name, false); throw logStart(client_name, false);

10
src/locales/en.json Normal file
View file

@ -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"
}

10
src/locales/fr.json Normal file
View file

@ -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"
}

View file

@ -1,5 +1,5 @@
import { Client, Collection, Intents } from 'discord.js'; import { Client, Collection, Intents } from 'discord.js';
import { readFileSync } from 'fs'; import { readFileSync, existsSync } from 'fs';
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
const { version } = JSON.parse(readFileSync('./package.json').toString()); const { version } = JSON.parse(readFileSync('./package.json').toString());
@ -9,7 +9,8 @@ declare module 'discord.js' {
export interface Client { export interface Client {
config: { config: {
version: string, version: string,
token_discord: string | undefined token_discord: string | undefined,
lang: string
}, },
commands: Collection< commands: Collection<
string, string,
@ -17,7 +18,8 @@ declare module 'discord.js' {
data: SlashCommandBuilder, data: SlashCommandBuilder,
interaction: (interaction: CommandInteraction, client: Client) => unknown interaction: (interaction: CommandInteraction, client: Client) => unknown
} }
> >,
locale: Map<string, string>
} }
} }
@ -33,10 +35,45 @@ export default async () => {
client.config = { client.config = {
version: version, version: version,
token_discord: process.env.TOKEN_DISCORD, token_discord: process.env.TOKEN_DISCORD,
lang: process.env.LANGUAGE ?? '',
}; };
// Store the commands available // Store the commands available
client.commands = new Collection(); client.commands = new Collection();
// Store all the strings
client.locale = await getLocale(client);
return 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]];
}),
);
};