AutoMod and AutoRespond items no longer need label

All configuration items for AutoMod and AutoRespond are now defined
as JSON properties. The name of the property is used as its label.
This commit is contained in:
Noikoio 2017-08-29 22:25:47 -07:00
parent 85f694ba7d
commit 019a5bdd96
4 changed files with 19 additions and 17 deletions

View file

@ -29,12 +29,15 @@ namespace Noikoio.RegexBot.Feature.AutoMod
public override async Task<object> ProcessConfiguration(JToken configSection) public override async Task<object> ProcessConfiguration(JToken configSection)
{ {
List<Rule> rules = new List<Rule>(); List<Rule> rules = new List<Rule>();
foreach (JObject ruleconf in configSection)
foreach (var def in configSection.Children<JProperty>())
{ {
var rule = new Rule(this, ruleconf); string label = def.Name;
var rule = new Rule(this, def);
rules.Add(rule); rules.Add(rule);
await Log($"Added rule '{rule.Label}'"); await Log($"Added rule '{rule.Label}'");
} }
return rules.AsReadOnly(); return rules.AsReadOnly();
} }

View file

@ -41,13 +41,14 @@ namespace Noikoio.RegexBot.Feature.AutoMod
/// <summary> /// <summary>
/// Creates a new Rule instance to represent the given configuration. /// Creates a new Rule instance to represent the given configuration.
/// </summary> /// </summary>
public Rule(AutoMod instance, JObject ruleconf) public Rule(AutoMod instance, JProperty definition)
{ {
_instance = instance; _instance = instance;
_label = ruleconf["label"]?.Value<string>(); _label = definition.Name;
if (string.IsNullOrEmpty(_label)) var ruleconf = (JObject)definition.Value;
throw new RuleImportException("Label not defined."); // 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}'."; string errpfx = $" in definition for rule '{_label}'.";

View file

@ -45,7 +45,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
public override Task<object> ProcessConfiguration(JToken configSection) public override Task<object> ProcessConfiguration(JToken configSection)
{ {
var responses = new List<ResponseDefinition>(); var responses = new List<ResponseDefinition>();
foreach (JObject def in configSection) foreach (var def in configSection.Children<JProperty>())
{ {
// Everything is left to the constructor // Everything is left to the constructor
responses.Add(new ResponseDefinition(def)); responses.Add(new ResponseDefinition(def));

View file

@ -25,19 +25,17 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
public FilterList Filter => _filter; public FilterList Filter => _filter;
public RateLimitCache RateLimit => _limit; public RateLimitCache RateLimit => _limit;
public ResponseDefinition(JObject definition) public ResponseDefinition(JProperty definition)
{ {
// label _label = definition.Name;
_label = definition["label"]?.Value<string>(); var data = (JObject)definition.Value;
if (string.IsNullOrWhiteSpace(_label))
throw new RuleImportException("Label is not defined in response definition.");
// error postfix string // error postfix string
string errorpfx = $" in response definition for '{_label}'."; string errorpfx = $" in response definition for '{_label}'.";
// regex trigger // regex trigger
const RegexOptions rxopts = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline; const RegexOptions rxopts = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline;
string triggerstr = definition["trigger"]?.Value<string>(); string triggerstr = data["trigger"]?.Value<string>();
if (string.IsNullOrWhiteSpace(triggerstr)) if (string.IsNullOrWhiteSpace(triggerstr))
throw new RuleImportException("Regular expression trigger is not defined" + errorpfx); throw new RuleImportException("Regular expression trigger is not defined" + errorpfx);
try try
@ -56,7 +54,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
_rtype = ResponseType.None; _rtype = ResponseType.None;
// exec response --- // exec response ---
string execstr = definition["exec"]?.Value<string>(); string execstr = data["exec"]?.Value<string>();
if (!string.IsNullOrWhiteSpace(execstr)) if (!string.IsNullOrWhiteSpace(execstr))
{ {
_rbody = execstr; _rbody = execstr;
@ -64,7 +62,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
} }
// reply response // reply response
string replystr = definition["reply"]?.Value<string>(); string replystr = data["reply"]?.Value<string>();
if (!string.IsNullOrWhiteSpace(replystr)) if (!string.IsNullOrWhiteSpace(replystr))
{ {
if (_rbody != null) if (_rbody != null)
@ -78,10 +76,10 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
// --- // ---
// whitelist/blacklist filtering // whitelist/blacklist filtering
_filter = new FilterList(definition); _filter = new FilterList(data);
// rate limiting // rate limiting
string rlstr = definition["ratelimit"].Value<string>(); string rlstr = data["ratelimit"].Value<string>();
if (string.IsNullOrWhiteSpace(rlstr)) if (string.IsNullOrWhiteSpace(rlstr))
{ {
_limit = new RateLimitCache(RateLimitCache.DefaultTimeout); _limit = new RateLimitCache(RateLimitCache.DefaultTimeout);