From 9e10230c073ce64a279f63513882c79240bb8a29 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 13 Dec 2016 16:17:40 +0100 Subject: [PATCH] random range --- .../Modules/CustomReactions/Extensions.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/NadekoBot/Modules/CustomReactions/Extensions.cs b/src/NadekoBot/Modules/CustomReactions/Extensions.cs index f5cb66cc..a8a46a90 100644 --- a/src/NadekoBot/Modules/CustomReactions/Extensions.cs +++ b/src/NadekoBot/Modules/CustomReactions/Extensions.cs @@ -3,6 +3,7 @@ using NadekoBot.Services; using NadekoBot.Services.Database.Models; using System; using System.Collections.Generic; +using System.Text.RegularExpressions; namespace NadekoBot.Modules.CustomReactions { @@ -17,7 +18,32 @@ namespace NadekoBot.Modules.CustomReactions { {"%mention%", (ctx) => { return $"<@{NadekoBot.Client.GetCurrentUser().Id}>"; } }, {"%user%", (ctx) => { return ctx.Author.Mention; } }, - {"%rng%", (ctx) => { return new NadekoRandom().Next(0,10).ToString(); } } + //{"%rng%", (ctx) => { return new NadekoRandom().Next(0,10).ToString(); } } + }; + + private static readonly Regex rngRegex = new Regex("%rng(?:(?(?:-)?\\d+)-(?(?:-)?\\d+))?%", RegexOptions.Compiled); + + private static readonly NadekoRandom rng = new NadekoRandom(); + + public static Dictionary regexPlaceholders = new Dictionary() + { + { rngRegex, (match) => { + int from = 0; + int.TryParse(match.Groups["from"].ToString(), out from); + + int to = 0; + int.TryParse(match.Groups["to"].ToString(), out to); + + if(from == 0 && to == 0) + { + return rng.Next(0, 11).ToString(); + } + + if(from >= to) + return ""; + + return rng.Next(from,to+1).ToString(); + } } }; private static string ResolveTriggerString(this string str, IUserMessage ctx) @@ -40,6 +66,11 @@ namespace NadekoBot.Modules.CustomReactions { str = str.Replace(ph.Key.ToLowerInvariant(), ph.Value(ctx, resolvedTrigger)); } + + foreach (var ph in regexPlaceholders) + { + str = ph.Key.Replace(str, ph.Value); + } return str; }