Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda