此信息命令有什么错误?

时间:2020-10-22 15:38:01

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

我当前正在执行信息命令,但无法正常工作。 这是输入:

bot.on('message', message => {
    const members = message.guild.members.cache;
    var onlineMembers = members.filter(member => member.presence.status === 'online').size + members.filter(member => member.presence.status === 'idle').size + members.filter(member => member.presence.status === 'dnd').size
    var serverIcon = message.guild.iconURL();
    const embedguild = new Discord.MessageEmbed()
    .setColor('RANDOM')
    .setTitle(`${message.guild} (${message.guild.id})\n`)
    .setAuthor('Server Info\n')
    .setThumbnail(serverIcon)
    .setFooter(`Requested by ${message.author.tag}`)
    .addFields(
        { name: 'Owner:', value: `**${message.guild.owner} (${message.guild.ownerID})**`},
        { name: 'Members:', value: `**${message.guild.memberCount}**` },
        { name: 'Online Members:', value: `**${onlineMembers}**` },
        { name: 'Emojis:', value: `**${message.guild.emojis.cache.size}**`},
        { name: 'Roles:', value: `**${message.guild.roles.cache.size}**`},
    )

        if(message.content === `${prefix}serverinfo`){
        message.channel.send(embedguild);
        }})

,这是输出: TypeError: Cannot read property 'members' of null

1 个答案:

答案 0 :(得分:0)

错误Cannot read property 'members' of null表示.members之前的变量为空。在这种情况下,message.guild为null并导致错误。

根据official documentation, 如果邮件未发送到公会频道,则message.guild将是null。 使用它之前,您必须检查它是否为空。

bot.on('message', message => {
    if (message.guild == null) {
      // handle when the message is NOT sent in a guild channel
      // ...
      return;
    }

    // `message.guild` is surely not null
    const members = message.guild.members.cache; // OK!
    
    // ...
});