random range

This commit is contained in:
Kwoth 2016-12-13 16:17:40 +01:00
parent 9a2a47cd04
commit 9e10230c07

View File

@ -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(?:(?<from>(?:-)?\\d+)-(?<to>(?:-)?\\d+))?%", RegexOptions.Compiled);
private static readonly NadekoRandom rng = new NadekoRandom();
public static Dictionary<Regex, MatchEvaluator> regexPlaceholders = new Dictionary<Regex, MatchEvaluator>()
{
{ 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;
}