Fix some unchecked access in the giveRole function in the reactionListener that would crash the bot if an expected value wasn't around

This commit is contained in:
Daan Boerlage 2022-07-19 16:22:46 +02:00
parent 7cc9fc92d9
commit fdd23ad00f
Signed by: daan
GPG key ID: FCE070E1E4956606

View file

@ -70,7 +70,10 @@ namespace Geekbot.Core.ReactionListener
public async void GiveRole(ISocketMessageChannel channel, SocketReaction reaction) public async void GiveRole(ISocketMessageChannel channel, SocketReaction reaction)
{ {
var roleId = _listener[reaction.MessageId][reaction.Emote]; _listener.TryGetValue(reaction.MessageId, out var registeredReactions);
if (registeredReactions == null) return;
if (!registeredReactions.ContainsKey(reaction.Emote)) return;
var roleId = registeredReactions[reaction.Emote];
var guild = (SocketGuildChannel) channel; var guild = (SocketGuildChannel) channel;
var role = guild.Guild.GetRole(roleId); var role = guild.Guild.GetRole(roleId);
await ((IGuildUser) reaction.User.Value).AddRoleAsync(role); await ((IGuildUser) reaction.User.Value).AddRoleAsync(role);