Estoy tratando de crear comandos, sin embargo, solo se activan cuando se escribe la palabra determinada... !price solo por ejemplo... Quiero que se active incluso después de escribir cualquier texto después del comando !price random text bla bla bla I no puedo entenderlo, ya que estoy tratando de usar un archivo diferente para cada comando para no tener todo en el archivo principal .js .
// Require the necessary discord.js classes const { Client, Intents } = require('discord.js'); const config = require("./configtest.json"); const ping = require("./commands/ping.js") const price = require("./commands/price.js") const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); //Commands client.on("messageCreate", (message) => { if (message.author.bot) return; var command = (message.content == "!price" || message.content == "!ping") if(command){ // Check if content is defined command if (message.content.toLowerCase() == "!price" ) command = price if (message.content.toLowerCase() == "!ping" ) command = ping command(message); // Call .reply() on the channel object the message was sent in } }); Esto funciona bien, sin embargo, si pongo algo detrás de !price o !ping , no se activarán.
Intenté esto también, pero esto no funciona para mí ...
client.on("messageCreate", (message) => { if (message.author.bot) return; var command = (message.content == "!price" || message.content == "!ping") if(command){ // Check if content is defined command const args = message.content.slice(config.prefix.length).trim().split(/ +/g); const cmd = args.shift().toLowerCase(); if (cmd === "!price" ) command = price if (cmd === "!ping" ) command = ping command(message); // Call .send() on the channel object the message was sent in } }); Error: command(message); // Call .send() on the channel object the message was sent in ^TypeError: command is not a function
Mi archivo ping.js se ve así
module.exports = (message) => { // Function with 'message' parameter message.reply("Pong!").catch(e => console.log(e)); }Pude encontrar una solución, primero definí el comando gracias a nuestros argumentos, luego comparé la instrucción if si se conoce el comando que enviamos.
client.on("messageCreate", (message) => { if (message.author.bot) return; const args = message.content.slice(config.prefix.length).trim().split(/ +/g); const cmd = args.shift().toLowerCase(); var command = (cmd === "price" || cmd === "ping") if(command){ // Check if content is defined command if (cmd === "price") command = price if (cmd === "ping") command = ping command(message); // Call .send() on the channel object the message was sent in } }); También puede eliminar el .slice(config.prefix.length) si no desea usar el prefijo definido en su config.json , ¡así que aquí tuvimos que agregarlo! antes de la word desencadenante.
client.on("messageCreate", (message) => { if (message.author.bot) return; const args = message.content.trim().split(/ +/g); const cmd = args.shift().toLowerCase(); var command = (cmd === "!price" || cmd === "!ping") if(command){ // Check if content is defined command if (cmd === "!price") command = price if (cmd === "!ping") command = ping command(message); // Call .send() on the channel object the message was sent in } });En lugar de usar el operador de igualdad, puede usar String.prototype.startsWith para que pueda ver si comienza con el comando en lugar de ser exactamente el comando.
var command = (message.content.startsWith("!price") || message.content.startsWith("!ping")) Esto funcionará porque una cadena como !ping abc comienza con !ping . Sin embargo, esto no funciona si está en el medio (generalmente no es lo que la gente quiere con los bots) como este: abc !ping abc