using Newtonsoft.Json.Linq; using Noikoio.RegexBot.ConfigItem; using Noikoio.RegexBot.Module.ModCommands.Commands; using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace Noikoio.RegexBot.Module.ModCommands { /// /// Represents ModTools configuration within one server. /// class ConfigItem { private readonly ReadOnlyDictionary _cmdInstances; public ReadOnlyDictionary Commands => _cmdInstances; public ConfigItem(CommandListener instance, JToken inconf) { if (inconf.Type != JTokenType.Object) { throw new RuleImportException("Configuration for this section is invalid."); } var config = (JObject)inconf; // Command instance creation var commands = new Dictionary(StringComparer.OrdinalIgnoreCase); var commandconf = config["Commands"]; if (commandconf != null) { if (commandconf.Type != JTokenType.Object) { throw new RuleImportException("CommandDefs is not properly defined."); } foreach (var def in commandconf.Children()) { string label = def.Name; var cmd = Command.CreateInstance(instance, def); if (commands.ContainsKey(cmd.Trigger)) throw new RuleImportException( $"{label}: 'command' value must not be equal to that of another definition. " + $"Given value is being used for \"{commands[cmd.Trigger].Label}\"."); commands.Add(cmd.Trigger, cmd); } } _cmdInstances = new ReadOnlyDictionary(commands); } } }