bwmarrin / discordgo

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

Crash when getting GuildMember #1361

Closed fklingenberg closed 1 year ago

fklingenberg commented 1 year ago

I'm trying to get a Guild Member. This is the code im using:

type RoleApi struct {
    discord *discordgo.Session
    cfg     *core.Config
}

func NewRoleApi(ctx *core.AppContext) *RoleApi {
    return &RoleApi{
        discord: ctx.Discord(),
        cfg:     ctx.Config(),
    }
}

func (api *RoleApi) HasRole(userId string, roleId string) bool {
    member, err := api.discord.GuildMember(api.cfg.Server.ServerId, userId)
    if err != nil {
        logrus.Error(err)
        return false
    }

    for _, r := range member.Roles {
        if r == roleId {
            return true
        }
    }

    return false
}

Whenever I'm trying to call the function i got this crash: 3p4NAaE

After calling discordgo.New im doing this:

discord, err := discordgo.New(fmt.Sprintf("Bot %v", conf.Token))
if err != nil {
    return nil, err
}
discord.Identify.Intents = discordgo.IntentsAll

In the discord developer portal i enabled the SERVER MEMBERS INTENT PUVA4Ca

Anyone maybe got the same problem who can tell me how to fix it? Thanks

FedorLap2006 commented 1 year ago

I'm not sure how this related to GuildMember, since the code seems to fail at handler.go:26. Can you provide a code snippet from that location?

fklingenberg commented 1 year ago

I'm not sure how this related to GuildMember, since the code seems to fail at handler.go:26. Can you provide a code snippet from that location?

Sure, this is the part where I'm calling my RoleApi

role := api.NewRoleApi(rr.ctx)

if role.HasRole(userId, roleId) { // This is line 26
    err := s.GuildMemberRoleRemove(serverId, userId, roleId)
    if err != nil {
        logrus.Error(err)
    }
} else {
    err := s.GuildMemberRoleAdd(serverId, userId, roleId)
    if err != nil {
        logrus.Error(err)
    }
}
Nv7-GitHub commented 1 year ago

Where are userId and roleId defined? Also, are you sure that this is line 26? The stack trace says the error isn't coming from within the function

fklingenberg commented 1 year ago

userId and roleId are variables which are hardcoded a few lines above. Yes, this is line 26.

I have already tried to call discord.GuildMember(api.cfg.Server.ServerId, userId) directly, without the RoleApi. It always gives the same error as soon as the GuildMember method is used.

Therefore, I suspect that it may be a bug in DiscordGo.