Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!