From 37f02daac3943500f5c4076a443877c8b2485c69 Mon Sep 17 00:00:00 2001 From: Tutur33 Date: Sun, 25 Feb 2024 22:00:19 +0100 Subject: [PATCH] add help command --- commands/help.ts | 71 ++++++++++++++++++++++++++++++++++--------- commands/ping.ts | 31 +++++++++++++++++-- commands/setprefix.ts | 4 ++- 3 files changed, 88 insertions(+), 18 deletions(-) diff --git a/commands/help.ts b/commands/help.ts index 316bd82..5eb2dcc 100644 --- a/commands/help.ts +++ b/commands/help.ts @@ -1,4 +1,5 @@ -import { Message, EmbedBuilder } from "discord.js"; +import { Message, EmbedBuilder, StringSelectMenuBuilder, ActionRowBuilder } from "discord.js"; +const getServerPrefix = require("../fonctions/getServerPrefix"); interface Command { name: string; @@ -16,7 +17,7 @@ module.exports = { utilisation: '[commande]', async execute(message: Message, args: string[], client: any) { - const prefix = '+'; + const prefix = await getServerPrefix(message.guild?.id); if (args[0]) { const command = client.commands.get(args[0]); @@ -24,8 +25,6 @@ module.exports = { return message.reply(`Je n'ai pas trouvé de commande nommée "${args[0]}".`); } - const commandPerm = 'None'; - const embed_command = new EmbedBuilder() .setColor('#0099ff') .setTitle(`Aide pour la commande ${command.emote ? ` ${command.emote}` : '🔧'} ${command.name}`) @@ -34,7 +33,7 @@ module.exports = { { name: 'Utilisation', value: `\`${prefix}${command.utilisation ? `${command.utilisation}` : ''}\``, inline: true }, { name: 'Catégorie', value: command.category || 'Non spécifiée', inline: true }, { name: 'Alias', value: command.aliases ? command.aliases.map((alias: string) => `\`${alias}\``).join(', ') : 'Aucun', inline: true }, - { name: 'Permissions', value: `Perm level: ${commandPerm}` || 'Indéfini', inline: true }, + { name: 'Permissions', value: `Perm level: ${command.permission}` || 'Indéfini', inline: true }, ) .setTimestamp() .setFooter({ text: `${client.user?.tag} © 2024`, iconURL: client.user?.displayAvatarURL({ dynamic: true })}); @@ -54,24 +53,66 @@ module.exports = { let categories: { [key: string]: Command[] } = {}; commands.forEach((command: any) => { if (!categories[command.category]) { - categories[command.category] = []; + categories[command.category] = [{ name: command.name, description: command.description, emote: command.emote, utilisation: command.utilisation}]; } categories[command.category]?.push(command); }); - const embed = new EmbedBuilder() - .setTitle('Liste des commandes') - .setDescription('Sélectionnez une catégorie parmi les options ci-dessous pour obtenir de l\'aide sur les commandes correspondantes.') - .setColor('#0099ff'); + let description = `Sélectionnez une catégorie parmi les options ci-dessous pour obtenir de l'aide sur les commandes correspondantes.\n Il y a ${commands.length} commandes disponibles\n\n`; for (const [category, commands] of Object.entries(categories)) { - embed.addFields({ - name: `Catégorie ${category}`, - value: commands.map((command: Command) => `\`${command.name}\` - ${command.description}`).join('\n') - }); + description += `**Catégorie ${category}**\n`; + description += commands.length > 0 ? '> ' + commands.map(command => `\`${command.name}\``).join(', ') : '> Aucune commande dans cette catégorie'; + description += '\n\n'; } - message.channel.send({ embeds: [embed] }); + const embeds: any = {home: new EmbedBuilder() + .setTitle('📚 Information') + .setDescription(description) + .setColor('#0099ff') + .setThumbnail(message.guild?.iconURL() ?? null) + .setFooter({text: `${client.user.tag} © 2024`, iconURL: client.user.displayAvatarURL({dynamic: true})}) + .setTimestamp() + }; + + for (const [category, commands] of Object.entries(categories)) { + let description; + if (commands.length > 0) { + description = commands.map(command => `${command.emote ? ` ${command.emote}` : '🔧'} **${prefix}${command.name ? ` ${command.name} ${command.utilisation}` : ''}**\n \`${command.description}\``).join('\n'); + } else { + description = 'Aucune commande dans cette catégorie.'; + } + + embeds[category] = new EmbedBuilder() + .setColor('#0099ff') + .setTitle(`Catégorie ${category}`) + .setDescription(description) + .setTimestamp() + .setFooter({text: `${client.user.tag} © 2024`, iconURL: client.user.displayAvatarURL({dynamic: true})}) + } + + const menu = new StringSelectMenuBuilder() + .setCustomId('help_menu') + .setPlaceholder('Sélectionnez une catégorie') + .addOptions([ + { label: 'Accueil', value: 'home' }, + ...Object.keys(categories).map(category => ({ label: `Catégorie ${category}`, value: category })), + ]); + + const row: any = new ActionRowBuilder() + .addComponents(menu); + + const sentMessage = await message.reply({ embeds: [embeds.home], components: [row] }); + + const filter = (i: any) => i.customId === 'help_menu' && i.user.id === message.author.id; + const collector = sentMessage.createMessageComponentCollector({ filter, time: 60000 }); + collector.on('collect', async (interaction: any) => { + await interaction.update({ embeds: [embeds[interaction.values[0]]] }); + }); + + collector.on('end', () => { + sentMessage.edit({ components: [] }); + }); } }, }; \ No newline at end of file diff --git a/commands/ping.ts b/commands/ping.ts index a3eaa8a..f358cd2 100644 --- a/commands/ping.ts +++ b/commands/ping.ts @@ -1,3 +1,5 @@ +const { ButtonStyle, ButtonBuilder, ActionRowBuilder } = require('discord.js'); + module.exports = { aliases: ['latence'], description: 'Avoir la latence du bot.', @@ -6,6 +8,31 @@ module.exports = { permission: 0, async execute(message: any, args: any, client: any) { - message.reply('Pong !'); - }, + const ping = new ButtonBuilder() + .setCustomId('confirm') + .setLabel('🔄') + .setStyle(ButtonStyle.Primary); + + const row = new ActionRowBuilder() + .addComponents(ping); + const sentMessage = await message.reply({ + content: `La latence est de : ${client.ws.ping}`, + components: [row], + }); + + + const filter = (i: any) => i.customId === 'confirm' && i.user.id === message.author.id; + const collector = sentMessage.createMessageComponentCollector({ filter, time: 15000 }) + collector.on('collect', async (interaction: any) => { + sentMessage.edit({ + content: `La latence est de : ${client.ws.ping}`, + components: [row], + }); + interaction.reply({ content: 'La latence a été rafraichie', ephemeral: true }); + }); + + collector.on('end', () => { + sentMessage.edit({ components: [] }); + }); + } }; \ No newline at end of file diff --git a/commands/setprefix.ts b/commands/setprefix.ts index b7e3abf..3e991f9 100644 --- a/commands/setprefix.ts +++ b/commands/setprefix.ts @@ -1,5 +1,6 @@ import sqlite3 from 'sqlite3'; import { Message } from 'discord.js'; +const getServerPrefix = require("../fonctions/getServerPrefix"); module.exports = { aliases: ['prefixset'], @@ -9,7 +10,8 @@ module.exports = { permission: 10, async execute(message: Message, args: string[]) { - if (args.length < 1) return message.reply('Veuillez entrer un préfixe.'); + const prefix = await getServerPrefix(message.guild?.id); + if (args.length < 1) return message.reply(`Veuillez entrer un préfixe a la fin de la commande example: \`${prefix}setprefix !\``); let db = new sqlite3.Database('db.db', sqlite3.OPEN_READWRITE, (err: Error | null) => { if (err) {