From 1e056e853db3fd3d734b062b6a9a617e471e915b Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 16 Oct 2016 05:22:54 +0200 Subject: [PATCH] custom reaction now support %target% --- .../CustomReactions/CustomReactions.cs | 14 ++++++++-- .../Modules/CustomReactions/Extensions.cs | 28 +++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 2e72533e..efbf1217 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -47,14 +47,24 @@ namespace NadekoBot.Modules.CustomReactions GuildReactions.TryGetValue(channel.Guild.Id, out reactions); if (reactions != null && reactions.Any()) { - var reaction = reactions.Where(cr => cr.TriggerWithContext(umsg).Trim().ToLowerInvariant() == content).Shuffle().FirstOrDefault(); + var reaction = reactions.Where(cr => { + var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%"); + var trigger = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant(); + return ((hasTarget && content.StartsWith(trigger + " ")) || content == trigger); + }).Shuffle().FirstOrDefault(); if (reaction != null) { try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } return; } } - var greaction = GlobalReactions.Where(cr => cr.TriggerWithContext(umsg).Trim().ToLowerInvariant() == content).Shuffle().FirstOrDefault(); + var greaction = GlobalReactions.Where(cr => + { + var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%"); + var trigger = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant(); + return ((hasTarget && content.StartsWith(trigger + " ")) || content == trigger); + }).Shuffle().FirstOrDefault(); + if (greaction != null) { try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } diff --git a/src/NadekoBot/Modules/CustomReactions/Extensions.cs b/src/NadekoBot/Modules/CustomReactions/Extensions.cs index 147d6dcf..91c3db20 100644 --- a/src/NadekoBot/Modules/CustomReactions/Extensions.cs +++ b/src/NadekoBot/Modules/CustomReactions/Extensions.cs @@ -12,27 +12,45 @@ namespace NadekoBot.Modules.CustomReactions { public static class Extensions { + public static Dictionary> responsePlaceholders = new Dictionary>() + { + {"%target%", (ctx, trigger) => { return ctx.Content.ToLowerInvariant().Substring(trigger.Length); } } + }; + public static Dictionary> placeholders = new Dictionary>() { {"%mention%", (ctx) => { return $"<@{NadekoBot.Client.GetCurrentUser().Id}>"; } }, {"%user%", (ctx) => { return ctx.Author.Mention; } }, - {"%target%", (ctx) => { return ctx.MentionedUsers.Shuffle().FirstOrDefault()?.Mention ?? "Nobody"; } }, {"%rng%", (ctx) => { return new NadekoRandom().Next(0,10).ToString(); } } }; - private static string ResolveCRString(this string str, IUserMessage ctx) + private static string ResolveTriggerString(this string str, IUserMessage ctx) { foreach (var ph in placeholders) { - str = str.Replace(ph.Key, ph.Value(ctx)); + str = str.ToLowerInvariant().Replace(ph.Key, ph.Value(ctx)); + } + return str; + } + + private static string ResolveResponseString(this string str, IUserMessage ctx, string resolvedTrigger) + { + foreach (var ph in placeholders) + { + str = str.ToLowerInvariant().Replace(ph.Key.ToLowerInvariant(), ph.Value(ctx)); + } + + foreach (var ph in responsePlaceholders) + { + str = str.ToLowerInvariant().Replace(ph.Key.ToLowerInvariant(), ph.Value(ctx, resolvedTrigger)); } return str; } public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx) - => cr.Trigger.ResolveCRString(ctx); + => cr.Trigger.ResolveTriggerString(ctx); public static string ResponseWithContext(this CustomReaction cr, IUserMessage ctx) - => cr.Response.ResolveCRString(ctx); + => cr.Response.ResolveResponseString(ctx, cr.Trigger.ResolveTriggerString(ctx)); } }