¡Tengo algunos comandos de categorías específicas y quiero enumerarlos cuando el usuario ejecuta un comando! Por ejemplo, uno de mis comandos:
const Discord = require('discord.js') const { MessageEmbed } = require('discord.js') module.exports = { name: "ping", description: "Bot websocket ping", category: "general", run: async (Raphy, message, args) => { message.channel.send(`${Raphy.ws.ping} ws ping`); } } Quiero enumerar los comandos en la categoría General si el usuario ejecuta !commands General , ¿cómo puedo hacer esto?
Un filtro simple funcionará:
let categoryCmds = message.client.commands.filter(c => c.category === args[0].toLowerCase()) message.reply(categoryCmds.map(c => c.name).join(" **|** "))Esta es una sintaxis muy corta y es fácil de modificar a su gusto.
Para hacer las cosas más limpias, use Collection#filter() con Collection#map() , esto funcionará con v12 y v13. Sin embargo, asegúrese de ejecutar Node versión 14 o superior.
const category = args[0].toLowerCase?.() || 'unspecified'; const commandList = message.client.commands .filter(cmd => cmd.category == category) ?.map(cmd => cmd.name) ?.join(', '); if (!commandList) { message.reply(`Commands with category ${category} not found`); } else { message.channel.send(`${category} command list: ${commandList}`); }Para enumerar todos los comandos de una categoría específica en variable, puede usar este código:
if(args[0]) { let list = ` `; let commands = message.client.commands.array(); commands.forEach((cmd) => { if(cmd.category == args[0].toLowerCase()) { list = list.toString() + " **|** " + cmd.name.toString() } }) if (list == ` `) { message.reply("Any commands in category or category does not exist!") } else { list = list.toString() + " **|**" message.channel.send(`${args[0].toLowerCase()} category commands list: ${list}`) } } if(args[0]) { let list = ` `; message.client.commands.each((cmd) => { if(cmd.category == args[0].toLowerCase()) { list = list.toString() + " **|** " + cmd.name.toString() } }) if (list == ` `) { message.reply("Any commands in category or category does not exist!") } else { list = list.toString() + " **|**" message.channel.send(`${args[0].toLowerCase()} category commands list: ${list}`) } }