【问题标题】:Discord.js: The bot doesn't see messageDiscord.js:机器人看不到消息
【发布时间】:2021-12-13 10:17:22
【问题描述】:

我一直在研究快速数学命令,它的工作原理是这样的:我执行它,它在聊天中发送一个问题,发送正确答案的人获胜。

但是如你所见,它没有看到我的 46 消息,忽略它,并在 30 秒后发送“无人接听!”

我的代码是:

const { MessageEmbed } = require('discord.js');
const Discord = require("discord.js")

module.exports = {
  name: "game",
  description: "smth",
  aliases: ["gamestart", "startgame"],
  permissions: ["MANAGE_MESSAGES"],
  timeout: 10000,
execute: async (client, message, args) => {
    var a = Math.floor(Math.random() * 50) + 1
    var b = Math.floor(Math.random() * 50) + 1
    var ab = await a + b
    console.log(ab)
    let embed = new MessageEmbed()
    .setTitle(":chart_with_downwards_trend: Quick Math")
    .setDescription(`Solve the problem below and get XP.\n\n\`${a}\` + \`${b}\` = **???**`)
    .setFooter("123")

    const msg = await message.channel.send(embed)

    try {
      let filter = (m) => {
        if (m.author.bot) return;
        if (m.channel.id == msg.channel.id && m.content === ab) return true;
        else {
          return false;
        }
      };
      let res = await msg.channel.awaitMessages(filter, {
        time: 30000,
        errors: ["time"],
      });
      if (res) {
        let embed = new MessageEmbed()
        .setTitle(":chart_with_downwards_trend: Quick Math")
        .setDescription(`The correct answer was \`${ab}\`\n\nThe winner is **you**!`)
        .setFooter("123")
        msg.channel.send(embed)
      }
    } catch (err) {
      console.log(err)
      msg.channel.send(
        "No one answered!"
      );
    }
}
}

控制台没有报错,看起来像这样: 46

这真的很奇怪,我请求你的帮助。提前致谢。

更新:(将 === 更改为 == 之后)

控制台日志:

49
Collection [Map] {
  '903319414777188413' => ExtendedMessage {
    channel: ExtendedTextChannel {
      type: 'text',
      deleted: false,
      id: '859482526364336160',
      name: 'rice-bot-testing',
      rawPosition: 37,
      parentID: '836281677994786846',
      permissionOverwrites: [Collection [Map]],
      topic: null,
      nsfw: false,
      lastMessageID: '903319414777188413',
      rateLimitPerUser: 5,
      lastPinTimestamp: 1624987887000,
      guild: [Guild],
      messages: [MessageManager],
      _typing: Map {}
    },
    deleted: false,
    id: '903319414777188413',
    type: 'DEFAULT',
    system: false,
    content: '49',
    author: User {
      id: '713008151661772812',
      system: null,
      locale: null,
      flags: [UserFlags],
      username: '8less',
      bot: false,
      discriminator: '8719',
      avatar: 'fb2c5843579f14c38b542349cfa5a256',
      lastMessageID: '903319414777188413',
      lastMessageChannelID: '859482526364336160'
    },
    pinned: false,
    tts: false,
    nonce: '903319413787197440',
    embeds: [],
    attachments: Collection [Map] {},
    createdTimestamp: 1635438531346,
    editedTimestamp: 0,
    reactions: ReactionManager {
      cacheType: [class Collection extends Collection],
      cache: Collection [Map] {},
      message: [Circular]
    },
    mentions: MessageMentions {
      everyone: false,
      users: Collection [Map] {},
      roles: Collection [Map] {},
      _members: Collection [Map] {},
      _channels: Collection [Map] {},
      crosspostedChannels: Collection [Map] {}
    },
    webhookID: null,
    application: null,
    activity: null,
    _edits: [],
    flags: MessageFlags { bitfield: 0 },
    reference: null,
    components: []
  }
}

确保在 30000 毫秒后记录 Collection [Map] {} 部分。

【问题讨论】:

标签: javascript node.js discord discord.js bots


【解决方案1】:

猜猜问题出在这一行:

if (m.channel.id == msg.channel.id && m.content === ab) return true;

您正在搜索的消息的内容(当然是字符串)严格等于您之前计算的结果(数字)并且=== 不仅检查变量的内容,它还会检查还有变量的类型,所以:'46' === 46 将返回 false。 你可以做的是:

此外,我更喜欢使用消息的cleanContent 属性,以确保仅检索消息的有效内容而不更改它。 希望对您有所帮助:D

编辑: 刚刚看到您导入了两次 discord.js 库,而不是这样做,您只需从 Discord 模块中提取 MessageEmbed:

const Discord = require('discord.js');
const { MessageEmbed } = Discord;

【讨论】:

  • 对不起,我很忙,我可以看到awaitMessages 方法的返回值是一个映射而不是一个对象。猜猜你应该写:const res = await msg.channel.awaitMessages(/* stuff */),然后访问使用res.get() 过滤的消息。当然不仅如此,返回值是一个法线贴图,因此您将能够使用贴图的所有标准方法。以防万一您不知道地图是key - value 对的集合,您可以通过map.get(key) 访问值。它还支持循环。希望对您有所帮助
  • 此外,如果您只想要与过滤器检查匹配的第一条消息,您可以使用map.first() 方法来获取第一个值,该值也应该是与您的条件匹配的第一条消息
猜你喜欢
  • 1970-01-01
  • 2018-06-05
  • 2021-02-01
  • 2021-05-08
  • 2020-09-08
  • 2021-10-19
  • 2021-07-13
  • 2020-07-18
  • 2020-08-27
相关资源
最近更新 更多