繁体   English   中英

Discord Bot回复自己,不承认message.author.bot

[英]Discord Bot replying to itself, doesn't recognize message.author.bot

所以我有一个机器人,我正试图用“我们爱丹!”回应“丹”。 对于我所在的服务器:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var mybot = new Discord.Client({
    token: auth.token,
    autorun: true
});
mybot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(mybot.username + ' - (' + mybot.id + ')');
});
mybot.on('message', function (user, userID, channelID, message, evt) {
    if (message.author.bot) return;
    if (message.includes("Dan")) {
            mybot.sendMessage({
                to: channelID,
                message: 'We love Dan!',
            });
     }
});

我已经查找了如何让机器人不响应自己,许多人提出了这行代码

if (message.author.bot) return;

但是当我把它放入时,我在命令提示符中收到此错误:

TypeError:无法读取未定义的属性'bot'

它似乎来自像discord.js这样的线程,它回答自己解决方案适用于人,但它不适合我。

我的做法有何不同?

谢谢!

该错误告诉您message.author undefined ,因此bot不是有效属性。 您正在尝试使用另一个discord库中的解决方案。

相反,尝试检查用户或用户ID是否与机器人匹配,然后返回。

嗯......乍一看看起来像一个范围问题。 不确定你正在构建什么,你可能想要阅读MCV 需要清理我构建的小机器人并将其发布在github上,明天我会发布它。 正在试验短期记忆阵列。 但通过快速查看和测试得到了这个工作:

const Discord = require("discord.js");
const client = new Discord.Client();
...
client.on("message", async message => {
  if (message.content.includes("Dan")){
    // right that is needed to kill the loop ->
    if(message.author.bot) return; 
    return message.reply("We love you Dan!");
    // could also use -> message.channel.send("We love you Dan!")
  } 
}

在我的情况下client是你的bot我相信它会回复异步或返回所以你只需要调用作为方法的一部分存在的内置原型方法。 API Docs中还有很多其他内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM