From 2eca82e8994e47608e2432c59ac0a4650f660b7d Mon Sep 17 00:00:00 2001 From: Runebaas Date: Thu, 19 Oct 2017 21:43:37 +0200 Subject: [PATCH] Add role self service commands --- Geekbot.net/Commands/Role.cs | 122 ++++++++++++++++++++++++++++++++ Geekbot.net/Lib/ErrorHandler.cs | 15 ++-- 2 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 Geekbot.net/Commands/Role.cs diff --git a/Geekbot.net/Commands/Role.cs b/Geekbot.net/Commands/Role.cs new file mode 100644 index 0000000..2e1939e --- /dev/null +++ b/Geekbot.net/Commands/Role.cs @@ -0,0 +1,122 @@ +using System; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AngleSharp; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using StackExchange.Redis; + +namespace Geekbot.net.Commands +{ + [Group("role")] + public class Role : ModuleBase + { + private readonly IErrorHandler _errorHandler; + private readonly IDatabase _redis; + + public Role(IErrorHandler errorHandler, IDatabase redis) + { + _errorHandler = errorHandler; + _redis = redis; + } + + [Command(RunMode = RunMode.Async)] + [Remarks(CommandCategories.Helpers)] + [Summary("Get a list of all available roles.")] + public async Task getAllRoles() + { + try + { + var roles = _redis.HashGetAll($"{Context.Guild.Id}:RoleWhitelist"); + if (roles.Length == 0) + { + await ReplyAsync("There are no roles configured for this server"); + return; + } + var sb = new StringBuilder(); + sb.AppendLine($"**Self Service Roles on {Context.Guild.Name}**"); + sb.AppendLine("To get a role, use `!role name`"); + foreach (var role in roles) + { + sb.AppendLine($"- {role.Name}"); + } + await ReplyAsync(sb.ToString()); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [Command(RunMode = RunMode.Async)] + [Remarks(CommandCategories.Helpers)] + [Summary("Get a role by mentioning it.")] + public async Task giveRole([Summary("roleNickname")] string roleName) + { + try + { + if (_redis.HashExists($"{Context.Guild.Id}:RoleWhitelist", roleName)) + { + var guildUser = (IGuildUser) Context.User; + var roleId = ulong.Parse(_redis.HashGet($"{Context.Guild.Id}:RoleWhitelist", roleName)); + var role = Context.Guild.Roles.First(r => r.Id == roleId); + if (role == null) + { + await ReplyAsync("That role doesn't seem to exist"); + return; + } + if (guildUser.RoleIds.Contains(roleId)) + { + guildUser.RemoveRoleAsync(role); + await ReplyAsync($"Removed you from {role.Name}"); + return; + } + await guildUser.AddRoleAsync(role); + await ReplyAsync($"Added you to {role.Name}"); + return; + } + await ReplyAsync("That role doesn't seem to exist"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("add", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Add a role to the whitelist.")] + public async Task addRole([Summary("@role")] IRole role, [Summary("alias")] string roleName) + { + try + { + _redis.HashSet($"{Context.Guild.Id}:RoleWhitelist", new HashEntry[] { new HashEntry(roleName, role.Id.ToString()) }); + await ReplyAsync($"Added {role.Name} to the whitelist"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + [RequireUserPermission(GuildPermission.Administrator)] + [Command("remove", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Admin)] + [Summary("Remove a role from the whitelist.")] + public async Task removeRole([Summary("roleNickname")] string roleName) + { + try + { + _redis.HashDelete($"{Context.Guild.Id}:RoleWhitelist", roleName); + await ReplyAsync($"Removed {roleName} from the whitelist"); + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index dfba5fa..65b61ef 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -6,26 +6,23 @@ namespace Geekbot.net.Lib { public class ErrorHandler : IErrorHandler { - private readonly ILogger logger; -// private readonly IDMChannel botOwnerDmChannel; + private readonly ILogger _logger; - public ErrorHandler(ILogger logger /*, IDMChannel botOwnerDmChannel*/) + public ErrorHandler(ILogger logger) { - this.logger = logger; -// this.botOwnerDmChannel = botOwnerDmChannel; + _logger = logger; } public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "Something went wrong :confused:") { var errorMsg = - $"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\", complete message was \"{Context.Message}\""; - logger.Error(e, errorMsg); + $"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\""; + _logger.Error(e, errorMsg); if (!string.IsNullOrEmpty(errorMessage)) { Context.Channel.SendMessageAsync(errorMessage); } -// await botOwnerDmChannel.SendMessageAsync($"{errorMsg}```{e.StackTrace}```"); -// await Context.Channel.SendMessageAsync("Something went wrong..."); + } }