2022-07-18 23:44:31 +00:00
|
|
|
|
using RegexBot.Modules.ModCommands.Commands;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace RegexBot.Modules.ModCommands;
|
|
|
|
|
class ModuleConfig {
|
|
|
|
|
public ReadOnlyDictionary<string, CommandConfig> Commands { get; }
|
|
|
|
|
|
|
|
|
|
public ModuleConfig(ModCommands instance, JToken conf) {
|
|
|
|
|
if (conf.Type != JTokenType.Array)
|
|
|
|
|
throw new ModuleLoadException("Command definitions must be defined as objects in a JSON array.");
|
|
|
|
|
|
|
|
|
|
// Command instance creation
|
|
|
|
|
var commands = new Dictionary<string, CommandConfig>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
foreach (var def in conf.Children<JObject>()) {
|
|
|
|
|
string Label;
|
|
|
|
|
Label = def[nameof(Label)]?.Value<string>()
|
|
|
|
|
?? throw new ModuleLoadException($"'{nameof(Label)}' was not defined in a command definition.");
|
|
|
|
|
var cmd = CreateCommandInstance(instance, def);
|
2024-06-09 04:07:35 +00:00
|
|
|
|
if (commands.TryGetValue(cmd.Command, out CommandConfig? existing)) {
|
2022-07-18 23:44:31 +00:00
|
|
|
|
throw new ModuleLoadException(
|
2024-06-09 04:07:35 +00:00
|
|
|
|
$"{Label}: The command name '{cmd.Command}' is already in use by '{existing.Label}'.");
|
2022-07-18 23:44:31 +00:00
|
|
|
|
}
|
|
|
|
|
commands.Add(cmd.Command, cmd);
|
|
|
|
|
}
|
|
|
|
|
Commands = new ReadOnlyDictionary<string, CommandConfig>(commands);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly ReadOnlyDictionary<string, Type> _commandTypes = new(
|
|
|
|
|
new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase) {
|
|
|
|
|
{ "ban", typeof(Ban) },
|
|
|
|
|
{ "confreload", typeof(ConfReload) },
|
|
|
|
|
{ "kick", typeof(Kick) },
|
|
|
|
|
{ "say", typeof(Say) },
|
|
|
|
|
{ "unban", typeof(Unban) },
|
2022-09-14 03:16:25 +00:00
|
|
|
|
{ "note", typeof(Note) },
|
|
|
|
|
{ "addnote", typeof(Note) },
|
|
|
|
|
{ "warn", typeof(Warn) },
|
2022-09-21 04:50:33 +00:00
|
|
|
|
{ "timeout", typeof(Commands.Timeout) },
|
2022-10-01 04:03:45 +00:00
|
|
|
|
{ "untimeout", typeof(Untimeout)},
|
2022-07-18 23:44:31 +00:00
|
|
|
|
{ "addrole", typeof(RoleAdd) },
|
|
|
|
|
{ "roleadd", typeof(RoleAdd) },
|
|
|
|
|
{ "delrole", typeof(RoleDel) },
|
2022-10-05 19:40:53 +00:00
|
|
|
|
{ "roledel", typeof(RoleDel) },
|
|
|
|
|
{ "modlogs", typeof(ShowModLogs) },
|
|
|
|
|
{ "showmodlogs", typeof(ShowModLogs) }
|
2022-07-18 23:44:31 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
private static CommandConfig CreateCommandInstance(ModCommands instance, JObject def) {
|
|
|
|
|
var label = def[nameof(CommandConfig.Label)]?.Value<string>()!;
|
|
|
|
|
|
|
|
|
|
var command = def[nameof(CommandConfig.Command)]?.Value<string>();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(command))
|
|
|
|
|
throw new ModuleLoadException($"{label}: '{nameof(CommandConfig.Command)}' was not specified.");
|
|
|
|
|
if (command.Contains(' '))
|
|
|
|
|
throw new ModuleLoadException($"{label}: '{nameof(CommandConfig.Command)}' must not contain spaces.");
|
|
|
|
|
|
|
|
|
|
string? Type;
|
|
|
|
|
Type = def[nameof(Type)]?.Value<string>();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Type))
|
|
|
|
|
throw new ModuleLoadException($"'{nameof(Type)}' must be specified within definition for '{label}'.");
|
|
|
|
|
if (!_commandTypes.TryGetValue(Type, out Type? cmdType)) {
|
|
|
|
|
throw new ModuleLoadException($"{label}: '{nameof(Type)}' does not have a valid value.");
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
return (CommandConfig)Activator.CreateInstance(cmdType, instance, def)!;
|
|
|
|
|
} catch (TargetInvocationException ex) when (ex.InnerException is ModuleLoadException) {
|
|
|
|
|
throw new ModuleLoadException($"{label}: {ex.InnerException.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|