diff --git a/ChaosBot/Discord/Modules/Admin/Role.cs b/ChaosBot/Discord/Modules/Admin/Role.cs index 2c58de3..1389d88 100644 --- a/ChaosBot/Discord/Modules/Admin/Role.cs +++ b/ChaosBot/Discord/Modules/Admin/Role.cs @@ -131,5 +131,75 @@ namespace ChaosBot.Discord.Modules.Admin Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); } } + + [Command("role remove")] + [CheckCommandPerm("Admin")] + public async Task RoleRemoveCommand(params string[] parameters) + { + try + { + await using (ChaosbotContext dbContext = new ChaosbotContext()) + { + string parameterString = String.Join("", parameters); + + // Fetch last message + IMessage message = (await Context.Channel.GetMessagesAsync(2).FlattenAsync()).Last(); + + // Parse parameters + string pattern = @"(|..)"; + string input = parameterString; + + foreach (Match m in Regex.Matches(input, pattern)) + { + Logger.Info(m.Value); + try + { + IEmote emote; + string emoteString = m.Value; + + if (Emote.TryParse(emoteString, out Emote tempEmote)) + { + if (tempEmote.Animated && Context.Client.CurrentUser.PremiumType != PremiumType.Nitro) + throw new NotSupportedException("No support for animated icons"); + + if (Context.Guild.Emotes.All(e => e.Id != tempEmote.Id) && + Context.Client.CurrentUser.PremiumType != PremiumType.Nitro) + throw new NotSupportedException($"No support for emotes from other servers"); + + emote = tempEmote; + } + else + { + emote = new Emoji(emoteString); + } + + // Delete DB entries + IQueryable roleReactions = dbContext.RoleReactions; + dbContext.RemoveRange(roleReactions + .Where(r => r.DiscordGuildId == Context.Guild.Id) + .ToList() + .Where(r => r.DiscordEmoteName == emote.ToString()) + .Where(r => r.DiscordMessageId == message.Id) + .ToList()); + + // Remove reaction from message + await message.RemoveReactionAsync(emote, Context.Client.CurrentUser); + } + catch (Exception ex) + { + await ReplyAsync($"Something went wrong trying to process {m.Value}: {ex.Message}"); + Logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + await dbContext.SaveChangesAsync(); + } + } + catch (Exception ex) + { + Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } } }