diff --git a/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 39d59efd..a2e3973c 100644 --- a/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -5,34 +5,42 @@ using NadekoBot.Modules.Permissions.Classes; using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; namespace NadekoBot.Modules.CustomReactions { - class CustomReactionsModule : DiscordModule + internal class CustomReactionsModule : DiscordModule { public override string Prefix { get; } = ""; - Random rng = new Random(); + private Random rng = new Random(); - private Dictionary> commandFuncs; + private Dictionary> commandFuncs; public CustomReactionsModule() { - commandFuncs = new Dictionary> + commandFuncs = new Dictionary> { - {"%rng%", (e) => rng.Next().ToString()}, - {"%mention%", (e) => NadekoBot.BotMention }, - {"%user%", e => e.User.Mention }, - {"%target%", e => e.GetArg("args")?.Trim() ?? "" }, + {new Regex(@"(?:%rng%|%rng:(\d{1,9})-(\d{1,9})%)"), (e,m) => { + int start, end; + if (m.Groups[1].Success) + { + start = int.Parse(m.Groups[1].Value); + end = int.Parse(m.Groups[2].Value); + return rng.Next(start, end).ToString(); + }else return rng.Next().ToString(); + } }, + {new Regex("%mention%"), (e,m) => NadekoBot.BotMention }, + {new Regex("%user%"), (e,m) => e.User.Mention }, + {new Regex("%target%"), (e,m) => e.GetArg("args")?.Trim() ?? "" }, + }; } public override void Install(ModuleManager manager) { - manager.CreateCommands("", cgb => { - cgb.AddCheck(PermissionChecker.Instance); foreach (var command in NadekoBot.Config.CustomReactions) @@ -47,11 +55,12 @@ namespace NadekoBot.Modules.CustomReactions .Do(async e => { string str = command.Value[rng.Next(0, command.Value.Count())]; - commandFuncs.Keys.ForEach(k => str = str.Replace(k, commandFuncs[k](e))); + commandFuncs.Keys.ForEach(key => str = key.Replace(str, m => commandFuncs[key](e, m))); + + await e.Channel.SendMessage(str).ConfigureAwait(false); }); } - }); } }