custom reaction now support %target%

This commit is contained in:
Kwoth 2016-10-16 05:22:54 +02:00
parent 966b091c95
commit 1e056e853d
2 changed files with 35 additions and 7 deletions

View File

@ -47,14 +47,24 @@ namespace NadekoBot.Modules.CustomReactions
GuildReactions.TryGetValue(channel.Guild.Id, out reactions); GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
if (reactions != null && reactions.Any()) 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) if (reaction != null)
{ {
try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
return; 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) if (greaction != null)
{ {
try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }

View File

@ -12,27 +12,45 @@ namespace NadekoBot.Modules.CustomReactions
{ {
public static class Extensions public static class Extensions
{ {
public static Dictionary<string, Func<IUserMessage, string, string>> responsePlaceholders = new Dictionary<string, Func<IUserMessage, string, string>>()
{
{"%target%", (ctx, trigger) => { return ctx.Content.ToLowerInvariant().Substring(trigger.Length); } }
};
public static Dictionary<string, Func<IUserMessage, string>> placeholders = new Dictionary<string, Func<IUserMessage, string>>() public static Dictionary<string, Func<IUserMessage, string>> placeholders = new Dictionary<string, Func<IUserMessage, string>>()
{ {
{"%mention%", (ctx) => { return $"<@{NadekoBot.Client.GetCurrentUser().Id}>"; } }, {"%mention%", (ctx) => { return $"<@{NadekoBot.Client.GetCurrentUser().Id}>"; } },
{"%user%", (ctx) => { return ctx.Author.Mention; } }, {"%user%", (ctx) => { return ctx.Author.Mention; } },
{"%target%", (ctx) => { return ctx.MentionedUsers.Shuffle().FirstOrDefault()?.Mention ?? "Nobody"; } },
{"%rng%", (ctx) => { return new NadekoRandom().Next(0,10).ToString(); } } {"%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) 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; return str;
} }
public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx) 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) public static string ResponseWithContext(this CustomReaction cr, IUserMessage ctx)
=> cr.Response.ResolveCRString(ctx); => cr.Response.ResolveResponseString(ctx, cr.Trigger.ResolveTriggerString(ctx));
} }
} }