Search code examples
c#discorddsharp+

Converting DiscordUser to DiscordMember


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?


Solution

  • 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