From 379c47cbfe76e3e9b91b6a38851a45ca5d16692c Mon Sep 17 00:00:00 2001 From: Sean Stoves Date: Wed, 5 Aug 2020 19:23:58 -0400 Subject: [PATCH] Added Configuration Handler for Setting and Getting --- ChaosBot/Discord/Modules/Admin/Config.cs | 153 +++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 ChaosBot/Discord/Modules/Admin/Config.cs diff --git a/ChaosBot/Discord/Modules/Admin/Config.cs b/ChaosBot/Discord/Modules/Admin/Config.cs new file mode 100644 index 0000000..300fda3 --- /dev/null +++ b/ChaosBot/Discord/Modules/Admin/Config.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Discord.Commands; +using System.Threading.Tasks; +using System.Reflection; +using System.Text; +using ChaosBot.Discord.PreConditions; +using ChaosBot.Models; +using ChaosBot.Repositories; +using Discord; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; +using NLog; + +namespace ChaosBot.Discord.Modules.Admin +{ + public class Config : ModuleBase + { + private static readonly ILogger _logger = Program.Logger; + + [Command("config")] + [CheckCommandPerm("Admin")] + public async Task ConfigCommand(string cmd = "help", string key = null, string value = null) + { + try + { + if (cmd == "help") + ConfigHelp(); + else if (cmd == "set") + ConfigSet(key, value); + else if (cmd == "get") + ConfigGet(key); + else + await ReplyAsync($"{Context.User.Mention}, The Sub-Command of Config {cmd} does not exist."); + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task ConfigHelp() + { + try + { + var sb = new StringBuilder(); + var embed = new EmbedBuilder(); + + embed.WithColor(new Color(255, 255, 0)); + embed.Title = $"Configuration Management Help"; + sb.AppendLine(); + sb.AppendLine("To set a configuration value:"); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config set "); + sb.AppendLine("To get a configuration value:"); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config get "); + sb.AppendLine(); + sb.AppendLine("To view this help:"); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config help"); + + /* + * Add the string to the Embed + */ + embed.Description = sb.ToString(); + + /* + * Reply with the Embed created above + */ + await ReplyAsync(null, false, embed.Build()); + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task ConfigSet(string key, string value) + { + try + { + if ((key != null) && (value != null)) + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + Configuration cnfSet = new Configuration(); + + cnfSet.Key = key; + cnfSet.DiscordGuildId = Context.Guild.Id; + cnfSet.SerializedValue = JsonConvert.SerializeObject(value); + + await dbContext.Configuration.Upsert(cnfSet) + .On(x => new {x.Key, x.DiscordGuildId}).RunAsync(); + + ConfigGet(key, true); + } + } + else + { + ConfigHelp(); + } + + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task ConfigGet(string key, Boolean update = false) + { + try + { + if (key != null) + { + StringBuilder sb = new StringBuilder(); + EmbedBuilder embed = new EmbedBuilder(); + + embed.WithColor(new Color(255, 255, 0)); + if (update) + embed.Title = $"Configuration Changed"; + else + embed.Title = $"Configuration Retrieval"; + sb.AppendLine(); + sb.AppendLine($" Flag: {key}"); + sb.AppendLine($"Value: {ConfigurationRepository.GetValue(key, Context.Guild.Id, "NotSet")}"); + + /* + * Add the string to the Embed + */ + embed.Description = sb.ToString(); + + /* + * Reply with the Embed created above + */ + await ReplyAsync(null, false, embed.Build()); + } + else + { + ConfigHelp(); + } + + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + } +} \ No newline at end of file