From d1106a8fa4e14fae1040a39624ba392c68bda41b Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Fri, 21 Aug 2020 22:52:08 +0200 Subject: [PATCH] Add event handlers for role reactions --- .../Discord/Services/RoleReactionHandler.cs | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/ChaosBot/Discord/Services/RoleReactionHandler.cs b/ChaosBot/Discord/Services/RoleReactionHandler.cs index 75c0834..39c4802 100644 --- a/ChaosBot/Discord/Services/RoleReactionHandler.cs +++ b/ChaosBot/Discord/Services/RoleReactionHandler.cs @@ -1,18 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ChaosBot.Models; using Discord; using Discord.WebSocket; +using NLog; namespace ChaosBot.Discord.Services { public static class RoleReactionHandler { + private static readonly ILogger Logger = Program.Logger; + public static async void HandleReactionAdded(Cacheable cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) { - // This would grant roles based on database lookup + Optional optionalUser = reaction.User; + if (!optionalUser.IsSpecified) return; + if (!(optionalUser.Value is IGuildUser user)) return; + if (!(socketMessageChannel is SocketGuildChannel channel)) return; + + await using ChaosbotContext dbContext = new ChaosbotContext(); + + IQueryable roleReactionsQueryable = dbContext.RoleReactions; + List roleReactions = roleReactionsQueryable + .Where(r => r.DiscordMessageId == cacheableMessage.Id) + .ToList() + .Where(r => r.DiscordEmoteName == reaction.Emote.ToString()) + .ToList(); + + foreach (RoleReaction roleReaction in roleReactions) + { + try + { + SocketRole role = channel.Guild.Roles.FirstOrDefault(r => r.Id == roleReaction.DiscordRoleId); + await user.AddRoleAsync(role); + } + catch (Exception ex) + { + Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } } public static async void HandleReactionRemoved(Cacheable cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction) { - // This would take roles based on database lookup + Optional optionalUser = reaction.User; + if (!optionalUser.IsSpecified) return; + if (!(optionalUser.Value is IGuildUser user)) return; + if (!(socketMessageChannel is SocketGuildChannel channel)) return; + + await using ChaosbotContext dbContext = new ChaosbotContext(); + + IQueryable roleReactionsQueryable = dbContext.RoleReactions; + List roleReactions = roleReactionsQueryable + .Where(r => r.DiscordMessageId == cacheableMessage.Id) + .ToList() + .Where(r => r.DiscordEmoteName == reaction.Emote.ToString()) + .ToList(); + + foreach (RoleReaction roleReaction in roleReactions) + { + try + { + SocketRole role = channel.Guild.Roles.FirstOrDefault(r => r.Id == roleReaction.DiscordRoleId); + await user.RemoveRoleAsync(role); + } + catch (Exception ex) + { + Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } } } } \ No newline at end of file