209 lines
7.1 KiB
C#
209 lines
7.1 KiB
C#
using System;
|
|
using System.Collections.Immutable;
|
|
using Discord.Commands;
|
|
using System.Threading.Tasks;
|
|
using System.Text;
|
|
using ChaosBot.ConfigHelpers;
|
|
using ChaosBot.Discord.PreConditions;
|
|
using ChaosBot.Models;
|
|
using ChaosBot.Services;
|
|
using Discord;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Configuration = ChaosBot.ConfigHelpers.Configuration;
|
|
|
|
namespace ChaosBot.Discord.Modules.Admin
|
|
{
|
|
public class Config : ModuleBase
|
|
{
|
|
[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)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task ConfigHelp()
|
|
{
|
|
try
|
|
{
|
|
var sb = new StringBuilder();
|
|
var embed = new EmbedBuilder();
|
|
Configuration config = new Configuration();
|
|
|
|
embed.WithColor(new Color(255, 255, 0));
|
|
embed.Title = $"Configuration Management Help";
|
|
sb.AppendLine();
|
|
sb.AppendLine("To set a configuration value:");
|
|
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config set <configFlag> <value>");
|
|
sb.AppendLine("To get a configuration value:");
|
|
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config get <configFlag>");
|
|
sb.AppendLine("To reset a configuration value to default:");
|
|
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config reset <configFlag>");
|
|
sb.AppendLine();
|
|
sb.AppendLine("To view this help:");
|
|
sb.AppendLine($"{config.GetValueGlobalDefault<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)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task ConfigSet(string key, string value)
|
|
{
|
|
try
|
|
{
|
|
if ((key != null) && (value != null) )
|
|
{
|
|
if(RestrictedConfig.IsAllowed(key))
|
|
{
|
|
using (ChaosbotContext dbContext = new ChaosbotContext())
|
|
{
|
|
Models.Configuration cnfSet = new Models.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)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task ConfigGet(string key, Boolean update = false)
|
|
{
|
|
try
|
|
{
|
|
if ((key != null) && (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}");
|
|
|
|
Configuration config = new Configuration();
|
|
ImmutableDictionary<string, (Type type, object defaultvalue)> configFlags =
|
|
config.GetConfigurationFlags();
|
|
|
|
dynamic configValue;
|
|
if (configFlags.TryGetValue(key, out (Type type, object defaultvalue) configFlagValue))
|
|
{
|
|
configValue = new Configuration().GetStringValue(key, Context.Guild.Id);
|
|
}
|
|
else
|
|
{
|
|
configValue = "Not a valid key";
|
|
}
|
|
|
|
sb.AppendLine($"Value: {configValue}");
|
|
|
|
/*
|
|
* 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)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
|
|
public async Task ConfigReset(string key)
|
|
{
|
|
try
|
|
{
|
|
if ((key != null) && (RestrictedConfig.IsAllowed(key)))
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
EmbedBuilder embed = new EmbedBuilder();
|
|
Configuration config = new Configuration();
|
|
|
|
config.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)
|
|
{
|
|
LoggingFacade.Exception(ex);
|
|
}
|
|
}
|
|
}
|
|
}
|