Discord.js 如何从你的机器人中显示你的服务器成员数

Discord.js How to get show your server member count from your bot

我一直在尝试通过我的机器人显示我的服务器成员数,但我不知道要输入的内容。

const {MessageEmbed} = require("discord.js")
module.exports.run = async (client, message, args) => {
  console.log(message.guild.iconURL())
  const messageEmbed = new MessageEmbed()
  .setTitle(" Server Information ")
  .setThumbnail(message.guild.iconURL())
  .addFields(
    {name: "Server Name:", value: `\`${message.guild.name}\``, inline: true},
    {name: "Server Owner:", value: `${message.guild.owner}`, inline: true},
    {name: "Server Created:", value: `${message.guild.createdAt}`},
    {name: "Emoji count:", value: `${message.guild.emojis.cache.size}`, inline: true},
    {name: "Member count:", value: `${message.guild.members}`, inline: true}
  )
  .setColor("#FFFF66")
  .setFooter("Requested by " + message.author.tag)
  .setTimestamp()

  message.channel.send(messageEmbed)
}


exports.help = {
    name: "serverinfo"
}

我感觉${message.guild.members}不对。

Message has no members property. What you need to do is get the Guild class from Message#guild and access its memberCount 属性.

{name: "Member count:", value: message.guild.memberCount, inline: true}

请注意,您应该在创建客户端时请求 GUILDSGUILD_MEMBERS 意图。例如:

const client = new Discord.Client({
    intents: ['GUILDS', 'GUILD_MEMBERS']
});

要从机器人中过滤成员,您可以使用 Collection#filter:

const members = message.guild.members.cache.filter(member => !member.user.bot);
const bots = message.guild.members.cache.filter(member => member.user.bot);

console.log(`There are ${members.size} members and ${bots.size} bots in this server.`);