简体   繁体   English

Discord.py 机器人在所有公会中仅显示 1 个成员

[英]Discord.py bot only displaying 1 member in all guilds

I have my Discord bot setup to display the member count in its presence, but it is only showing the number 1 for the user count yet I have 2 members in the server that the bot can see.我有我的 Discord 机器人设置以在其存在时显示成员计数,但它只显示用户计数的数字 1,但我在服务器中有 2 个成员可以看到机器人。 I have enabled presence and members intents on the application dashboard.我在应用程序仪表板上启用了状态和成员意图。

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
intents.presences = True
bot = commands.Bot(command_prefix="!", intents=intents, activity=discord.Activity(
    type=discord.ActivityType.watching, name=f"{len([discord.client.Guild.members])} users"), status=discord.Status.do_not_disturb)

bot.run("")

In fact, it will always display 1 no matter how many members it has access to.事实上,无论它可以访问多少个成员,它都将始终显示1

Look at this line:看看这一行:

name=f"{len([discord.client.Guild.members])} users"

This sets the displayed user count to the length of [discord.client.Guild.members] .这会将显示的用户计数设置为[discord.client.Guild.members]的长度。 The length of that is 1, because you just created a new list with all the members being the first element.其长度为 1,因为您刚刚创建了一个新列表,其中所有成员都是第一个元素。


However, discord.client.Guild.members isn't how to get the member count.但是, discord.client.Guild.members不是获取成员数量的方法。 (I don't even think those names exist.) (我什至不认为这些名字存在。)

To properly get all the members in every guild, you need to loop through every guild that the client is in and count each one's members :要正确获取每个公会中的所有成员,您需要遍历客户端所在的每个公会计算每个成员的数量

member_count = sum([guild.member_count for guild in bot.guilds])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM