Added RandomChance parameter to AutoRespond

This commit is contained in:
Noikoio 2018-01-23 14:06:31 -08:00
parent b8abc50703
commit 4d6bd13e03
2 changed files with 36 additions and 4 deletions

View file

@ -13,6 +13,7 @@ namespace Noikoio.RegexBot.Module.AutoRespond
class ConfigItem class ConfigItem
{ {
public enum ResponseType { None, Exec, Reply } public enum ResponseType { None, Exec, Reply }
private static Random ChangeRng = new Random();
string _label; string _label;
IEnumerable<Regex> _regex; IEnumerable<Regex> _regex;
@ -20,12 +21,14 @@ namespace Noikoio.RegexBot.Module.AutoRespond
string _rbody; string _rbody;
private FilterList _filter; private FilterList _filter;
private RateLimitCache _limit; private RateLimitCache _limit;
private double _random;
public string Label => _label; public string Label => _label;
public IEnumerable<Regex> Regex => _regex; 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;
public double RandomChance => _random;
public ConfigItem(JProperty definition) public ConfigItem(JProperty definition)
{ {
@ -119,6 +122,24 @@ namespace Noikoio.RegexBot.Module.AutoRespond
throw new RuleImportException("Rate limit value is invalid" + errorpfx); throw new RuleImportException("Rate limit value is invalid" + errorpfx);
} }
} }
// random chance
string randstr = data["RandomChance"]?.Value<string>();
if (string.IsNullOrWhiteSpace(randstr))
{
_random = double.NaN;
}
else
{
if (!double.TryParse(randstr, out _random))
{
throw new RuleImportException("Random value is invalid (unable to parse)" + errorpfx);
}
if (_random > 1 || _random < 0)
{
throw new RuleImportException("Random value is invalid (not between 0 and 1)" + errorpfx);
}
}
} }
/// <summary> /// <summary>
@ -145,6 +166,15 @@ namespace Noikoio.RegexBot.Module.AutoRespond
// 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;
// Random chance check
if (!double.IsNaN(RandomChance))
{
// Fail if randomly generated value is higher than the parameter
// Example: To fail a 75% chance, the check value must be between 0.75000...001 and 1.0.
var chk = ChangeRng.NextDouble();
if (chk > RandomChance) return false;
}
return true; return true;
} }

View file

@ -18,7 +18,8 @@ Sample within a [server definition](serverdef.html):
"dumb", "dumb",
"productive conversation" "productive conversation"
], ],
"exec": "python /home/bot/did-someone-say-botspam.py" "exec": "python /home/bot/did-someone-say-botspam.py",
"RandomChance": 0.5
} }
} }
``` ```
@ -26,8 +27,9 @@ Sample within a [server definition](serverdef.html):
### Definition structure ### Definition structure
The following is a list of accepted members within an AutoRespond definition: The following is a list of accepted members within an AutoRespond definition:
* regex (*string* or *string array*) - **Required.** Regular expression pattern(s) that will invoke the response. * regex (*string* or *string array*) - **Required.** Regular expression pattern(s) that will invoke the response.
* reply *(string)* - The message to send out to the channel in which the response was invoked.<sup>1</sup> * reply (*string*) - The message to send out to the channel in which the response was invoked.<sup>1</sup>
* exec *(string)* - Command line path and optional parameters to an external program. The program's output will be sent to the channel in which the response was invoked.<sup>1</sup> * exec (*string*) - Command line path and optional parameters to an external program. The program's output will be sent to the channel in which the response was invoked.<sup>1</sup>
* ratelimit *(integer)* - The amount of time in seconds in which the response may not be triggered again within the same channel. Defaults to 20. * ratelimit (*integer*) - The amount of time in seconds in which the response may not be triggered again within the same channel. Defaults to 20.
* RandomChance (*number*) - A value between 0 and 1 representing the percent chance for the bot to respond to the corresponding trigger. Defaults to 1.0 (100%).
<sup>1</sup> It is **required** to have either *reply* or *exec* specified in a definition. <sup>1</sup> It is **required** to have either *reply* or *exec* specified in a definition.