【问题标题】:cannot read property 'count' of undefined discord.js无法读取未定义 discord.js 的属性“计数”
【发布时间】:2020-10-23 03:45:08
【问题描述】:

我使用的是 discord.js 的第 12 版,我正在制作一个赠品命令。

let embed = new Discord.MessageEmbed()
            .setTitle('Giveaway!')
            .setAuthor('Hosted by ' + message.author.username, message.author.avatarURL())
            .setDescription('The prize is **' + prize + '**!')
            .setTimestamp(Date.now() + ms(args[1]))
            .setColor("BLUE")
            let m = await channel.send(embed)
            m.react("????")
            setTimeout(() => {
                if (m.reactions.cache.get("????").count <= 1) {
                  message.channel.send(`Reactions: ${m.reactions.cache.get("????").count}`);
                  return err('Not enough people reacted!')
                }

这是我的代码,我得到这个错误:

                if (m.reactions.cache.get("????").count <= 1) {
                                               ^

TypeError: Cannot read property 'count' of undefined
    at Timeout._onTimeout (C:\Users\abhir\Downloads\Tada!\index.js:38:48)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

详情:

操作系统:Windows Home 64 位 Node.JS 版本:12 Discord.JS 版本:12.0.0

【问题讨论】:

  • 变量m 是一个新创建的消息对象,它在某个通道中发送。您还没有编写任何检索反应的逻辑。您正在引用缓存,但那是旧的。在创建消息时,它没有任何反应。因此,当您尝试找到它时,它不会返回任何内容。
  • 虽然我有一个 setTimeout(),所以 1 分钟后它应该只会触发。
  • 没关系。变量m 是创建消息时的消息对象。它没有任何反应。您需要刷新该变量以反映其当前状态。这是一个协程,您没有在代码中执行另一个 await 语句。我对 discord.js 不太熟悉,因此无法为您提供完整的解决方案,但我知道您至少缺少另一个 await 语句,该语句从特定消息中检索当前反应。
  • 我试过了,还是不行。

标签: javascript caching discord discord.js typeerror


【解决方案1】:

您应该尝试在超时完成后重新获取消息:

let m = await channel.send(embed);
m.react("?");
            
setTimeout(() => {
    const message = channel.messages.cache.get(m.id);
    const reactions = message.reactions.cache.get("?");

    if (!reactions) return message.channel.send('No reactions found.');

    if (reactions.count <= 1) {
        message.channel.send(`Reactions: ${reactions.count}`);
        return err('Not enough people reacted!');
    }
    // Do your stuff

希望这能解决您的问题:)

【讨论】:

  • 现在,无论如何,它总是说“没有发现任何反应”:(
  • @Aawesome 您应该尝试记录 message.reactions.cache 以确保那里一切正常
  • 这可能是错误的方法(它的 v12 discord.js 顺便说一句)
  • 它成功了!
  • @Aawesome 很高兴我能帮上忙! :3
猜你喜欢
  • 2021-04-09
  • 2022-06-10
  • 2021-06-27
  • 2020-07-20
  • 1970-01-01
  • 2021-05-17
  • 2020-12-05
  • 2021-04-14
  • 2021-11-17
相关资源
最近更新 更多