Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IUser, SocketUser and SocketGuildUser occasionally randomly null

See original GitHub issue

I am using Discord .NET 1.0.2 and .NET Framework 4.6.1 on Visual Studio 2017 Community

It seems that at random, occasionally, a user will stop existing as far as Discord .NET/The Discord API is concerned.

Here are a few example debugging commands:

public async Task TestCommand1(IUser user)
    await Context.Channel.SendMessageAsync($"Your mention is: {user.Mention}, your ID is: {user.Id}");
public async Task TestCommand2(SocketUser user)
    await Context.Channel.SendMessageAsync($"Your mention is: {user.Mention}, your ID is: {user.Id}");
public async Task TestCommand3(SocketGuildUser user)
    await Context.Channel.SendMessageAsync($"Your mention is: {user.Mention}, your ID is: {user.Id}");

And here is where the commands are handled (Which is more or less the default command handler method provided in the Discord .NET tutorial, I added && !message.Content.ToCharArray().All(c => char.IsSymbol(c) || char.IsPunctuation(c)) to prevent the bot from spewing out: “Unknown command” when a user types something like “!?!”):

private async Task HandleCommand(SocketMessage messageParam)
    // Don't process the command if it was a System Message
    var message = messageParam as SocketUserMessage;
    if (message == null) return;
    // Create a number to track where the prefix ends and the command begins
    int argPos = 0;
    // Determine if the message is a command, based on if it starts with '/' or a mention prefix
    if (!(message.HasCharPrefix(CommandPrefix, ref argPos) || message.HasMentionPrefix(Client.CurrentUser, ref argPos))) return;
    // Create a Command Context
    var context = new CommandContext(Client, message);
    // Execute the command. (result does not indicate a return value, 
    // rather an object stating if the command executed succesfully)
    var result = await CommandService.ExecuteAsync(context, argPos);

    if (!result.IsSuccess && !message.Content.ToCharArray().All(c => char.IsSymbol(c) || char.IsPunctuation(c)))
        await context.Channel.SendMessageAsync(result.ErrorReason);

It works fine for most users, but there is a small percentage (that is, by the way, persistent, as in, the same users will always get impacted randomly) that suffer from not being found at all by the bot.

In this scenario, for privacy purposes, we’re gonna have @User01#0001 and @User02#0002. User02#0002 gets frequently impacted with this issue, and thus cannot use many of the bot commands at all.

Let’s run the debug commands.

> !test1 @User01#0001

Which returns as expected: Your mention is: @User01#0001, your ID is: XXXX “XXXX” being the correct user’s ID.

Now let’s run it for the other user who happens to be impacted at this time.

> !test2 @User02#0001

Which returns: “User not found”, yet, the user is in the same guild the bot is and is present and a proper discord account.

It seems that it hits an exception that indeed, it cannot find the user, thus the command is not ran at all… The exact same issue happens with !test2 and !test3 commands, so it is not a type issue.

The same issue happens when manually finding the user by ID, with either DiscordSocketClient.GetUser(ID) and SocketGuild.GetUser(ID), despite said user being present in the same server, it returns null.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

Still34commented, Apr 18, 2018

@AshkoreDracson Try toggling the AlwaysDownloadUser under the DiscordSocketConfig. This will attempt to fetch the entire user collection when GuildAvailable is fired.

Also, just a heads up that a similar issue regarding WS user fetching backup via REST is being tracked in #1015.

foxbotcommented, Jul 3, 2018

This is correctly resolved via 8fb2c71814fad9bcab1888fb8d66d693cc98a4b1.

Discord now includes complete member objects in the MESSAGE_CREATE payload, so we now add that member to the cache when handling the dispatch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Discord.NET Trying to get SocketUser as SocketGuildUser ...
userToModCheck returns the correct username and user ID, but as a SocketUnknownUser. userToMod returns null. So far, I have tried: Checking the ...
Read more >
Discord.NET Trying to get SocketUser as SocketGuildUser ...
Coding example for the question Discord.NET Trying to get SocketUser as SocketGuildUser returning null-C#.
Read more >
Make a Discord Music Bot (C# Discord.Net) - YouTube
This is a guide on how to create your very own Music playing Discord bot.
Read more >
Class SocketGuildUser
A string representing the display name of the user; If the nickname is null, this will be the username. | Improve this Doc...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found