【问题标题】:My Discord bot recently won't respond when mentioned我的 Discord 机器人最近在被提及时不会响应
【发布时间】:2020-04-18 19:19:53
【问题描述】:

这是我第一次使用这个网站,如果格式不符合要求,我深表歉意。

问题: 我的 Discord 机器人 (javascript) 最近在@提及时停止响应。没有对代码进行任何更改来导致此问题,并且不久前它工作得非常好。一个机器人程序类似的朋友也有这个问题,所以我知道这不仅仅是我。

该机器人基本上是一个聊天和回复机器人;您@提及它的名称并包含一个触发器,它有随机机会以四个响应之一响应。但是,发生了一些事情,它似乎没有注册它已被@提及,因此没有回复。

例如,如果我输入“@bot hi!”在不和谐中,机器人会回复以下回复之一:“现在还为时过早。”,“早上好。”,“我需要咖啡。”。 “[打哈欠,喃喃的问候]”。

我尝试将client.user.toString() 直接替换为它的客户端标识符以及将在不和谐中使用的标识符(例如;“@name#0000”、“ ") 但那些也被忽略了。我在代码中的这个区域旁边添加了一个箭头。

我不确定是否有更新导致某些代码过时,但我尝试搜索类似问题但没有成功。

我比较确定问题不在于 processChat(receivedMessage) 函数,因为我可以用备用触发器替换指出的问题部分,例如:

if (receivedMessage.content.startsWith("@all ")) {
        processChat(receivedMessage)
}

机器人会发送回复。当被提及时,它似乎根本不想回应;其余代码按应有的方式工作。虽然这是我可以转换的东西,但我已经在我的服务器上安装了这个机器人快一年了,还有多个其他服务器成员需要适应这种变化。我宁愿让事情按照他们过去的方式运行,也不愿让每个人都失去习惯来补偿。

有没有办法解决这个问题?

这是一个有相同问题的小示例代码:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
   console.log("Connected as " + client.user.tag);
})

//The color for all of the bot's messages
messageColor = 4611141;

client.on('message', receivedMessage => {
    // Prevent bot from responding to its own messages
    if (receivedMessage.author == client.user) {
        return;
    }    

    //process regular triggers (@ mentions)
    //This is where the issue seems to be           <--------------------------
    if ((receivedMessage.content.includes(client.user.toString()))) {
        processChat(receivedMessage);
    }    
});

//For usage with chat prompts and triggers
function processChat(receivedMessage) {

    if ((receivedMessage.content.toLowerCase().includes("hi"))){
      var random = randomInt(5, 1) ;
        if (random == 1) {
            receivedMessage.channel.send({embed: {
              color: messageColor,
              description: "It's too early for this."
            }});         
        } else if (random == 2) {
            receivedMessage.channel.send({embed: {
              color: messageColor,
              description: "Mornin'."
            }}); 
        } else if (random == 3) {
            receivedMessage.channel.send({embed: {
              color: messageColor,
              description: "I need coffee."
            }}); 
        } else if (random == 4) {
            receivedMessage.channel.send({embed: {
              color: messageColor,
              description: "*[yawn, mumbled greeting]*"
            }});    
        } else {
           return;
        }
    } 
}


//Random number generation
function randomInt(max, min) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

client.on('error', console.error);

client.login(token)

【问题讨论】:

  • 你给你的机器人起个昵称了吗? mention format changes 在这种情况下,所以 user.toString() 将不正确。如果您想正确检查提及有很多other questions 可以回答这个问题。
  • 机器人没有昵称。我不久前就发现了这个问题,所以我没有搞砸它。但是,感谢您提供该链接。它很好地回答了我的问题;我想我没有使用完全正确的标签进行搜索。
  • 我已回滚您的编辑。此处不适合在问题标题中添加 [SOLVED] 或在问题中编辑解决方案。如果您在此处收到解决问题的答案,您可以将其标记为已接受以表明已接受。如果您自己找到了解决方案并想分享它,您可以在下面为该用途提供的空白处写下答案(有关更多信息,请参阅Can I answer my own question?)。

标签: triggers botframework discord discord.js mention


【解决方案1】:

切换

if ((receivedMessage.content.includes(client.user.toString()))) {
        processChat(receivedMessage);
    }    

if (recievedMessage.isMemberMentioned(client.user)) {
        processChat(recievedMessage)
    }

解决了这个问题。谢谢,Bauke,提供这些链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2019-12-21
    • 2021-04-09
    • 2020-12-28
    • 2018-08-17
    • 1970-01-01
    • 2021-07-22
    相关资源
    最近更新 更多