Renamed 'trigger' to 'regex', arrays now allowed
To have regex config be consistent with AutoMod.
This commit is contained in:
parent
a40c115d87
commit
14a811062e
1 changed files with 46 additions and 14 deletions
|
@ -2,6 +2,7 @@
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Noikoio.RegexBot.ConfigItem;
|
using Noikoio.RegexBot.ConfigItem;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Noikoio.RegexBot.Feature.AutoRespond
|
namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
|
@ -14,14 +15,14 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
public enum ResponseType { None, Exec, Reply }
|
public enum ResponseType { None, Exec, Reply }
|
||||||
|
|
||||||
string _label;
|
string _label;
|
||||||
Regex _trigger;
|
IEnumerable<Regex> _regex;
|
||||||
ResponseType _rtype;
|
ResponseType _rtype;
|
||||||
string _rbody; // response body
|
string _rbody;
|
||||||
private FilterList _filter;
|
private FilterList _filter;
|
||||||
private RateLimitCache _limit;
|
private RateLimitCache _limit;
|
||||||
|
|
||||||
public string Label => _label;
|
public string Label => _label;
|
||||||
public Regex Trigger => _trigger;
|
public IEnumerable<Regex> Regex => _regex;
|
||||||
public (ResponseType, string) Response => (_rtype, _rbody);
|
public (ResponseType, string) Response => (_rtype, _rbody);
|
||||||
public FilterList Filter => _filter;
|
public FilterList Filter => _filter;
|
||||||
public RateLimitCache RateLimit => _limit;
|
public RateLimitCache RateLimit => _limit;
|
||||||
|
@ -35,26 +36,48 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
string errorpfx = $" in response definition for '{_label}'.";
|
string errorpfx = $" in response definition for '{_label}'.";
|
||||||
|
|
||||||
// regex trigger
|
// regex trigger
|
||||||
|
const string NoRegexError = "No regular expression patterns are defined";
|
||||||
|
var regexes = new List<Regex>();
|
||||||
const RegexOptions rxopts = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline;
|
const RegexOptions rxopts = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline;
|
||||||
string triggerstr = data["trigger"]?.Value<string>();
|
var rxconf = data["regex"];
|
||||||
if (string.IsNullOrWhiteSpace(triggerstr))
|
if (rxconf == null) throw new RuleImportException(NoRegexError + errorpfx);
|
||||||
throw new RuleImportException("Regular expression trigger is not defined" + errorpfx);
|
if (rxconf.Type == JTokenType.Array)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
_trigger = new Regex(triggerstr, rxopts);
|
foreach (var input in rxconf.Values<string>())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Regex r = new Regex(input, rxopts);
|
||||||
|
regexes.Add(r);
|
||||||
|
}
|
||||||
|
catch (ArgumentException)
|
||||||
|
{
|
||||||
|
throw new RuleImportException(
|
||||||
|
$"Failed to parse regular expression pattern '{input}'{errorpfx}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (ArgumentException ex)
|
else
|
||||||
{
|
{
|
||||||
throw new RuleImportException
|
string rxstr = rxconf.Value<string>();
|
||||||
("Failed to parse regular expression pattern" + errorpfx +
|
try
|
||||||
$" ({ex.GetType().Name}: {ex.Message})");
|
{
|
||||||
|
Regex r = new Regex(rxstr, rxopts);
|
||||||
|
regexes.Add(r);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is ArgumentException || ex is NullReferenceException)
|
||||||
|
{
|
||||||
|
throw new RuleImportException(
|
||||||
|
$"Failed to parse regular expression pattern '{rxstr}'{errorpfx}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
_regex = regexes.ToArray();
|
||||||
|
|
||||||
// response - defined in either "exec" or "reply", but not both
|
// response - defined in either "exec" or "reply", but not both
|
||||||
_rbody = null;
|
_rbody = null;
|
||||||
_rtype = ResponseType.None;
|
_rtype = ResponseType.None;
|
||||||
|
|
||||||
// exec response ---
|
// exec response
|
||||||
string execstr = data["exec"]?.Value<string>();
|
string execstr = data["exec"]?.Value<string>();
|
||||||
if (!string.IsNullOrWhiteSpace(execstr))
|
if (!string.IsNullOrWhiteSpace(execstr))
|
||||||
{
|
{
|
||||||
|
@ -108,7 +131,16 @@ namespace Noikoio.RegexBot.Feature.AutoRespond
|
||||||
if (Filter.IsFiltered(m)) return false;
|
if (Filter.IsFiltered(m)) return false;
|
||||||
|
|
||||||
// Match check
|
// Match check
|
||||||
if (!Trigger.IsMatch(m.Content)) return false;
|
bool matchFound = false;
|
||||||
|
foreach (var item in Regex)
|
||||||
|
{
|
||||||
|
if (item.IsMatch(m.Content))
|
||||||
|
{
|
||||||
|
matchFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!matchFound) return false;
|
||||||
|
|
||||||
// Rate limit check - currently per channel
|
// Rate limit check - currently per channel
|
||||||
if (!RateLimit.AllowUsage(m.Channel.Id)) return false;
|
if (!RateLimit.AllowUsage(m.Channel.Id)) return false;
|
||||||
|
|
Loading…
Reference in a new issue