custom reaction now support %target%
This commit is contained in:
parent
966b091c95
commit
1e056e853d
@ -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 { }
|
||||
|
@ -12,27 +12,45 @@ namespace NadekoBot.Modules.CustomReactions
|
||||
{
|
||||
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>>()
|
||||
{
|
||||
{"%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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user