问题
For some reason I cannot get any members anymore from the guild the bot is in.
To make sure I have the users, I construct it with
DiscordSocketConfig config = new DiscordSocketConfig { AlwaysDownloadUsers = true };
client = new DiscordSocketClient(config);
The event I respond to is long after it has connected. It will pick up messages from the channels themselves in the guild the bot is in, so it's connected, working, and can see all of the members.
I go through all the members and I'm trying to print them out with some debug code like:
Console.WriteLine("Users:");
foreach (IGuildUser? u in Guild.GetUsersAsync().Result)
Console.WriteLine($" {u.Username} ({u.Id})");
and it only prints itself.
The way I'm using this in my code is like:
Guild.GetUserAsync(user.Id) // user is valid, this is empty for anyone who is not the bot
The private field _members
on the guild object has 1 element (which is itself, the bot), but all the other things (ex: _emojis
, _channels
, any other cached thing) are all fully populated. The bot can talk in channels and receive messages in channels with all the users as well, so I have no idea why it cannot get all the members.
What confuses me the most is that this was all working, and then magically stopped working overnight. The guild has been over 100 members for a while, so I don't understand why it's suddenly failing now.
Most importantly, the bot has all the roles that I have, so there's no way it's magically unable to see the members. I can rule out any visual problems for the bot.
Any way I can debug this? Am I missing something? How can I get all the members of a guild to work again?
回答1:
This is a result of Discord enforcing intents.
Intents allow you to opt-in to certain gateway events. Currently, the GUILD_MEMBERS and GUILD_PRESENCES intents are "privileged intents" and have the following effects:
- GUILD_MEMBER_[ADD|UPDATE|REMOVE] requires the GUILD_MEMBERS intent to be received
- PRESENCE_UPDATE requires the GUILD_PRESENCES intent to be received
- REQUEST_GUILD_MEMBERS
- requires GUILD_PRESENCES intent to set presences=true
- requires GUILD_MEMBERS intent to request the whole list (query='', limit=0<=n)
- is limited to requesting 1 guild_id
- is limited to 100 members when using a prefix (query='prefix', limit=1<=n<=100)
- GUILD_CREATE requires GUILD_PRESENCES intent to contain more than just the bot and voice members
- List Guild Members requires GUILD_MEMBERS intent to access
Privileged intents are disabled by default. To enable them:
- Visit https://discord.com/developers/applications
- Click on the bot where you wish to enable privileged intents
- Navigate to the "Bot" tab on the left
- Scroll to the Privileged Gateway Intents section
- Enable your desired intents
If your bot is in over 100 guilds, you will need to verify your bot: https://dis.gd/bot-verification. If your bot is already verified, but need to request intents permission, contact Discord support: https://dis.gd/contact.
来源:https://stackoverflow.com/questions/64566016/guild-members-are-not-populated-anymore-cannot-getuserasync-as-it-only-returns