Discord.js |聊天机器人响应命令名称

时间:2021-06-09 07:15:48

标签: javascript node.js discord.js

所以我试图制作一个聊天机器人,在用户输入前缀和命令名称后发送消息。该命令一般有效,但它似乎也包含命令名称。顺便说一句,我使用命令和事件处理程序。这是它的样子:

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

因此,当人们在此之后没有 arg 的情况下执行 a.chat 时,bot 会响应 To chat, do a.chat <message> 并且当人们将消息放入其中时,它似乎将 a.chat 中的聊天部分作为 { {1}} 也是如此。如何让它忽略 ${message.content} 并只响应它之后的内容?

1 个答案:

答案 0 :(得分:0)

您可以将所有 args 数组项合并为一个句子。

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