add structure for simplifie creation of global command

This commit is contained in:
Arthur Puechberty
2026-01-17 15:13:22 +01:00
parent 08647924e3
commit d5f0f4c30b
11 changed files with 593 additions and 17 deletions
+59
View File
@@ -0,0 +1,59 @@
const fs = require("fs");
const path = require("path");
const { Collection } = require("discord.js");
module.exports = (client) => {
client.commands = new Collection();
function loadCommandsFromDirectory(directory) {
fs.readdir(directory, (err, files) => {
if (err) {
console.error("Erreur lors de la lecture du dossier:", err);
return;
}
files.forEach((file) => {
const filePath = path.join(directory, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(
"Erreur lors de la récupération des informations du fichier:",
err,
);
return;
}
if (stats.isDirectory()) {
loadCommandsFromDirectory(filePath);
} else if (stats.isFile() && file.endsWith(".js")) {
try {
const command = require(filePath);
const commandName = command.name || file.split(".")[0];
if (!command.category) {
const parentDir = path.basename(path.dirname(filePath));
command.category =
parentDir === "commands" ? "🌟・Other" : parentDir;
}
if (!command.dm) command.dm = false;
if (!command.botOwnerOnly) command.botOwnerOnly = false;
if (!command.permissions) command.permissions = [];
if (!command.aliases) command.aliases = [];
if (!command.description)
command.description = "Aucune description.";
client.commands.set(commandName, command);
delete require.cache[require.resolve(filePath)];
} catch (error) {
console.error(
`Erreur lors du chargement de la commande '${file}':`,
error,
);
}
}
});
});
});
}
loadCommandsFromDirectory(path.join(__dirname, "..", "commands"));
};
+45
View File
@@ -0,0 +1,45 @@
const fs = require("fs");
const path = require("path");
module.exports = (client) => {
function loadEventsFromDirectory(directory) {
fs.readdir(directory, (err, files) => {
if (err) {
console.error("Erreur lors de la lecture du dossier:", err);
return;
}
files.forEach((file) => {
const filePath = path.join(directory, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(
"Erreur lors de la récupération des informations du fichier:",
err,
);
return;
}
if (stats.isDirectory()) {
loadEventsFromDirectory(filePath);
} else if (stats.isFile() && file.endsWith(".js")) {
try {
const event = require(filePath);
let eventName = event.name || file.split(".")[0];
client.on(eventName, event.execute.bind(null, client));
delete require.cache[require.resolve(filePath)];
} catch (error) {
console.error(
`Erreur lors du chargement de l'événement '${file}':`,
error,
);
}
}
});
});
});
}
loadEventsFromDirectory(path.join(__dirname, "..", "events"));
};