mcdiscordbot/MinecraftDiscordBot/BotConfiguration.cs

49 lines
2.2 KiB
C#
Raw Normal View History

using CommandLine;
using Newtonsoft.Json;
using System.Text;
2022-01-10 16:10:32 +01:00
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;
2022-01-10 16:10:32 +01:00
[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;
2022-01-10 16:10:32 +01:00
[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 Discord bot command prefix", Required = true)]
public string SocketHost { get; init; } = default!;
[JsonIgnore]
public BotConfiguration Config => this;
2022-01-10 16:10:32 +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!");
}