【问题标题】:Discord bot reacts just sometimesDiscord 机器人有时会做出反应
【发布时间】:2020-04-16 17:03:19
【问题描述】:

我最近一直在为我的不和谐机器人上的一个小而易于制作的脚本而苦苦挣扎。该脚本应该使机器人对发布到某个频道的任何图像(并且只有图像)做出反应。问题是机器人有时会做出反应,当它做出反应时它看起来真的很随机​​,脚本从未真正按预期运行,但在某些时候它确实对频道的每张图片都有效,重启后我故意让它再次随机运行。

client.on("message", message => {
if (message.author.bot) return;
let prefix = ';';

if (message.channel.id == showoffid){
  const collector = new Discord.MessageCollector(
    message.channel,
    m => m.author.id === message.author.id,
    {}
  );
  collector.on('collect', message => {
    if (message.attachments.size > 0) {
     message.react('✨')
      .catch(console.error);
      return;
    }
  })
}

if (message.channel.id == hundoid){
  const collector = new Discord.MessageCollector(
    message.channel,
    m => m.author.id === message.author.id,
    {}
  );
  collector.on('collect', message => {
    if (message.attachments.size > 0) {
     message.react('????')
      .catch(console.error);
      return;
    }
  })
}
}

【问题讨论】:

  • 你有什么问题?而且你不需要在那里使用收集器。您的反应是随机的,因为它开始时,有人发送消息,然后发送带有图像的消息。就像拳头消息一样,它是等待带有图像的消息的触发器
  • 好吧,我的问题基本上是为什么它会随机工作或“怎么做才能不让它随机工作”,您刚刚试图回答我的问题,我认为这很明显。好吧,如果我不需要收集器,那么我假设我需要的只是一个简单的“if”语句来检查帖子是否实际上是图像,我认为这是 discord.js 内置的,你就是这样意思是?对吗?

标签: node.js discord discord.js


【解决方案1】:

您可以使用此代码,然后随机停止工作。

client.on("message", message => {
    if (message.author.bot) return;
    let prefix = ';';

    if (message.channel.id == showoffid){
            if (message.attachments.size > 0) {
             message.react('✨')
                .catch(console.error);
                return;
            }
    }

    if (message.channel.id == hundoid){
            if (message.attachments.size > 0) {
             message.react('?')
                .catch(console.error);
                return;
            }
    }
}

【讨论】:

  • 使用工具为我服务,但我并不真正了解它们是如何工作的。感谢您的帮助,目前看来它可以按预期工作。
猜你喜欢
  • 2021-12-18
  • 1970-01-01
  • 2020-07-29
  • 2021-04-30
  • 2021-04-23
  • 2021-10-04
  • 2019-01-15
  • 1970-01-01
  • 2019-11-16
相关资源
最近更新 更多