bwmarrin / discordgo

(Golang) Go bindings for Discord
BSD 3-Clause "New" or "Revised" License
4.89k stars 787 forks source link

Getting initial member list of guilds #1391

Open delthas opened 1 year ago

delthas commented 1 year ago

Hi, It seems that discordgo does not request Discord for the initial member list, even when the member intent is enabled.

discord, err = discordgo.New("Bot " + cfg.DiscordToken)
// ...
discord.Identify.Intents = discordgo.IntentsAllWithoutPrivileged | discordgo.IntentsGuildMembers
err = discord.Open()
// ...
g, err := discord.State.Guild(c.GuildID)
// ...
// g.Members is empty

This is because Discord only sends the initial member list automatically when both IntentsGuildMembers and IntentsGuildPresences are sent. When the presences intent is not sent, the bot will still receive updates to the member list, but it must request the initial member list explicitly.

I fixed this on my end by adding:

discord.AddHandler(func(s *discordgo.Session, m *discordgo.Ready) {
  for _, g := range s.State.Guilds {
    s.RequestGuildMembers(g.ID, "", 0, "", false)
  }
})

Would it make sense to add this by default in the library? I would say yes. I can create the MR if that's something you would like to see being added.

delthas commented 1 year ago

Source: https://discord.com/developers/docs/topics/gateway-events#request-guild-members

Used to request all members for a guild or a list of guilds. When initially connecting, if you don't have the GUILD_PRESENCES Gateway Intent, or if the guild is over 75k members, it will only send members who are in voice, plus the member for you (the connecting user). Otherwise, if a guild has over large_threshold members (value in the Gateway Identify), it will only send members who are online, have a role, have a nickname, or are in a voice channel, and if it has under large_threshold members, it will send all members. If a client wishes to receive additional members, they need to explicitly request them via this operation. The server will send Guild Members Chunk events in response with up to 1000 members per chunk until all members that match the request have been sent.