Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Discord.js allow more words behind the command

I'm trying to create commands, however they only triggers when the certain word is written... !price for example only.. I want it to trigger even after any text is written after the command !price random text bla bla bla I kinda can't figure out as i'm trying to use different file for each command to do not have everything in the main .js file.

// 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
    }
  });

This works fine however if i put anything behind the !price or !ping, they won't trigger.

I tried this also but this is not working for me...

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

My ping.js file looks like this

module.exports = (message) => { // Function with 'message' parameter
    message.reply("Pong!").catch(e => console.log(e));
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I was able to find solution, first define command thanks to our args, then compare at if statement if the command we sent is known.

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
    }
  });

You can also remove the .slice(config.prefix.length) if you do not want to use defined prefix in your config.json, so here we had to add ! before the trigger word.

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
    }
  });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Instead of using the equality operator, you can use String.prototype.startsWith so that you can see if it starts with the command rather than being exactly the command.

var command = (message.content.startsWith("!price") || message.content.startsWith("!ping"))

This will work because a string like !ping abc does start with !ping. However this doesn’t work if it’s in the middle (usually not what people want with bots) like this: abc !ping abc

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda