Files
gestion/commands/utils/helpall.js
T
2024-02-26 13:16:45 +01:00

103 lines
3.9 KiB
JavaScript

const { EmbedBuilder , ButtonBuilder, ButtonStyle, ActionRowBuilder } = require('discord.js');
const sqlite3 = require('sqlite3').verbose();
module.exports = {
name: 'helpall',
aliases: ['hall', 'aideall'],
description: 'Liste toutes les commandes disponibles par niveau de permission',
emote: '📚',
utilisation: 'helpall',
category: 'utils',
async execute(message, args, client) {
const db = new sqlite3.Database('myDatabase.db');
const botId = client.user.id;
const guildId = message.guild.id;
const defaultPrefix = "+";
let data = await new Promise((resolve, reject) => {
db.get('SELECT value FROM gestion WHERE id = ?', [botId], (err, row) => {
if (err) {
console.error(err.message);
reject(err);
}
resolve(row ? JSON.parse(row.value) : {});
});
});
const permissions = data.permissions || {};
const prefix = data.prefix || defaultPrefix;
const commandsByPermission = {};
// Parcourir les permissions et les commandes associées
for (const [commandName, permissionLevel] of Object.entries(permissions)) {
if (!commandsByPermission[permissionLevel]) {
commandsByPermission[permissionLevel] = [];
}
commandsByPermission[permissionLevel].push(commandName);
}
// Créer les embeds pour chaque niveau de permission avec des commandes associées
const embeds = Object.entries(commandsByPermission).map(([permissionLevel, commands]) => {
const commandDescriptions = commands.map(commandName => {
const command = client.commands.get(commandName);
if (!command) {
return null;
}
return `**${prefix}${commandName}**\n\`${command.description}\``;
}).filter(Boolean);
if (commandDescriptions.length === 0) {
return null; // Skip creating an embed if there are no commands for this permission level
}
const embed = new EmbedBuilder()
.setTitle(`Commandes de niveau de permission ${permissionLevel}`)
.setColor('#0099ff');
embed.setDescription(commandDescriptions.join('\n'));
return embed;
}).filter(Boolean) // Filter out any undefined embeds
if (embeds.length === 0) {
return message.reply("Aucune commande disponible pour ce serveur.");
}
const backButton = new ButtonBuilder()
.setCustomId('back')
.setLabel('⬅️')
.setStyle(ButtonStyle.Primary);
const nextButton = new ButtonBuilder()
.setCustomId('next')
.setLabel('➡️')
.setStyle(ButtonStyle.Primary);
const row = new ActionRowBuilder()
.addComponents(backButton, nextButton);
const msg = await message.channel.send({ embeds: [embeds[0]], components: [row] });
const collector = msg.createMessageComponentCollector({
filter: (interaction) => interaction.isButton() && interaction.user.id === message.author.id,
time: 60000
});
let currentPage = 0;
collector.on('collect', async (interaction) => {
await interaction.deferUpdate();
if (interaction.customId === 'back') {
currentPage = currentPage > 0 ? --currentPage : embeds.length - 1;
} else if (interaction.customId === 'next') {
currentPage = currentPage + 1 < embeds.length ? ++currentPage : 0;
}
await msg.edit({ embeds: [embeds[currentPage]], components: [row] });
});
collector.on('end', () => msg.edit({ embeds: [embeds[currentPage]], components: [] }));
},
};