From 4d6bd13e03991d97033db4f7153cce966a67f1e7 Mon Sep 17 00:00:00 2001 From: Noikoio Date: Tue, 23 Jan 2018 14:06:31 -0800 Subject: [PATCH] Added RandomChance parameter to AutoRespond --- Module/AutoRespond/ConfigItem.cs | 30 ++++++++++++++++++++++++++++++ docs/autorespond.md | 10 ++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Module/AutoRespond/ConfigItem.cs b/Module/AutoRespond/ConfigItem.cs index 7ed89a1..60fc7f8 100644 --- a/Module/AutoRespond/ConfigItem.cs +++ b/Module/AutoRespond/ConfigItem.cs @@ -13,6 +13,7 @@ namespace Noikoio.RegexBot.Module.AutoRespond class ConfigItem { public enum ResponseType { None, Exec, Reply } + private static Random ChangeRng = new Random(); string _label; IEnumerable _regex; @@ -20,12 +21,14 @@ namespace Noikoio.RegexBot.Module.AutoRespond string _rbody; private FilterList _filter; private RateLimitCache _limit; + private double _random; public string Label => _label; public IEnumerable Regex => _regex; public (ResponseType, string) Response => (_rtype, _rbody); public FilterList Filter => _filter; public RateLimitCache RateLimit => _limit; + public double RandomChance => _random; public ConfigItem(JProperty definition) { @@ -119,6 +122,24 @@ namespace Noikoio.RegexBot.Module.AutoRespond throw new RuleImportException("Rate limit value is invalid" + errorpfx); } } + + // random chance + string randstr = data["RandomChance"]?.Value(); + 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); + } + } } /// @@ -145,6 +166,15 @@ namespace Noikoio.RegexBot.Module.AutoRespond // Rate limit check - currently per channel 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; } diff --git a/docs/autorespond.md b/docs/autorespond.md index b3b6838..ba2db8b 100644 --- a/docs/autorespond.md +++ b/docs/autorespond.md @@ -18,7 +18,8 @@ Sample within a [server definition](serverdef.html): "dumb", "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 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. -* reply *(string)* - The message to send out to the channel in which the response was invoked.1 -* 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.1 -* ratelimit *(integer)* - The amount of time in seconds in which the response may not be triggered again within the same channel. Defaults to 20. +* reply (*string*) - The message to send out to the channel in which the response was invoked.1 +* 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.1 +* 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%). 1 It is **required** to have either *reply* or *exec* specified in a definition. \ No newline at end of file