Add role remove command

This commit is contained in:
Daniel_I_Am 2020-08-21 22:15:35 +02:00
parent d748de5a15
commit 0681254476
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84

View File

@ -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 = @"(<a?:\w+:\d+>|..)";
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<RoleReaction> 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}]>.");
}
}
}
}