mirror of
https://github.com/arthur-pbty/gestion-perso.git
synced 2026-06-20 21:41:20 +02:00
add anti crash + command
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { Message, EmbedBuilder, StringSelectMenuBuilder, ActionRowBuilder } from "discord.js";
|
||||
const getServerPrefix = require("../../fonctions/getServerPrefix");
|
||||
|
||||
interface Command {
|
||||
name: string;
|
||||
aliases?: string[];
|
||||
description: string;
|
||||
emote?: string;
|
||||
utilisation?: string;
|
||||
category?: string;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
aliases: ['h', 'aide'],
|
||||
description: 'Affiche la liste des commandes',
|
||||
emote: '📚',
|
||||
utilisation: '[commande]',
|
||||
|
||||
async execute(message: Message, args: string[], client: any) {
|
||||
const prefix = await getServerPrefix(message.guild?.id);
|
||||
|
||||
if (args[0]) {
|
||||
const command = client.commands.get(args[0]);
|
||||
if (!command) {
|
||||
return message.reply(`Je n'ai pas trouvé de commande nommée "${args[0]}".`);
|
||||
}
|
||||
|
||||
const embed_command = new EmbedBuilder()
|
||||
.setColor('#0099ff')
|
||||
.setTitle(`Aide pour la commande ${command.emote ? ` ${command.emote}` : '🔧'} ${command.name}`)
|
||||
.setDescription(command.description)
|
||||
.addFields(
|
||||
{ name: 'Utilisation', value: `\`${prefix}${command.name}${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: ${command.permission}` || 'Indéfini', inline: true },
|
||||
)
|
||||
.setTimestamp()
|
||||
.setFooter({ text: `${client.user?.tag} © 2024`, iconURL: client.user?.displayAvatarURL({ dynamic: true })});
|
||||
|
||||
return message.reply({ embeds: [embed_command] });
|
||||
|
||||
} else {
|
||||
|
||||
let commands: Command[] = [];
|
||||
for (const command of client.commands?.values() || []) {
|
||||
const existingCommand = commands.find(cmd => cmd.name === command.name);
|
||||
if (!existingCommand) {
|
||||
commands.push(command);
|
||||
}
|
||||
}
|
||||
|
||||
let categories: { [key: string]: Command[] } = {};
|
||||
commands.forEach((command: any) => {
|
||||
if (!categories[command.category]) {
|
||||
categories[command.category] = [];
|
||||
}
|
||||
categories[command.category]?.push(command);
|
||||
});
|
||||
|
||||
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)) {
|
||||
description += `**Catégorie ${category}**\n`;
|
||||
description += commands.length > 0 ? '> ' + commands.map(command => `\`${command.name}\``).join(', ') : '> Aucune commande dans cette catégorie';
|
||||
description += '\n\n';
|
||||
}
|
||||
|
||||
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: [] });
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user