Custom Random range

This commit is contained in:
appelemac 2016-06-17 22:10:51 +02:00
parent 202a0a3363
commit d2c455a9a6

View File

@ -1,38 +1,42 @@
using Discord.Commands; using Discord.Commands;
using Discord.Modules; using Discord.Modules;
using NadekoBot.Extensions;
using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Modules.Permissions.Classes;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
namespace NadekoBot.Modules.CustomReactions namespace NadekoBot.Modules.CustomReactions
{ {
class CustomReactionsModule : DiscordModule internal class CustomReactionsModule : DiscordModule
{ {
public override string Prefix { get; } = ""; public override string Prefix { get; } = "";
Random rng = new Random(); private Random rng = new Random();
private Dictionary<string, Func<CommandEventArgs, string>> commandFuncs; private Dictionary<Regex, Func<CommandEventArgs, Match, string>> commandFuncs;
public CustomReactionsModule() public CustomReactionsModule()
{ {
commandFuncs = new Dictionary<string, Func<CommandEventArgs, string>> commandFuncs = new Dictionary<Regex, Func<CommandEventArgs, Match, string>>
{ {
{"%rng%", (e) => rng.Next().ToString()}, {new Regex(@"%rng:?(\d{0,9})-?(\d{0,9})%"), (e,m) => {
{"%mention%", (e) => NadekoBot.BotMention }, int start, end;
{"%user%", e => e.User.Mention }, if (int.TryParse(m.Groups[1].Value, out start) && int.TryParse(m.Groups[2].Value, out end)) {
{"%target%", e => e.GetArg("args")?.Trim() ?? "" }, 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) public override void Install(ModuleManager manager)
{ {
manager.CreateCommands("", cgb => manager.CreateCommands("", cgb =>
{ {
cgb.AddCheck(PermissionChecker.Instance); cgb.AddCheck(PermissionChecker.Instance);
foreach (var command in NadekoBot.Config.CustomReactions) foreach (var command in NadekoBot.Config.CustomReactions)
@ -47,11 +51,17 @@ namespace NadekoBot.Modules.CustomReactions
.Do(async e => .Do(async e =>
{ {
string str = command.Value[rng.Next(0, command.Value.Count())]; string str = command.Value[rng.Next(0, command.Value.Count())];
commandFuncs.Keys.ForEach(k => str = str.Replace(k, commandFuncs[k](e))); foreach (var key in commandFuncs.Keys)
{
foreach (Match m in key.Matches(str))
{
str = str.Replace(m.Value, commandFuncs[key](e, m));
}
}
await e.Channel.SendMessage(str).ConfigureAwait(false); await e.Channel.SendMessage(str).ConfigureAwait(false);
}); });
} }
}); });
} }
} }