InvalidOperationException at DSharpPlus.DiscordClient.UpdateUser
Created by: Mailaender
First I'd like to point out that the https://dsharpplus.github.io/articles/basics/first_bot.html tutorial no longer works. According to the API documentation you need DiscordIntents.GuildMessages
for the message handler to fire.
After I figured that out, I now get a crash that I don't understand. This may be a problem in my code which is based on https://github.com/DSharpPlus/Example-Bots/tree/master/DSPlus.Examples.CSharp.Ex01 so I attached it. I used version 4.0.1 via NuGet.
System.InvalidOperationException: Nullable object must have a value.
at System.Nullable`1.get_Value()
at DSharpPlus.DiscordClient.UpdateUser(DiscordUser usr, Nullable`1 guildId, DiscordGuild guild, TransportMember mbr)
at DSharpPlus.DiscordClient.UpdateMessage(DiscordMessage message, TransportUser author, DiscordGuild guild, TransportMember member)
at DSharpPlus.DiscordClient.PopulateMessageReactionsAndCache(DiscordMessage message, TransportUser author, TransportMember member)
at DSharpPlus.DiscordClient.OnMessageCreateEventAsync(DiscordMessage message, TransportUser author, TransportMember member, TransportUser referenceAuthor, TransportMember referenceMember)
at DSharpPlus.DiscordClient.HandleDispatchAsync(GatewayPayload payload)
at DSharpPlus.DiscordClient.HandleSocketMessageAsync(String data)
at DSharpPlus.DiscordClient.<InternalConnectAsync>g__SocketOnMessage|365_1(IWebSocketClient sender, SocketMessageEventArgs e)
Steps to reproduce
- Connect this bot
- Type
ping
in the chat.
namespace DiscordBot
{
public class Program
{
readonly EventId eventId = new EventId(42, "Bot");
DiscordClient client;
public static void Main(string[] args)
{
var prog = new Program();
prog.RunBotAsync().GetAwaiter().GetResult();
}
public async Task RunBotAsync()
{
var json = "";
using (var fileStream = File.OpenRead("config.json"))
using (var streamReader = new StreamReader(fileStream, new UTF8Encoding(false)))
json = await streamReader.ReadToEndAsync();
var configJson = JsonConvert.DeserializeObject<ConfigJson>(json);
var config = new DiscordConfiguration
{
Token = configJson.Token,
TokenType = TokenType.Bot,
Intents = DiscordIntents.GuildMessages,
AutoReconnect = true,
MinimumLogLevel = LogLevel.Information
};
client = new DiscordClient(config);
client.Ready += ClientReady;
client.GuildAvailable += GuildAvailable;
client.ClientErrored += ClientError;
await client.ConnectAsync();
client.MessageCreated += MessageHandler;
await Task.Delay(-1);
}
Task ClientReady(DiscordClient sender, ReadyEventArgs e)
{
sender.Logger.LogInformation(eventId, "Client is ready to process events.");
return Task.CompletedTask;
}
Task GuildAvailable(DiscordClient sender, GuildCreateEventArgs e)
{
sender.Logger.LogInformation(eventId, $"Guild available: {e.Guild.Name}");
return Task.CompletedTask;
}
Task ClientError(DiscordClient sender, ClientErrorEventArgs e)
{
sender.Logger.LogError(eventId, e.Exception, "Exception occured");
return Task.CompletedTask;
}
Task MessageHandler(DiscordClient sender, MessageCreateEventArgs args)
{
if (args.Message.Content.ToLower().StartsWith("ping"))
args.Message.RespondAsync("pong!");
return Task.CompletedTask;
}
}
public struct ConfigJson
{
[JsonProperty("token")]
public string Token { get; private set; }
[JsonProperty("prefix")]
public string CommandPrefix { get; private set; }
}
}
I believe this is nulling something in library.