Home > other >  Discord.js allow more words behind the command
Discord.js allow more words behind the command

Time:09-23

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

CodePudding user response:

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

CodePudding user response:

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

  • Related