diff --git a/.gitignore b/.gitignore index d6d0c3f..f8b8bfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env /node_modules -.vscode \ No newline at end of file +.vscode +json.sqlite \ No newline at end of file diff --git a/commands/gestion/change.js b/commands/gestion/change.js new file mode 100644 index 0000000..4fc9892 --- /dev/null +++ b/commands/gestion/change.js @@ -0,0 +1,47 @@ +const fs = require('fs'); +const path = require('path'); +const db = require('quick.db'); +const GestionDb = new db.table("gestion"); +module.exports = { + name: 'change', + description: 'Change le niveau de permission d\'une commande', + async execute(message, args, client) { + const botId = client.user.id; + const unchangeableCommands = ['owner', 'unowner','buyer']; + + if (args.length < 2) { + return message.reply('Veuillez spécifier une commande et un niveau de permission.'); + } + + const commandName = args[0].toLowerCase(); + let permissionLevel; + + if (args[1] === 'owner') { + permissionLevel = 10; + } + + else if (args[1] === 'buyer') { + permissionLevel = 11; + } + else { + permissionLevel = parseInt(args[1]); + if (isNaN(permissionLevel) || permissionLevel < 0 || permissionLevel > 9) { + return message.reply('Veuillez spécifier un niveau de permission valide (entre 0 et 9).'); + } + } + + if (unchangeableCommands.includes(commandName)) { + return message.reply(`La commande ${commandName} ne peut pas être modifiée.`); + } + + if (!client.commands.has(commandName)) { + return message.reply('Cette commande n\'existe pas.'); + } + + let permissions = await GestionDb.get(`${botId}.permissions`); + permissions[commandName] = permissionLevel; + await GestionDb.set(`${botId}.permissions`, permissions); + + message.reply(`Le niveau de permission de la commande ${commandName} a été changé à ${args[0].toLowerCase()}.`); + }, +}; \ No newline at end of file diff --git a/commands/gestion/delperm.js b/commands/gestion/delperm.js new file mode 100644 index 0000000..dc1952d --- /dev/null +++ b/commands/gestion/delperm.js @@ -0,0 +1,51 @@ +const db = require('quick.db'); +const GestionDb = new db.table("gestion"); + +module.exports = { + name: 'delperm', + description: 'Supprime un rôle d\'un niveau de permission ou d\'une commande', + async execute(message, args, client) { + const botId = client.user.id; + // Récupère le niveau de permission/la commande et le rôle + const permissionOrCommand = args[0].toLowerCase(); + const role = message.mentions.roles.first(); + + // Vérifie si le rôle est valide + if (!role) { + return message.reply('Veuillez mentionner un rôle valide.'); + } + + // Vérifie si c'est un niveau de permission ou une commande + if (['1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(permissionOrCommand)) { + // C'est un niveau de permission + let roleIds = await GestionDb.get(`${botId}.${message.guild.id}.p${permissionOrCommand}`); + if (roleIds) { + // Si roleIds n'est pas un tableau, le convertir en tableau + if (!Array.isArray(roleIds)) { + roleIds = [roleIds]; + } + const index = roleIds.indexOf(role.id); + if (index !== -1) { + roleIds.splice(index, 1); + await GestionDb.set(`${botId}.${message.guild.id}.p${permissionOrCommand}`, roleIds); + } + } + message.reply(`Le rôle pour ${permissionOrCommand} a été supprimé.`); + } else { + // C'est une commande + let roleIds = await GestionDb.get(`${botId}.${message.guild.id}.c${permissionOrCommand}`); + if (roleIds) { + // Si roleIds n'est pas un tableau, le convertir en tableau + if (!Array.isArray(roleIds)) { + roleIds = [roleIds]; + } + const index = roleIds.indexOf(role.id); + if (index !== -1) { + roleIds.splice(index, 1); + await GestionDb.set(`${botId}.${message.guild.id}.c${permissionOrCommand}`, roleIds); + } + } + message.reply(`L commande ${permissionOrCommand} a été supprimé du role.`); + } + }, +}; \ No newline at end of file diff --git a/commands/gestion/perm.js b/commands/gestion/perm.js new file mode 100644 index 0000000..7e0353a --- /dev/null +++ b/commands/gestion/perm.js @@ -0,0 +1,56 @@ +const db = require('quick.db'); +const GestionDb = new db.table("gestion"); +const { EmbedBuilder } = require('discord.js'); +module.exports = { + name: 'perm', + description: 'Affiche les rôles pour chaque niveau de permission et commande', + emote: '🛡️', + utilisation: 'perm', + category: 'botcontrol', + async execute(message, client) { + const botId = message.client.user.id; + const embed = new EmbedBuilder() + .setTitle('Rôles de permission et de commande') + .setColor('#0099ff'); + + for (let i = 1; i <= 9; i++) { + let roleIds = await GestionDb.get(`${botId}.${message.guild.id}.p${i}`); + let roles = 'Aucun rôle défini'; + if (roleIds) { + // Si roleIds n'est pas un tableau, le convertir en tableau + if (!Array.isArray(roleIds)) { + roleIds = [roleIds]; + } + roles = roleIds.map(id => `<@&${id}>`).join(', '); + } + embed.addFields({ name: `Perm ${i}`, value: roles}); + } + const commandNames = await GestionDb.get(`${botId}.${message.guild.id}.commandNames`); + if (commandNames && commandNames.length > 0) { + // Créez un objet pour regrouper les commandes par rôle + const commandsByRole = {}; + + for (const commandName of commandNames) { + const roleIds = await GestionDb.get(`${botId}.${message.guild.id}.c${commandName}`); + if (roleIds && roleIds.length > 0) { + for (const roleId of roleIds) { + if (!commandsByRole[roleId]) { + commandsByRole[roleId] = []; + } + commandsByRole[roleId].push(commandName); + } + } + } + + // Ajoutez chaque groupe de commandes à l'embed + for (const roleId in commandsByRole) { + const role = message.guild.roles.cache.get(roleId); + const commands = commandsByRole[roleId].join('\n'); + embed.addFields({ name: `${role.name}`,value: commands , inline: true }); + } + } + + + message.channel.send({ embeds: [embed] }); + }, +}; \ No newline at end of file diff --git a/commands/gestion/setperm.js b/commands/gestion/setperm.js new file mode 100644 index 0000000..af605db --- /dev/null +++ b/commands/gestion/setperm.js @@ -0,0 +1,60 @@ +const db = require('quick.db'); +const GestionDb = new db.table("gestion"); +module.exports = { + name: 'setperm', + description: 'Définit un rôle pour un niveau de permission ou une commande', + async execute(message, args, client) { + const botId = message.client.user.id; + // Récupère le niveau de permission/la commande et le rôle + const permissionOrCommand = args[0].toLowerCase(); + const role = message.mentions.roles.first(); + + // Vérifie si le rôle est valide + if (!role) { + return message.reply('Veuillez mentionner un rôle valide.'); + } + + // Vérifie si c'est un niveau de permission ou une commande + if (['1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(permissionOrCommand)) { + // C'est un niveau de permission + for (let i = 1; i <= 9; i++) { + let oldRoleIds = await GestionDb.get(`${botId}.${message.guild.id}.p${i}`); + if (oldRoleIds) { + // Si oldRoleIds n'est pas un tableau, le convertir en tableau + if (!Array.isArray(oldRoleIds)) { + oldRoleIds = [oldRoleIds]; + } + const index = oldRoleIds.indexOf(role.id); + if (index !== -1) { + oldRoleIds.splice(index, 1); + await GestionDb.set(`${botId}.${message.guild.id}.p${i}`, oldRoleIds); + } + } + } + let roleIds = await GestionDb.get(`${botId}.${message.guild.id}.p${permissionOrCommand}`); + if (!roleIds) { + roleIds = []; + } else if (!Array.isArray(roleIds)) { + roleIds = [roleIds]; + } + roleIds.push(role.id); + await GestionDb.set(`${botId}.${message.guild.id}.p${permissionOrCommand}`, roleIds); + message.reply(`La permission ${permissionOrCommand} a été défini sur ${role.name}.`); + } else { + // C'est une commande + if (!client.commands.has(permissionOrCommand)) { + return message.reply('Cette commande n\'existe pas.'); + } + let roleIds = await GestionDb.get(`${botId}.${message.guild.id}.c${permissionOrCommand}`); + if (!roleIds) { + roleIds = []; + } else if (!Array.isArray(roleIds)) { + roleIds = [roleIds]; + } + roleIds.push(role.id); + await GestionDb.set(`${botId}.${message.guild.id}.c${permissionOrCommand}`, roleIds); + GestionDb.push(`${botId}.${message.guild.id}.commandNames`, permissionOrCommand); + message.reply(`La role ${role.name} a maintenant accée a la commande ${permissionOrCommand} .`); + } + }, +}; \ No newline at end of file diff --git a/loaders/loadCommands.js b/loaders/loadCommands.js index 134913d..9669f81 100644 --- a/loaders/loadCommands.js +++ b/loaders/loadCommands.js @@ -1,5 +1,7 @@ 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; @@ -22,12 +24,38 @@ module.exports = (client) => { }); 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 botId = client.user.id; + const guildId = message.guild.id; + const botInfo = GestionDb.get(botId); + const permissions = botInfo.permissions; //const prefix = botTokens.coins[botId].prefix; const prefix = '+'; if (!message.content.startsWith(prefix) || message.author.bot) return; @@ -36,17 +64,17 @@ module.exports = (client) => { 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)); + || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)); if (command) { - //const permissionLevel = await getPermissionLevel(message.member, client, guildId); + const permissionLevel = await getPermissionLevel(message.member, client, guildId); - //console.log(permissionLevel, permissions[command.name]) - //if (permissionLevel >= permissions[command.name]) { + console.log(permissionLevel, permissions[command.name]) + if (permissionLevel >= permissions[command.name]) { command.execute(message, args, client); - //} else { - // return message.reply("Vous n'avez pas accès à cette commande."); - // } + } else { + return message.reply("Vous n'avez pas accès à cette commande."); + } } }); } \ No newline at end of file diff --git a/main.js b/main.js index 5e4e798..366a2bc 100644 --- a/main.js +++ b/main.js @@ -2,12 +2,18 @@ require('dotenv').config(); const { Client, IntentsBitField, Collection } = require("discord.js"); const loadCommands = require("./loaders/loadCommands"); const loadEvents = require("./loaders/loadEvents"); - +const db = require('quick.db'); +const GestionDb = new db.table('gestion') const client = new Client({intents: new IntentsBitField(3276799)}); client.events = new Collection(); client.commands = new Collection(); client.snipes = new Map(); +client.once('ready', () => { + const permissions = require('./permissions.json'); + GestionDb.set(`${client.user.id}.permissions`, permissions); + console.log(`${client.user.username} a bien charge les permissions`); +}); (async () => { loadCommands(client); diff --git a/permissions.json b/permissions.json new file mode 100644 index 0000000..5fbf550 --- /dev/null +++ b/permissions.json @@ -0,0 +1,109 @@ +{ + "clear": 4, + "help": 0, + "support": 0, + "setperm": 6, + "delperm": 6, + "owner": 11, + "unowner": 11, + "backup": 11, + "perm": 5, + "alladmin": 5, + "allbot": 5, + "banner": 5, + "botinfo": 5, + "channel": 5, + "helpall": 1, + "pic": 0, + "ping": 0, + "snipe": 0, + "roleinfo": 0, + "uptime": 0, + "setavatar": 10, + "setname": 10, + "prefix": 10, + "servers": 10, + "leave": 10, + "change": 10, + "find": 0, + "serverinfo": 0, + "antibot": 10, + "say": 0, + "massiverole": 6, + "unmassiverole": 6, + "soutien": 10, + "renew": 5, + "lock": 5, + "unlock": 5, + "unlockall": 5, + "lockall": 5, + "hide": 5, + "hideall": 5, + "unhide": 5, + "unhideall": 5, + "ban": 5, + "unban": 5, + "mute": 5, + "unmute": 5, + "tempmute": 5, + "mutelist": 5, + "banlist": 5, + "warn": 5, + "unwarn": 5, + "sanction": 5, + "kick": 5, + "addrole": 5, + "delrole": 5, + "derank": 5, + "stream": 10, + "watch": 10, + "listen": 10, + "game": 10, + "antichannel": 10, + "wl": 10, + "unwl": 10, + "secur": 10, + "buyer": 11, + "clearowner": 10, + "checkperms": 10, + "create": 5, + "embed": 5, + "joinsettings": 10, + "setlog": 10, + "clearsanction": 5, + "unbanall": 8, + "calcul": 0, + "clearwl": 10, + "reset": 10, + "resetall": 11, + "dm": 10, + "giveaway": 5, + "8ball": 0, + "slowmode": 5, + "mybot": 10, + "invite": 10, + "setalerte": 10, + "alerteping": 10, + "messagelog": 10, + "modlog": 10, + "vocallog": 10, + "rolelog": 10, + "snake": 0, + "catsay": 0, + "puissance4": 0, + "morpion": 0, + "pfc": 0, + "vc": 0, + "flood": 0, + "2048": 0, + "findemoji": 0, + "guesspokemon": 0, + "slot": 0, + "fasttype": 0, + "demineur": 0, + "pendu": 0, + "wordle": 0, + "pdefault": 11, + "changelog": 0, + "mod": 10 +} \ No newline at end of file