【问题标题】:TypeError: Cannot read property 'cache' of undefined discord.jsTypeError:无法读取未定义 discord.js 的属性“缓存”
【发布时间】:2021-03-31 12:43:39
【问题描述】:

嗨,当我试图让它发布服务器计数和它返回的其他内容时,我遇到了一些带有缓存的机器人

    B:\stuff\Downloads\Admeeeeeen bot\src\commands\info\stats.js:27
        { name: 'Server Count:', value: `${client.guilds.cache.size}`, inline: true },
                                                         ^
TypeError: Cannot read property 'cache' of undefined

 run(message, client, guild) {
    const d = moment.duration(message.client.uptime);
    const days = (d.days() == 1) ? `${d.days()} day` : `${d.days()} days`;
    const hours = (d.hours() == 1) ? `${d.hours()} hour` : `${d.hours()} hours`;
    const minutes = (d.minutes() == 1) ? `${d.minutes()} minute` : `${d.minutes()} minutes`;
    const seconds = (d.seconds() == 1) ? `${d.seconds()} second` : `${d.seconds()} seconds`;
    const prefix = message.client.db.settings.selectPrefix.pluck().get(message.guild.id);
    
    const embed = new MessageEmbed()
    .setTitle('__Admeeeeeen bot\'s Bot Information__')
    .addFields(
        { name: 'Server Count:', value: `${client.guilds.cache.size}`, inline: true },
        { name: 'Uptime:', value: `${days}, ${hours}, ${minutes}, and ${seconds}`, inline: true },
    )
      .setThumbnail('https://imgur.com/aaX76bI.png')
      .setFooter(message.member.displayName,  message.author.displayAvatarURL({ dynamic: true }))
      .setTimestamp()
      .setColor(message.guild.me.displayHexColor);
    message.channel.send(embed);
    console.log(guild) 

  }
}

【问题讨论】:

  • 你能登录client.guilds看看你得到了什么
  • 返回同样的错误
  • client 必须不是有效的 Discord 客户端对象,然后确保您的 run() 参数顺序和大小在整个文件中保持不变

标签: discord.js


【解决方案1】:

您已将 client 定义为第二个参数,请尝试使用 message.client.guilds.cache.size,并且您不需要额外的参数,那么代码将是这样的

run(message) {
   .
   { name: 'Server Count:', value: `${message.client.guilds.cache.size}`, inline: true },
   .
}

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 2020-07-23
    • 2021-04-19
    • 2021-03-15
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2023-01-14
    • 2020-07-14
    相关资源
    最近更新 更多