refactor: cleanup #26

Merged
Anri merged 1 commit from dev into main 2022-07-20 23:47:42 +02:00
7 changed files with 35 additions and 10 deletions

View file

@ -3,6 +3,7 @@ import { Routes } from 'discord-api-types/v9';
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
/** Load all the commands */
export default async (client: Client) => { export default async (client: Client) => {
const rest = new REST({ version: '9' }).setToken(client.token ?? ''); const rest = new REST({ version: '9' }).setToken(client.token ?? '');
@ -23,6 +24,7 @@ export default async (client: Client) => {
await import(`../commands/${command_category}/${command_file}`) await import(`../commands/${command_category}/${command_file}`)
).default; ).default;
// Add it to the collection so the interaction will work
client.commands.set(command.data.name, command); client.commands.set(command.data.name, command);
return command.data.toJSON(); return command.data.toJSON();
@ -32,6 +34,7 @@ export default async (client: Client) => {
) )
).flat(2); ).flat(2);
// Send commands to Discord
return await rest.put(Routes.applicationCommands(client.user?.id ?? ''), { return await rest.put(Routes.applicationCommands(client.user?.id ?? ''), {
body: commands, body: commands,
}); });

View file

@ -1,5 +1,6 @@
export const once = true; export const once = true;
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-ready */
export default async () => { export default async () => {
console.log('Connecté à Discord !'); console.log('Connected to Discord!');
}; };

View file

@ -1,11 +1,12 @@
import { Client, Interaction } from 'discord.js'; import { Client, Interaction } from 'discord.js';
/** https://discord.js.org/#/docs/discord.js/main/class/Client?scrollTo=e-interactionCreate */
export default (interaction: Interaction, client: Client) => { export default (interaction: Interaction, client: Client) => {
if (interaction.isCommand()) { if (interaction.isCommand()) {
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: 'Désolé la commande n\'existe probablement plus...', content: 'Sorry, the command probably no longer exists...',
ephemeral: true, ephemeral: true,
}); });
} }

View file

@ -1,6 +1,7 @@
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
/** Load all the events */
export default async (client: Client) => { export default async (client: Client) => {
const events_categories = (await readdir(__dirname)) const events_categories = (await readdir(__dirname))
.filter(element => !element.endsWith('.js') && !element.endsWith('.ts')); .filter(element => !element.endsWith('.js') && !element.endsWith('.ts'));

View file

@ -2,6 +2,9 @@ import loadClient from './utils/client';
import loadEvents from './events/loader'; import loadEvents from './events/loader';
import loadCommands from './commands/loader'; import loadCommands from './commands/loader';
import { logStart } from './utils/misc';
/** Run the bot */
const run = async () => { const run = async () => {
console.log('Starting Botanique...'); console.log('Starting Botanique...');
@ -13,27 +16,34 @@ const run = async () => {
} }
// Client Discord.JS // Client Discord.JS
const client_name = 'Client';
await loadClient() await loadClient()
.then(async client => { .then(async client => {
console.log('Client ✅'); console.log(logStart(client_name, true));
// Events Discord.JS
const events_name = 'Events';
await loadEvents(client) await loadEvents(client)
.then(() => console.log('Events ✅')) .then(() => console.log(logStart(events_name, true)))
.catch(() => { .catch(() => {
throw 'Events ❌'; throw logStart(events_name, false);
}); });
// Connect the bot to Discord.com
await client.login(client.config.token_discord); await client.login(client.config.token_discord);
// Commands Slash Discord.JS
const commands_name = 'Commands';
await loadCommands(client) await loadCommands(client)
.then(() => console.log('Commands ✅')) .then(() => console.log(logStart(commands_name, true)))
.catch(() => { .catch(() => {
throw 'Commands ❌'; throw logStart(commands_name, false);
}); });
console.log(`Botanique "${client.user?.username}" ${client.config.version} started!`); console.log(`Botanique "${client.user?.username}" v${client.config.version} started!`);
}) })
.catch(() => { .catch(() => {
throw 'Client ❌'; throw logStart(client_name, false);
}); });
}; };

View file

@ -21,7 +21,7 @@ declare module 'discord.js' {
} }
} }
// Création du client et de ses propriétés /** Creation of the client and definition of its properties */
export default async () => { export default async () => {
const client: Client = new Client({ const client: Client = new Client({
intents: [ intents: [

9
src/utils/misc.ts Normal file
View file

@ -0,0 +1,9 @@
/**
* Log module status
* @param {string} name Module name
* @param {boolean} status Module status
* @returns String
*/
export const logStart = (name: string, status: boolean) => {
return `> ${name} ${status === true ? '✅' : '❌'}`;
};