From e9ad8e6409c83ac9e9ea335aa6549f18b5da12b2 Mon Sep 17 00:00:00 2001 From: VALOU3336 Date: Tue, 13 Feb 2024 21:50:33 +0100 Subject: [PATCH] correctif load event et commande --- loaders/loadCommands.js | 38 ++++++++++++++++++++------------ loaders/loadEvents.js | 41 ++++++++++++++++++----------------- loaders/loadPrefixCommands.js | 14 ------------ 3 files changed, 45 insertions(+), 48 deletions(-) delete mode 100644 loaders/loadPrefixCommands.js diff --git a/loaders/loadCommands.js b/loaders/loadCommands.js index 5b63695..340206f 100644 --- a/loaders/loadCommands.js +++ b/loaders/loadCommands.js @@ -1,19 +1,29 @@ const { readdirSync } = require("fs"); -module.exports = async client => { +module.exports = (client) => { + const loadCommands = (dir) => { + let count = 0; + fs.readdirSync(path.join(__dirname, dir)).forEach(file => { + const filePath = path.join(__dirname, dir, file); + if (fs.statSync(filePath).isDirectory()) { + count += loadCommands(path.join(dir, file)); + } else if (file.endsWith('.js')) { + try { + // Delete the cache for this command file + delete require.cache[require.resolve(filePath)]; + const command = require(filePath); + client.commands.set(command.name, command); + count++; + } catch (error) { + console.error(`Failed to load file: ${filePath}`); // Log any errors + console.error(error); + } + } + }); + return count; + } - let count = 0; - const dirsCommands = readdirSync("./commands/"); - - for(const dirs of dirsCommands) { - const filesDirs = readdirSync(`./commands/${dirs}/`).filter(f => f.endsWith(".js")); - for(const files of filesDirs) { - const command = require(`../commands/${dirs}/${files}`); - client.commands.set(command.data.name, command); - count++; - } - }; - - console.log(`[Commands Slash] => ${count} logged commands`); + const totalCommands = loadCommands('../commands'); + console.log(`Commands => ${totalCommands} commandes préfixées chargées sur le bot`); } \ No newline at end of file diff --git a/loaders/loadEvents.js b/loaders/loadEvents.js index 88647cd..0150c51 100644 --- a/loaders/loadEvents.js +++ b/loaders/loadEvents.js @@ -1,22 +1,23 @@ -const { readdirSync } = require("fs"); +const fs = require('fs'); +const path = require('path'); -module.exports = async client => { +module.exports = (client) => { - let count = 0; - const dirsEvents = readdirSync("./events/"); - - for(const dirs of dirsEvents) { - const filesDirs = readdirSync(`./events/${dirs}/`).filter(f => f.endsWith(".js")); - for(const files of filesDirs) { - const event = require(`../events/${dirs}/${files}`); - if(dirs === "music") client.player.events.on(event.name, (...args) => event.run(client, ...args)); - else client.on(event.name, (...args) => event.run(client, ...args)); - count++; - - }; - - }; - - console.log(`Event => ${count} chargé sur le bot`) - -}; \ No newline at end of file + let count = 0; + const loadEvents = (dir) => { + fs.readdirSync(path.join(__dirname, dir)).forEach(file => { + const filePath = path.join(__dirname, dir, file); + if (fs.statSync(filePath).isDirectory()) { + loadEvents(path.join(dir, file)); + } else if (file.endsWith('.js')) { + delete require.cache[require.resolve(filePath)]; + const event = require(filePath); + client.on(event.name, (...args) => event.execute(...args, client)); + console.log(`Event ${event.name} loaded`); + count++; + } + }); + } + loadEvents('../events'); + console.log(`Event => ${count} chargé sur le bot`) +} \ No newline at end of file diff --git a/loaders/loadPrefixCommands.js b/loaders/loadPrefixCommands.js deleted file mode 100644 index b91728f..0000000 --- a/loaders/loadPrefixCommands.js +++ /dev/null @@ -1,14 +0,0 @@ -const fs = require('fs'); -const path = require('path'); - -module.exports = async (client) => { - const prefixCommandsPath = path.join(__dirname, '../commandprefix'); // Ajustez le chemin ici - const commandFiles = fs.readdirSync(prefixCommandsPath).filter(file => file.endsWith('.js')); - - for (const file of commandFiles) { - const command = require(`../commandprefix/${file}`); // Ajustez le chemin ici - client.prefixCommands.set(command.name, command); - } - - console.log(`[Prefix Commands] => ${client.prefixCommands.size} prefix commands loaded`); -}; \ No newline at end of file