2022-01-12 14:32:25 +01:00
|
|
|
|
using CommandLine;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Text;
|
2022-01-10 16:10:32 +01:00
|
|
|
|
|
|
|
|
|
namespace MinecraftDiscordBot;
|
|
|
|
|
|
2022-01-12 14:32:25 +01:00
|
|
|
|
[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;
|
2022-01-10 16:10:32 +01:00
|
|
|
|
[JsonProperty("token", Required = Required.Always)]
|
2022-01-12 14:32:25 +01:00
|
|
|
|
[Option('t', "token", HelpText = "The Discord bot token", Required = true)]
|
|
|
|
|
public string Token { get; init; } = default!;
|
2022-11-23 00:28:01 +01:00
|
|
|
|
[JsonProperty("address", Required = Required.Always)]
|
|
|
|
|
[Option('a', "address", HelpText = "The connection string for the websocket", Required = true)]
|
|
|
|
|
public string Address { get; init; } = default!;
|
2022-01-12 14:32:25 +01:00
|
|
|
|
[JsonProperty("port", Required = Required.DisallowNull)]
|
2022-11-23 00:28:01 +01:00
|
|
|
|
[Option('p', "port", Default = DEFAULT_PORT, HelpText = "The websocket server listen port")]
|
2022-01-12 14:32:25 +01:00
|
|
|
|
public int Port { get; init; } = DEFAULT_PORT;
|
2022-01-10 16:10:32 +01:00
|
|
|
|
[JsonProperty("channels", Required = Required.Always)]
|
2022-01-12 14:32:25 +01:00
|
|
|
|
[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;
|
2022-01-16 22:29:50 +01:00
|
|
|
|
[JsonProperty("host", Required = Required.Always)]
|
2022-01-17 15:24:04 +01:00
|
|
|
|
[Option("host", Default = DEFAULT_PREFIX, HelpText = "The external websocket hostname.", Required = true)]
|
2022-01-16 22:29:50 +01:00
|
|
|
|
public string SocketHost { get; init; } = default!;
|
2022-01-17 15:24:04 +01:00
|
|
|
|
[JsonProperty("admins", Required = Required.DisallowNull)]
|
|
|
|
|
[Option("admins", Default = new ulong[] { }, HelpText = "The list of bot administrators.")]
|
|
|
|
|
public ulong[] Administrators { get; init; } = Array.Empty<ulong>();
|
2022-01-18 13:05:34 +01:00
|
|
|
|
[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;
|
2022-01-12 14:32:25 +01:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public BotConfiguration Config => this;
|
2022-01-10 16:10:32 +01:00
|
|
|
|
}
|
2022-01-12 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
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!");
|
|
|
|
|
}
|