199 lines
7.1 KiB
C#
199 lines
7.1 KiB
C#
using System;
|
|
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 ChaosBot.Services;
|
|
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")
|
|
await ConfigHelp();
|
|
else if (cmd == "set")
|
|
await ConfigSet(key, value);
|
|
else if (cmd == "get")
|
|
await ConfigGet(key);
|
|
else if (cmd == "reset" || cmd == "unset")
|
|
await ConfigReset(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("To reset a configuration value to default:");
|
|
sb.AppendLine($"{ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}config reset <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) )
|
|
{
|
|
if(await RestrictedConfig.IsAllowed(key))
|
|
{
|
|
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();
|
|
|
|
await ConfigGet(key, true);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await 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) && (await RestrictedConfig.IsAllowed(key)))
|
|
{
|
|
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
|
|
{
|
|
await ConfigHelp();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
|
|
public async Task ConfigReset(string key)
|
|
{
|
|
try
|
|
{
|
|
if ((key != null) && (await RestrictedConfig.IsAllowed(key)))
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
EmbedBuilder embed = new EmbedBuilder();
|
|
|
|
ConfigurationRepository.DeleteValue(key, Context.Guild.Id);
|
|
|
|
embed.WithColor(new Color(255, 255, 0));
|
|
embed.Title = $"Configuration Reset";
|
|
sb.AppendLine();
|
|
sb.AppendLine($" Flag: {key}");
|
|
|
|
/*
|
|
* Add the string to the Embed
|
|
*/
|
|
embed.Description = sb.ToString();
|
|
|
|
/*
|
|
* Reply with the Embed created above
|
|
*/
|
|
await ReplyAsync(null, false, embed.Build());
|
|
}
|
|
else
|
|
{
|
|
await ConfigHelp();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error(
|
|
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
|
}
|
|
}
|
|
}
|
|
} |