Added Configuration Handler for Setting and Getting
This commit is contained in:
parent
74f4206f27
commit
379c47cbfe
153
ChaosBot/Discord/Modules/Admin/Config.cs
Normal file
153
ChaosBot/Discord/Modules/Admin/Config.cs
Normal file
@ -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<string>("Discord:Prefix", Context.Guild.Id, "!")}config set <configFlag> <value>");
|
||||
sb.AppendLine("To get a configuration value:");
|
||||
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}config get <configFlag>");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("To view this help:");
|
||||
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("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<string>(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}]>.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user