Fix core classes not being able to read configuration
This commit is contained in:
parent
951692e5fb
commit
858edf6851
@ -13,30 +13,30 @@ namespace ChaosBot.ConfigHelpers
|
||||
|
||||
private static readonly Dictionary<string, IConfigurationDetails> ConfigurationFlags = new Dictionary<string, IConfigurationDetails>
|
||||
{
|
||||
{"Bot:Name", new ConfigurationDetails<string>("Bot:Name", "ChaosBot", true)},
|
||||
{"Bot:Version", new ConfigurationDetails<string>("Bot:Version", "1.0.0", true)},
|
||||
{"Bot:Name", new ConfigurationDetails<string>("Bot:Name", "ChaosBot", true, false)},
|
||||
{"Bot:Version", new ConfigurationDetails<string>("Bot:Version", "1.0.0", true, false)},
|
||||
|
||||
{"WebServer:Port", new ConfigurationDetails<int>("WebServer:Port", 8080, true)},
|
||||
{"WebServer:Debug", new ConfigurationDetails<bool>("WebServer:Debug", false, true)},
|
||||
{"WebServer:Port", new ConfigurationDetails<int>("WebServer:Port", 8080, true, true)},
|
||||
{"WebServer:Debug", new ConfigurationDetails<bool>("WebServer:Debug", false, true, false)},
|
||||
|
||||
{"Discord:Prefix", new ConfigurationDetails<string>("Discord:Prefix", "!", true)},
|
||||
{"Discord:Token", new ConfigurationDetails<string>("Discord:Token", "SECRET_TOKEN", true)},
|
||||
{"Discord:BaseUri", new ConfigurationDetails<string>("Discord:BaseUri", "http://localhost:8080/", true)},
|
||||
{"Discord:ClientId", new ConfigurationDetails<string>("Discord:ClientId", "1234567890", true)},
|
||||
{"Discord:ClientSecret", new ConfigurationDetails<string>("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true)},
|
||||
{"Discord:Prefix", new ConfigurationDetails<string>("Discord:Prefix", "!", true, false)},
|
||||
{"Discord:Token", new ConfigurationDetails<string>("Discord:Token", "SECRET_TOKEN", true, true)},
|
||||
{"Discord:BaseUri", new ConfigurationDetails<string>("Discord:BaseUri", "http://localhost:8080/", true, false)},
|
||||
{"Discord:ClientId", new ConfigurationDetails<string>("Discord:ClientId", "1234567890", true, false)},
|
||||
{"Discord:ClientSecret", new ConfigurationDetails<string>("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true, true)},
|
||||
|
||||
{"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true)},
|
||||
{"Lodestone:ChaosBotApi:Url", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true)},
|
||||
{"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true, true)},
|
||||
{"Lodestone:ChaosBotApi:Url", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true, true)},
|
||||
|
||||
{"Database:Host", new ConfigurationDetails<string>("Database:Host", "localhost", true)},
|
||||
{"Database:Port", new ConfigurationDetails<int>("Database:Port", 3306, true)},
|
||||
{"Database:User", new ConfigurationDetails<string>("Database:User", "root", true)},
|
||||
{"Database:Pass", new ConfigurationDetails<string>("Database:Pass", "password", true)},
|
||||
{"Database:Name", new ConfigurationDetails<string>("Database:Name", "chaosbot", true)},
|
||||
{"Database:Host", new ConfigurationDetails<string>("Database:Host", "localhost", true, true)},
|
||||
{"Database:Port", new ConfigurationDetails<int>("Database:Port", 3306, true, true)},
|
||||
{"Database:User", new ConfigurationDetails<string>("Database:User", "root", true, true)},
|
||||
{"Database:Pass", new ConfigurationDetails<string>("Database:Pass", "password", true, true)},
|
||||
{"Database:Name", new ConfigurationDetails<string>("Database:Name", "chaosbot", true, true)},
|
||||
|
||||
{"Module:Experience:Enabled", new ConfigurationDetails<bool>("Module:Experience:Enabled", true, false)},
|
||||
{"LevelUp:Channel", new ConfigurationDetails<string>("LevelUp:Channel", null, false)},
|
||||
{"LevelUp:MentionUser", new ConfigurationDetails<bool>("LevelUp:MentionUser", true, false)},
|
||||
{"Module:Experience:Enabled", new ConfigurationDetails<bool>("Module:Experience:Enabled", true, false, false)},
|
||||
{"LevelUp:Channel", new ConfigurationDetails<string>("LevelUp:Channel", null, false, false)},
|
||||
{"LevelUp:MentionUser", new ConfigurationDetails<bool>("LevelUp:MentionUser", true, false, false)},
|
||||
};
|
||||
|
||||
public Configuration()
|
||||
@ -86,7 +86,8 @@ namespace ChaosBot.ConfigHelpers
|
||||
public interface IConfigurationDetails
|
||||
{
|
||||
string Key { get; }
|
||||
bool Restricted { get; }
|
||||
bool WriteRestricted { get; }
|
||||
bool ReadRestricted { get; }
|
||||
Type Type { get; }
|
||||
object DefaultValue { get; }
|
||||
string GetStringValue(ulong guildId);
|
||||
@ -98,24 +99,26 @@ namespace ChaosBot.ConfigHelpers
|
||||
{
|
||||
new T DefaultValue { get; }
|
||||
T GetValue(ulong? guildId = null, bool readRestricted = false);
|
||||
T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false);
|
||||
T GetValue(T defaultValue, ulong? guildId = null, bool readValueEvenIfRestricted = false);
|
||||
void SetValue(T value, ulong guildId);
|
||||
}
|
||||
|
||||
public class ConfigurationDetails<T> : IConfigurationDetails<T>
|
||||
{
|
||||
public string Key { get; }
|
||||
public bool Restricted { get; }
|
||||
public bool WriteRestricted { get; }
|
||||
public bool ReadRestricted { get; }
|
||||
public T DefaultValue { get; }
|
||||
|
||||
object IConfigurationDetails.DefaultValue => DefaultValue;
|
||||
public Type Type => typeof(T);
|
||||
|
||||
public ConfigurationDetails(string key, T defaultValue, bool restricted)
|
||||
public ConfigurationDetails(string key, T defaultValue, bool writeRestricted, bool readRestricted)
|
||||
{
|
||||
Key = key;
|
||||
DefaultValue = defaultValue;
|
||||
Restricted = restricted;
|
||||
WriteRestricted = writeRestricted;
|
||||
ReadRestricted = readRestricted;
|
||||
}
|
||||
|
||||
public T GetValue(ulong? guildId = null, bool readRestricted = false)
|
||||
@ -123,9 +126,9 @@ namespace ChaosBot.ConfigHelpers
|
||||
return GetValue(DefaultValue, guildId, readRestricted);
|
||||
}
|
||||
|
||||
public T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false)
|
||||
public T GetValue(T defaultValue, ulong? guildId = null, bool readValueEvenIfRestricted = false)
|
||||
{
|
||||
if (!readRestricted && Restricted)
|
||||
if (!readValueEvenIfRestricted && ReadRestricted)
|
||||
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
|
||||
|
||||
if (guildId.HasValue)
|
||||
@ -143,7 +146,7 @@ namespace ChaosBot.ConfigHelpers
|
||||
|
||||
public void SetValue(T value, ulong guildId)
|
||||
{
|
||||
if (Restricted)
|
||||
if (WriteRestricted)
|
||||
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
|
||||
ConfigurationRepository.SetValue(Key, value, guildId);
|
||||
}
|
||||
@ -155,7 +158,7 @@ namespace ChaosBot.ConfigHelpers
|
||||
|
||||
public void DeleteValue(ulong guildId)
|
||||
{
|
||||
if (Restricted)
|
||||
if (WriteRestricted)
|
||||
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
|
||||
ConfigurationRepository.DeleteValue(Key, guildId);
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ namespace ChaosBot.Discord
|
||||
Configuration config = new Configuration();
|
||||
|
||||
// this is where we get the Token value from the configuration file, and start the bot
|
||||
await client.LoginAsync(TokenType.Bot, config.GetByKey<string>("Discord:Token").GetValue());
|
||||
await client.LoginAsync(TokenType.Bot, config.GetByKey<string>("Discord:Token").GetValue(readRestricted: true));
|
||||
await client.StartAsync();
|
||||
|
||||
// we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service
|
||||
|
||||
@ -81,13 +81,10 @@ namespace ChaosBot.Discord.Modules.Admin
|
||||
try
|
||||
{
|
||||
if ((key != null) && (value != null) )
|
||||
{
|
||||
if(RestrictedConfig.IsAllowed(key))
|
||||
{
|
||||
new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id);
|
||||
await ConfigGet(key, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await ConfigHelp();
|
||||
@ -104,7 +101,7 @@ namespace ChaosBot.Discord.Modules.Admin
|
||||
{
|
||||
try
|
||||
{
|
||||
if ((key != null) && (RestrictedConfig.IsAllowed(key)))
|
||||
if ((key != null))
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
EmbedBuilder embed = new EmbedBuilder();
|
||||
@ -159,7 +156,7 @@ namespace ChaosBot.Discord.Modules.Admin
|
||||
{
|
||||
try
|
||||
{
|
||||
if ((key != null) && (RestrictedConfig.IsAllowed(key)))
|
||||
if ((key != null))
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
EmbedBuilder embed = new EmbedBuilder();
|
||||
|
||||
@ -110,8 +110,8 @@ namespace ChaosBot.Discord.Modules.Admin
|
||||
using HttpClient client = new HttpClient();
|
||||
|
||||
Configuration config = new Configuration();
|
||||
string endpoint =config.GetByKey<string>("Lodestone:ChaosBotApi:Url").GetValue();
|
||||
string apiToken = config.GetByKey<string>("Lodestone:ChaosBotApi:ApiToken").GetValue();
|
||||
string endpoint =config.GetByKey<string>("Lodestone:ChaosBotApi:Url").GetValue(readRestricted: true);
|
||||
string apiToken = config.GetByKey<string>("Lodestone:ChaosBotApi:ApiToken").GetValue(readRestricted: true);
|
||||
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken);
|
||||
HttpResponseMessage result =
|
||||
|
||||
@ -38,11 +38,11 @@ namespace ChaosBot.Models
|
||||
{
|
||||
ConfigHelpers.Configuration config = new ConfigHelpers.Configuration();
|
||||
|
||||
server = config.GetByKey<string>("Database:Host").GetValue();
|
||||
port = config.GetByKey<int>("Database:Port").GetValue();
|
||||
user = config.GetByKey<string>("Database:User").GetValue();
|
||||
pass = config.GetByKey<string>("Database:Pass").GetValue();
|
||||
name = config.GetByKey<string>("Database:Name").GetValue();
|
||||
server = config.GetByKey<string>("Database:Host").GetValue(readRestricted: true);
|
||||
port = config.GetByKey<int>("Database:Port").GetValue(readRestricted: true);
|
||||
user = config.GetByKey<string>("Database:User").GetValue(readRestricted: true);
|
||||
pass = config.GetByKey<string>("Database:Pass").GetValue(readRestricted: true);
|
||||
name = config.GetByKey<string>("Database:Name").GetValue(readRestricted: true);
|
||||
}
|
||||
|
||||
optionsBuilder.UseMySql(
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ChaosBot.Services
|
||||
{
|
||||
public static class RestrictedConfig
|
||||
{
|
||||
public static bool IsAllowed(string key)
|
||||
{
|
||||
// TODO: List populated from DB
|
||||
List<string> restrictedCfg = new List<string>
|
||||
{
|
||||
"Bot:Name",
|
||||
"Bot:Version",
|
||||
|
||||
"WebServer:Port",
|
||||
"WebServer:Debug",
|
||||
|
||||
"Discord:Prefix",
|
||||
"Discord:Token",
|
||||
"Discord:BaseUri",
|
||||
"Discord:ClientId",
|
||||
"Discord:ClientSecret",
|
||||
|
||||
"Lodestone:ChaosBotApi:ApiToken",
|
||||
"Lodestone:ChaosBotApi:Url",
|
||||
|
||||
"Database:Host",
|
||||
"Database:Port",
|
||||
"Database:User",
|
||||
"Database:Pass",
|
||||
"Database:Name",
|
||||
};
|
||||
|
||||
return !restrictedCfg.Contains(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -40,7 +40,7 @@ namespace ChaosBot.WebServer.App
|
||||
|
||||
string redirectUri = $"{config.GetByKey<string>("Discord:BaseUri").GetValue()}/api/discord";
|
||||
string clientId = config.GetByKey<string>("Discord:ClientId").GetValue();
|
||||
string clientSecret = config.GetByKey<string>("Discord:ClientSecret").GetValue();
|
||||
string clientSecret = config.GetByKey<string>("Discord:ClientSecret").GetValue(readRestricted: true);
|
||||
|
||||
if (code == null)
|
||||
return Redirect($"https://discord.com/api/oauth2/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=identify%20guilds");
|
||||
|
||||
@ -29,7 +29,7 @@ namespace ChaosBot.WebServer
|
||||
webBuilder.UseWebRoot(webRoot);
|
||||
webBuilder.ConfigureKestrel(serverOptions =>
|
||||
{
|
||||
serverOptions.Listen(IPAddress.Any, config.GetByKey<int>("WebServer:Port").GetValue(),
|
||||
serverOptions.Listen(IPAddress.Any, config.GetByKey<int>("WebServer:Port").GetValue(readRestricted: true),
|
||||
listenOptions =>
|
||||
{
|
||||
listenOptions.UseConnectionLogging();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user