【问题标题】:Discord.js | Chatbot responds to command name不和谐.js |聊天机器人响应命令名称
【发布时间】:2021-08-26 04:06:57
【问题描述】:

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

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 时,机器人将响应 To chat, do a.chat &lt;message&gt; 并且当人们将消息放在那里时,它似乎也将 a.chat 中的聊天部分作为 ${message.content} 进行。我怎样才能让它忽略a.chat并只响应它之后的东西?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您可以将所有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)
           })
      }
    }
    

    【讨论】:

    • 嘿,它成功了,谢谢!我对 js 很陌生,所以是的,哈哈
    • 不客气!随便问什么
    猜你喜欢
    • 2021-06-17
    • 2022-10-13
    • 2019-02-04
    • 2018-07-21
    • 2018-09-18
    • 1970-01-01
    • 2021-11-21
    • 2018-11-10
    • 2017-05-31
    相关资源
    最近更新 更多