chaosbot/ChaosBot/Discord/ModulesOld/ConfigCommands.cs.txt

91 lines
3.1 KiB
Plaintext

using System;
using System.Linq;
using Discord;
using Discord.Commands;
using System.Threading.Tasks;
using NLog;
using System.Text;
using System.Text.Json;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
using ChaosBot.Repositories;
namespace ChaosBot.Discord.Modules
{
public class ConfigCommands : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("config")]
[CheckCommandPerm]
public async Task setConfig(string configFlag = null, string value = null)
{
var sb = new StringBuilder();
var embed = new EmbedBuilder();
try
{
if (configFlag == null || value == null)
{
string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!");
await ReplyAsync($"Syntax Wrong. Please see {prefix}config help");
return;
}
using (ChaosbotContext dbContext = new ChaosbotContext())
{
Configuration config = dbContext.Configuration
.FirstOrDefault(c => c.DiscordGuildId == Context.Guild.Id && c.Key == configFlag);
// TODO: Is this warning valid?
config.SerializedValue = JsonSerializer.Serialize(value);
dbContext.SaveChanges();
}
embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Configuration Management";
sb.AppendLine($"{Context.User.Mention} has changed the Configuration.");
sb.AppendLine();
sb.AppendLine($"{configFlag} == {value}");
/*
* 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($"ConfigCommands.setCfg: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
[Command("config help")]
[RequireBotPermission(GuildPermission.ManageGuild)]
[RequireUserPermission(GuildPermission.ManageGuild)]
public async Task helpConfig(string configFlag = null, string value = null)
{
string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!");
var sb = new StringBuilder();
var embed = new EmbedBuilder();
embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Configuration Management Help";
sb.AppendLine();
sb.AppendLine($"{prefix}config <configFlag> <value>");
/*
* Add the string to the Embed
*/
embed.Description = sb.ToString();
/*
* Reply with the Embed created above
*/
await ReplyAsync(null, false, embed.Build());
}
}
}