In the past I've had success with adding roles to a user from the users "reaction on a message". However, recently, this code broke and is returning an error:
A ReactionAdded handler has thrown an unhandled exception.:System.NullReferenceException: Object reference not set to an instance of an object.
Now, I've spent a few days trying to figure this out, combing documentation and I honestly am lost as to how to resolve this, especially as I'm still a novice C# coder. guildUser
is apparently null
but I have no idea how to code it to not be.
I've stripped back many lines of code to simply this. Any help would be awesome.
public async Task GetReactionAddedAsync(Cacheable<IUserMessage, ulong> cachedMessage, ISocketMessageChannel originChannel, SocketReaction reaction)
{
var guild = _client.GetGuild(177962785009958913);
var user = reaction.UserId;
var guildUser = guild.GetUser(user);
var Members = (guildUser as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == ("Members"));
await guildUser.AddRoleAsync(Members);
}