mirror of
https://github.com/arthur-pbty/gestion-perso.git
synced 2026-06-12 00:04:35 +02:00
add commands and events
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function loadEvents(client, dir) {
|
||||
let count = 0;
|
||||
dir = `../${dir}`
|
||||
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 require.cache[require.resolve(filePath)];
|
||||
const command = require(filePath);
|
||||
client.commands.set(command.name, command);
|
||||
if (command.aliases) {
|
||||
command.aliases.forEach(alias => {
|
||||
client.commands.set(alias, command);
|
||||
});
|
||||
}
|
||||
count++;
|
||||
} catch (error) {
|
||||
console.error(`Failed to load file: ${filePath}`); // Log any errors
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
return count;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function loadEvents(client, dir) {
|
||||
let count = 0;
|
||||
dir = `../${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);
|
||||
if (typeof event.execute === 'function') {
|
||||
client.on(event.name, (...args) => event.execute(...args, client));
|
||||
count++;
|
||||
} else {
|
||||
console.error(`Event ${event.name} does not have an execute method.`);
|
||||
}
|
||||
}
|
||||
});
|
||||
return count;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
const { Client, IntentsBitField, Collection } = require('discord.js')
|
||||
const loadEvents = require("./loadEvents");
|
||||
const loadCommands = require("./loadCommands");
|
||||
|
||||
module.exports = function run(token) {
|
||||
const client = new Client({intents: new IntentsBitField(3276799)});
|
||||
|
||||
client.events = new Collection();
|
||||
client.commands = new Collection();
|
||||
|
||||
console.log(`${loadEvents(client, 'events')} events loaded`);
|
||||
console.log(`${loadCommands(client, 'commands')} commands loaded`);
|
||||
|
||||
client.login(token);
|
||||
}
|
||||
Reference in New Issue
Block a user