Discord.js多服务器成员统计命令需求:现有代码仅支持单服务器统计
Global Member Count Command for Discord.js
Got it, let's adjust your existing code to track member stats across all servers your bot is connected to, instead of just the one where the command is run. Here's a revised version with explanations:
Modified Code
if(message.content.startsWith("?membercount")) { // Initialize counters to track totals across all guilds let totalMembers = 0; let totalHumans = 0; let totalBots = 0; let totalOnline = 0; let totalIdle = 0; let totalDnd = 0; let totalOffline = 0; let totalStreaming = 0; // Loop through every server (guild) the bot is in message.client.guilds.cache.forEach(guild => { // Add the current guild's total members to our global count totalMembers += guild.memberCount; // Count humans in this guild and add to the global total const humanCount = guild.members.cache.filter(member => !member.user.bot).size; totalHumans += humanCount; // Count bots in this guild and add to the global total const botCount = guild.members.cache.filter(member => member.user.bot).size; totalBots += botCount; // Track presence statuses globally (optional chaining avoids null errors) totalOnline += guild.members.cache.filter(o => o.presence?.status === 'online').size; totalIdle += guild.members.cache.filter(i => i.presence?.status === 'idle').size; totalDnd += guild.members.cache.filter(dnd => dnd.presence?.status === 'dnd').size; totalOffline += guild.members.cache.filter(off => off.presence?.status === 'offline').size; totalStreaming += guild.members.cache.filter(s => s.presence?.status === 'streaming').size; }); // Build the embed with global stats (works for v11 and v12+ Discord.js) const embed = new (Discord.MessageEmbed || Discord.RichEmbed)() .setTitle("**Bot Name - Global Member Stats**") .setColor('#0099ff') .addField('Total Members (All Servers)', `**${totalMembers}**`, true) .addBlankField(true) .addField('Total Humans', `**${totalHumans}**`, true) .addField('Total Bots', `**${totalBots}**`, true) .addField('Global Presence Status', `**${totalOnline}** Online\n` + `**${totalIdle}** Inactive\n` + `**${totalDnd}** Do Not Disturb\n` + `**${totalOffline}** Offline\n` + `**${totalStreaming}** Streaming` ) .setFooter(`© footername`); message.channel.send({ embed }); }
Key Notes to Keep in Mind
- Discord.js Version Tweaks:
- If you're on v11, replace
guilds.cachewithguildsandmembers.cachewithmembers. - For older JavaScript environments, swap
presence?.statuswithpresence && presence.statusto avoid syntax errors.
- If you're on v11, replace
- Intents for v14+: Make sure your bot has the
GUILD_MEMBERSintent enabled in the Discord Developer Portal and your bot code—without it, the member cache will be incomplete, leading to inaccurate counts. - Large Server Accuracy: If your bot is in huge servers, the cache might not include all members. For full accuracy, you could use
guild.members.fetch()to load all members, but be mindful of Discord's API rate limits to avoid getting blocked.
内容的提问来源于stack exchange,提问作者ItsLars




