diff --git a/Feature/AutoMod/AutoMod.cs b/Feature/AutoMod/AutoMod.cs index a8cf37e..affbd45 100644 --- a/Feature/AutoMod/AutoMod.cs +++ b/Feature/AutoMod/AutoMod.cs @@ -29,12 +29,15 @@ namespace Noikoio.RegexBot.Feature.AutoMod public override async Task ProcessConfiguration(JToken configSection) { List rules = new List(); - foreach (JObject ruleconf in configSection) + + foreach (var def in configSection.Children()) { - var rule = new Rule(this, ruleconf); + string label = def.Name; + var rule = new Rule(this, def); rules.Add(rule); await Log($"Added rule '{rule.Label}'"); } + return rules.AsReadOnly(); } diff --git a/Feature/AutoMod/Rule.cs b/Feature/AutoMod/Rule.cs index 3c494a5..dd767db 100644 --- a/Feature/AutoMod/Rule.cs +++ b/Feature/AutoMod/Rule.cs @@ -41,13 +41,14 @@ namespace Noikoio.RegexBot.Feature.AutoMod /// /// Creates a new Rule instance to represent the given configuration. /// - public Rule(AutoMod instance, JObject ruleconf) + public Rule(AutoMod instance, JProperty definition) { _instance = instance; - _label = ruleconf["label"]?.Value(); - if (string.IsNullOrEmpty(_label)) - throw new RuleImportException("Label not defined."); + _label = definition.Name; + var ruleconf = (JObject)definition.Value; + // TODO validation. does the above line even throw an exception in the right cases? + // and what about the label? does it make for a good name? string errpfx = $" in definition for rule '{_label}'."; diff --git a/Feature/AutoRespond/AutoRespond.cs b/Feature/AutoRespond/AutoRespond.cs index b550ea1..51283d3 100644 --- a/Feature/AutoRespond/AutoRespond.cs +++ b/Feature/AutoRespond/AutoRespond.cs @@ -45,7 +45,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond public override Task ProcessConfiguration(JToken configSection) { var responses = new List(); - foreach (JObject def in configSection) + foreach (var def in configSection.Children()) { // Everything is left to the constructor responses.Add(new ResponseDefinition(def)); diff --git a/Feature/AutoRespond/ResponseDefinition.cs b/Feature/AutoRespond/ResponseDefinition.cs index 168cbe0..5aaa268 100644 --- a/Feature/AutoRespond/ResponseDefinition.cs +++ b/Feature/AutoRespond/ResponseDefinition.cs @@ -25,19 +25,17 @@ namespace Noikoio.RegexBot.Feature.AutoRespond public FilterList Filter => _filter; public RateLimitCache RateLimit => _limit; - public ResponseDefinition(JObject definition) + public ResponseDefinition(JProperty definition) { - // label - _label = definition["label"]?.Value(); - if (string.IsNullOrWhiteSpace(_label)) - throw new RuleImportException("Label is not defined in response definition."); + _label = definition.Name; + var data = (JObject)definition.Value; // error postfix string string errorpfx = $" in response definition for '{_label}'."; // regex trigger const RegexOptions rxopts = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline; - string triggerstr = definition["trigger"]?.Value(); + string triggerstr = data["trigger"]?.Value(); if (string.IsNullOrWhiteSpace(triggerstr)) throw new RuleImportException("Regular expression trigger is not defined" + errorpfx); try @@ -56,7 +54,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond _rtype = ResponseType.None; // exec response --- - string execstr = definition["exec"]?.Value(); + string execstr = data["exec"]?.Value(); if (!string.IsNullOrWhiteSpace(execstr)) { _rbody = execstr; @@ -64,7 +62,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond } // reply response - string replystr = definition["reply"]?.Value(); + string replystr = data["reply"]?.Value(); if (!string.IsNullOrWhiteSpace(replystr)) { if (_rbody != null) @@ -78,10 +76,10 @@ namespace Noikoio.RegexBot.Feature.AutoRespond // --- // whitelist/blacklist filtering - _filter = new FilterList(definition); + _filter = new FilterList(data); // rate limiting - string rlstr = definition["ratelimit"].Value(); + string rlstr = data["ratelimit"].Value(); if (string.IsNullOrWhiteSpace(rlstr)) { _limit = new RateLimitCache(RateLimitCache.DefaultTimeout);