Add event handlers for role reactions
This commit is contained in:
parent
b80eb4767c
commit
d1106a8fa4
@ -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<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
|
||||
{
|
||||
// This would grant roles based on database lookup
|
||||
Optional<IUser> 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<RoleReaction> roleReactionsQueryable = dbContext.RoleReactions;
|
||||
List<RoleReaction> 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<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
|
||||
{
|
||||
// This would take roles based on database lookup
|
||||
Optional<IUser> 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<RoleReaction> roleReactionsQueryable = dbContext.RoleReactions;
|
||||
List<RoleReaction> 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}]>.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user