Skip to content

Discord.js | Chatbot responds to command name

so I’m trying to make a chatbot that sends the message after the user types the prefix and the command name. The command works in general but it seems to also take in the command name. I use a command and event handler btw. This is what it looks like:

const fetch = require("node-fetch").default;

module.exports = {
   name: 'chat',
   description: "chat command",
   execute(client, message, args){

       if(!args[0]) return message.reply("To chat, do a.chat <message>");
       fetch(`https://api.monkedev.com/fun/chat?msg=${message.content}&uid=${message.author.id}`)
       .then(response => response.json())
       .then(data => {
           message.channel.send(data.response)
       })
  }
}

So when people do a.chat without an arg after that, bot will respond To chat, do a.chat <message> and when people put the message in there it seems to take the chat part in a.chat as a ${message.content} as well. How do I make it so it will ignore a.chat and respond to only the things after it?

Answer

You can join all args array items into one sentence.

const fetch = require("node-fetch").default;

module.exports = {
   name: 'chat',
   description: "chat command",
   execute(client, message, args){
       const content = args.join(" ");
       if(!content) return message.reply("To chat, do a.chat <message>");
       fetch(`https://api.monkedev.com/fun/chat?msg=${content}&uid=${message.author.id}`)
       .then(response => response.json())
       .then(data => {
           message.channel.send(data.response)
       })
  }
}