首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Discord.js \ Chatbot响应命令名

Discord.js \ Chatbot响应命令名
EN

Stack Overflow用户
提问于 2021-06-09 07:15:48
回答 1查看 309关注 0票数 0

因此,我试图制作一个聊天机器人,在用户输入前缀和命令名之后发送消息。该命令一般起作用,但它似乎也采用了命令名。我使用的是命令和事件处理程序。看上去是这样的:

代码语言:javascript
运行
复制
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中的聊天部分作为${message.content}来处理。我如何使它能够忽略a.chat,并且只对它之后的事情做出反应?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-09 07:42:46

您可以将所有args数组项加入到一个句子中。

代码语言:javascript
运行
复制
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)
       })
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67899296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档