Converting DiscordUser to DiscordMember

405 Views Asked by At

i have a problem with dsharp+, so basically when i attach an event Client.VoiceStateUpdated += OnVoiceState;

private Task OnVoiceState(DiscordClient sender, VoiceStateUpdateEventArgs e)
        {
            var member = (DiscordMember) e.After.User;
            Console.WriteLine(member.Nickname);
            return Task.CompletedTask;
}

and try to convert it from DiscordUser to DiscordMember it throws an exception, i know that i can do this from docs
DocsDiscordUser

[Error] Exception occured
System.InvalidCastException: Unable to cast object of type 'DSharpPlus.Entities.DiscordUser' to type 'DSharpPlus.Entities.DiscordMember'.
   at dcBot.Run.Bot.OnVoiceState(DiscordClient sender, VoiceStateUpdateEventArgs e) in C:\Users\lukas\RiderProjects\discord\bot\Run\Main.cs:line 219
   at Emzi0767.Utilities.AsyncEvent`2.InvokeAsync(TSender sender, TArgs e, AsyncEventExceptionMode exceptionMode)

It's working 50% of the time, when i join a channel or leave it throws, but when my friend join a channel it doesn't. What's going on?

2

There are 2 best solutions below

0
On BEST ANSWER

So i managed it to work i needed to add this in DiscordConfiguration:
Intents = DiscordIntents.All
And enable these 2 checks in discord developer:
Checks discord

0
On

I would comment but I don't have enough reputation to do so:

I was able to reproduce the same behaviour myself, I'd suggest opening an issue on the GitHub repository. I'd imagine this being an issue on their part, and if not, they have better knowledge of the expected behaviours and methods.

Hope this helps in the long run.