2018-05-06 20:09:17 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2022-05-26 02:27:53 +00:00
|
|
|
|
using RegexBot.Data;
|
2018-05-06 20:09:17 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
namespace RegexBot;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains essential instance configuration for this bot including Discord connection settings, service configuration,
|
|
|
|
|
/// and command-line options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class InstanceConfig {
|
2018-05-06 20:09:17 +00:00
|
|
|
|
/// <summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// Token used for Discord authentication.
|
2018-05-06 20:09:17 +00:00
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
internal string BotToken { get; }
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// List of assemblies to load, by file. Paths are always relative to the bot directory.
|
|
|
|
|
/// </summary>
|
2022-07-21 03:34:29 +00:00
|
|
|
|
internal IReadOnlyList<string> Assemblies { get; }
|
2018-06-05 00:15:18 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Webhook URL for bot log reporting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal string InstanceLogTarget { get; }
|
2018-05-11 06:13:00 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
// TODO add fields for services to be configurable: DMRelay
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets up instance configuration object from file and command line parameters.
|
|
|
|
|
/// </summary>
|
2022-07-21 01:55:08 +00:00
|
|
|
|
internal InstanceConfig() {
|
|
|
|
|
var path = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)
|
2022-07-21 03:34:29 +00:00
|
|
|
|
+ "." + Path.DirectorySeparatorChar + "instance.json";
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
JObject conf;
|
|
|
|
|
try {
|
|
|
|
|
var conftxt = File.ReadAllText(path);
|
|
|
|
|
conf = JObject.Parse(conftxt);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
string pfx;
|
|
|
|
|
if (ex is JsonException) pfx = "Unable to parse configuration: ";
|
|
|
|
|
else pfx = "Unable to access configuration: ";
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
throw new Exception(pfx + ex.Message, ex);
|
|
|
|
|
}
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-03-29 05:03:01 +00:00
|
|
|
|
// Input validation - throw exception on errors. Exception messages printed as-is.
|
2022-05-12 03:26:28 +00:00
|
|
|
|
BotToken = conf[nameof(BotToken)]?.Value<string>()!;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (string.IsNullOrEmpty(BotToken))
|
|
|
|
|
throw new Exception($"'{nameof(BotToken)}' is not properly specified in configuration.");
|
2018-05-06 20:09:17 +00:00
|
|
|
|
|
2022-05-26 02:27:53 +00:00
|
|
|
|
var pginput = conf[nameof(BotDatabaseContext.PostgresConnectionString)]?.Value<string>()!;
|
|
|
|
|
if (string.IsNullOrEmpty(pginput))
|
|
|
|
|
throw new Exception($"'{nameof(BotDatabaseContext.PostgresConnectionString)}' is not properly specified in configuration.");
|
|
|
|
|
BotDatabaseContext.PostgresConnectionString = pginput;
|
2018-05-11 06:13:00 +00:00
|
|
|
|
|
2022-05-12 03:26:28 +00:00
|
|
|
|
InstanceLogTarget = conf[nameof(InstanceLogTarget)]?.Value<string>()!;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (string.IsNullOrEmpty(InstanceLogTarget))
|
|
|
|
|
throw new Exception($"'{nameof(InstanceLogTarget)}' is not properly specified in configuration.");
|
2018-06-05 00:15:18 +00:00
|
|
|
|
|
2022-07-21 03:34:29 +00:00
|
|
|
|
try {
|
|
|
|
|
Assemblies = Common.Utilities.LoadStringOrStringArray(conf[nameof(Assemblies)]).AsReadOnly();
|
|
|
|
|
} catch (ArgumentNullException) {
|
|
|
|
|
Assemblies = Array.Empty<string>();
|
|
|
|
|
} catch (ArgumentException) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
throw new Exception($"'{nameof(Assemblies)}' is not properly specified in configuration.");
|
2018-05-06 20:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|