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)
{
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);
await Log($"Added rule '{rule.Label}'");
}
return rules.AsReadOnly();
}

View file

@ -41,13 +41,14 @@ namespace Noikoio.RegexBot.Feature.AutoMod
/// <summary>
/// Creates a new Rule instance to represent the given configuration.
/// </summary>
public Rule(AutoMod instance, JObject ruleconf)
public Rule(AutoMod instance, JProperty definition)
{
_instance = instance;
_label = ruleconf["label"]?.Value<string>();
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}'.";

View file

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

View file

@ -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<string>();
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>();
string triggerstr = data["trigger"]?.Value<string>();
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>();
string execstr = data["exec"]?.Value<string>();
if (!string.IsNullOrWhiteSpace(execstr))
{
_rbody = execstr;
@ -64,7 +62,7 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
}
// reply response
string replystr = definition["reply"]?.Value<string>();
string replystr = data["reply"]?.Value<string>();
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>();
string rlstr = data["ratelimit"].Value<string>();
if (string.IsNullOrWhiteSpace(rlstr))
{
_limit = new RateLimitCache(RateLimitCache.DefaultTimeout);