Files
gestion/loaders/loadCommands.js
T
2024-02-17 02:02:17 +01:00

91 lines
3.7 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const db = require('quick.db');
const GestionDb = new db.table('gestion')
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);
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;
}
async function getPermissionLevel(member, client) {
const botId = client.user.id;
const buyerId = ['1003985920162287696', '671763971803447298'];
let owners = await GestionDb.get(`${botId}.owners`) || {};
if (buyerId.includes(member.id)) {
return 11;
}
if (owners[member.id]) {
return 10;
}
let highestPermission = 0;
for (let i = 1; i <= 9; i++) {
const roleIds = await GestionDb.get(`${botId}.${member.guild.id}.p${i}`);
if (roleIds) {
// Si roleIds n'est pas un tableau, le convertir en tableau
if (!Array.isArray(roleIds)) {
roleIds = [roleIds];
}
if (roleIds.some(id => member.roles.cache.has(id))) {
highestPermission = Math.max(highestPermission, i);
}
}
}
return highestPermission;
}
const totalCommands = loadCommands('../commands');
console.log(`Commands => ${totalCommands} commandes préfixées chargées sur le bot`);
client.on('messageCreate', async message => {
const botId = client.user.id;
const guildId = message.guild.id;
const botInfo = GestionDb.get(botId);
const permissions = botInfo.permissions;
const defaultprefix = "+";
let mainPrefix = await GestionDb.get(`${botId}.prefix`);
let serverPrefix = await GestionDb.get(`${botId}.${guildId}.prefix`);
const prefix = serverPrefix !== undefined ? serverPrefix : mainPrefix !== undefined ? mainPrefix : defaultprefix;
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
if (!client.commands.has(commandName)) return;
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (command) {
const permissionLevel = await getPermissionLevel(message.member, client, guildId);
if (permissionLevel === 11) {
command.execute(message, args, client);
} else if (permissionLevel >= permissions[command.name]) {
command.execute(message, args, client);
} else {
return message.reply("Vous n'avez pas accès à cette commande.");
}
}
});
}