mcdiscordbot/MinecraftDiscordBot/BotConfiguration.cs
2022-01-18 13:05:34 +01:00

55 lines
2.6 KiB
C#

using CommandLine;
using Newtonsoft.Json;
using System.Text;
namespace MinecraftDiscordBot;
[Verb("config", HelpText = "Manually configure the bot with CLI arguments.")]
public class BotConfiguration : IBotConfiguration, IBotConfigurator {
private const string DEFAULT_PREFIX = "!";
private const int DEFAULT_PORT = 8080;
[JsonProperty("token", Required = Required.Always)]
[Option('t', "token", HelpText = "The Discord bot token", Required = true)]
public string Token { get; init; } = default!;
[JsonProperty("port", Required = Required.DisallowNull)]
[Option('p', "port", Default = DEFAULT_PORT, HelpText = "The websocket server port")]
public int Port { get; init; } = DEFAULT_PORT;
[JsonProperty("channels", Required = Required.Always)]
[Option('c', "channel", HelpText = "The list of whitelisted channels", Required = true, Min = 1)]
public IEnumerable<ulong> Channels { get; init; } = default!;
[JsonProperty("prefix", Required = Required.DisallowNull)]
[Option("prefix", Default = DEFAULT_PREFIX, HelpText = "The Discord bot command prefix")]
public string Prefix { get; init; } = DEFAULT_PREFIX;
[JsonProperty("host", Required = Required.Always)]
[Option("host", Default = DEFAULT_PREFIX, HelpText = "The external websocket hostname.", Required = true)]
public string SocketHost { get; init; } = default!;
[JsonProperty("admins", Required = Required.DisallowNull)]
[Option("admins", Default = new ulong[] { }, HelpText = "The list of bot administrators.")]
public ulong[] Administrators { get; init; } = Array.Empty<ulong>();
[JsonProperty("logchannel", Required = Required.DisallowNull)]
[Option("logchannel", Default = null, HelpText = "Optionally the id of a channel to mirror log to.")]
public ulong? LogChannel { get; init; } = null;
[JsonIgnore]
public BotConfiguration Config => this;
}
public interface IBotConfigurator {
BotConfiguration Config { get; }
}
public interface IBotConfiguration {
string Token { get; }
int Port { get; }
IEnumerable<ulong> Channels { get; }
string Prefix { get; }
}
[Verb("file", true, HelpText = "Load a bot configuration file.")]
public class ConfigFile : IBotConfigurator {
private const string DEFAULT_CONFIGPATH = "config.json";
[Option('f', "file", Default = DEFAULT_CONFIGPATH, HelpText = "The path of the configuration file")]
public string ConfigPath { get; set; } = DEFAULT_CONFIGPATH;
public BotConfiguration Config
=> JsonConvert.DeserializeObject<BotConfiguration>(File.ReadAllText(ConfigPath, Encoding.UTF8))
?? throw new InvalidProgramException("Invalid empty config file!");
}