Compare commits

..

No commits in common. "8ec15b78038cc67f68a28d8de9524dc87184ab2b" and "75655c6414714e81716ad943050d1f9e23cfaf78" have entirely different histories.

9 changed files with 64 additions and 22 deletions

View file

@ -35,6 +35,7 @@ services:
| Nom | Commentaire | Valeur par défaut
| :-----------: | :-----------: | :-:
| TOKEN_DISCORD | Token Discord | Aucune
| LANGUAGE | Langue du bot | en
---
### Références

View file

@ -25,9 +25,9 @@ export default async (client: Client) => {
).default;
// Add it to the collection so the interaction will work
client.commands.set(command.data(client).name, command);
client.commands.set(command.data.name, command);
return command.data(client).toJSON();
return command.data.toJSON();
}),
);
}),

View file

@ -1,20 +1,19 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { Client, CommandInteraction, Message } from 'discord.js';
import { getLocale } from '../../utils/locales';
export default {
data: (client: Client) => new SlashCommandBuilder()
.setNameLocalizations(getLocale(client, 'ping_name'))
.setDescriptionLocalizations(getLocale(client, 'ping_desc')),
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Pong!'),
interaction: async (interaction: CommandInteraction, client: Client) => {
const loc = client.locales.get(interaction.locale) ?? client.locales.get(client.config.default_lang);
const loc = client.locale;
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message;
interaction.editReply(
`${loc?.get('c_ping1')}: \
`${loc.get('c_ping1')}: \
${sent.createdTimestamp - interaction.createdTimestamp}ms
${loc?.get('c_ping2')}: ${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;
/** 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'));
};

View file

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

View file

@ -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);

View file

@ -1,6 +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"
}

View file

@ -1,6 +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,20 +1,17 @@
import { Client, Collection, Intents } from 'discord.js';
import { readFileSync } from 'fs';
import { readFileSync, existsSync } from 'fs';
import { SlashCommandBuilder } from '@discordjs/builders';
import { loadLocales } from './locales';
const { version } = JSON.parse(readFileSync('./package.json').toString());
declare module 'discord.js' {
// eslint-disable-next-line no-shadow
export interface Client {
/** Store the configuration */
config: {
version: string,
token_discord: string | undefined,
default_lang: string
lang: string
},
/** Store all the slash commands */
commands: Collection<
string,
{
@ -22,8 +19,7 @@ declare module 'discord.js' {
interaction: (interaction: CommandInteraction, client: Client) => unknown
}
>,
/** Store all the localizations */
locales: Map<string, Map<string, string>>
locale: Map<string, string>
}
}
@ -35,15 +31,51 @@ export default async () => {
],
});
// Store the client configuration
client.config = {
version: version,
token_discord: process.env.TOKEN_DISCORD,
default_lang: 'en-US',
lang: process.env.LANGUAGE ?? '',
};
// Store the commands available
client.commands = new Collection();
client.locales = await loadLocales(client.config.default_lang);
// 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-US';
// Check if there is a defined valid value
const old_path = __dirname.split('/');
old_path.pop();
const lang = client.config.lang === undefined ? default_lang : existsSync(
`${old_path.join('/')}/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]];
}),
);
};