having .crca enabled with %target% will replace target with everything that comes after the trigger wherever it is in the triggering message

This commit is contained in:
Master Kwoth 2017-08-06 14:31:28 +02:00
parent 10fdd36e87
commit 464118f792
2 changed files with 52 additions and 9 deletions

View File

@ -58,11 +58,23 @@ namespace NadekoBot.Modules.CustomReactions.Extensions
return str;
}
private static async Task<string> ResolveResponseStringAsync(this string str, IUserMessage ctx, DiscordSocketClient client, string resolvedTrigger)
private static async Task<string> ResolveResponseStringAsync(this string str, IUserMessage ctx, DiscordSocketClient client, string resolvedTrigger, bool containsAnywhere)
{
var substringIndex = resolvedTrigger.Length;
if (containsAnywhere)
{
var pos = ctx.Content.GetWordPosition(resolvedTrigger);
if (pos == WordPosition.Start)
substringIndex += 1;
else if (pos == WordPosition.End)
substringIndex = ctx.Content.Length;
else if (pos == WordPosition.Middle)
substringIndex += ctx.Content.IndexOf(resolvedTrigger);
}
var rep = new ReplacementBuilder()
.WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild, client)
.WithOverride("%target%", () => ctx.Content.Substring(resolvedTrigger.Length).Trim())
.WithOverride("%target%", () => ctx.Content.Substring(substringIndex))
.Build();
str = rep.Replace(str);
@ -77,8 +89,8 @@ namespace NadekoBot.Modules.CustomReactions.Extensions
public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client)
=> cr.Trigger.ResolveTriggerString(ctx, client);
public static Task<string > ResponseWithContextAsync(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client)
=> cr.Response.ResolveResponseStringAsync(ctx, client, cr.Trigger.ResolveTriggerString(ctx, client));
public static Task<string > ResponseWithContextAsync(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, bool containsAnywhere)
=> cr.Response.ResolveResponseStringAsync(ctx, client, cr.Trigger.ResolveTriggerString(ctx, client), containsAnywhere);
public static async Task<IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, CustomReactionsService crs)
{
@ -88,16 +100,49 @@ namespace NadekoBot.Modules.CustomReactions.Extensions
if (CREmbed.TryParse(cr.Response, out CREmbed crembed))
{
var trigger = cr.Trigger.ResolveTriggerString(ctx, client);
var substringIndex = trigger.Length;
if (cr.ContainsAnywhere)
{
var pos = ctx.Content.GetWordPosition(trigger);
if (pos == WordPosition.Start)
substringIndex += 1;
else if (pos == WordPosition.End)
substringIndex = ctx.Content.Length;
else if (pos == WordPosition.Middle)
substringIndex += ctx.Content.IndexOf(trigger);
}
var rep = new ReplacementBuilder()
.WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild, client)
.WithOverride("%target%", () => ctx.Content.Substring(cr.Trigger.ResolveTriggerString(ctx, client).Length).Trim())
.WithOverride("%target%", () => ctx.Content.Substring(substringIndex).Trim())
.Build();
rep.Replace(crembed);
return await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? "");
}
return await channel.SendMessageAsync((await cr.ResponseWithContextAsync(ctx, client)).SanitizeMentions());
return await channel.SendMessageAsync((await cr.ResponseWithContextAsync(ctx, client, cr.ContainsAnywhere)).SanitizeMentions());
}
public static WordPosition GetWordPosition(this string str, string word)
{
if (str.StartsWith(word + " "))
return WordPosition.Start;
else if (str.EndsWith(" " + word))
return WordPosition.End;
else if (str.Contains(" " + word + " "))
return WordPosition.Middle;
else
return WordPosition.None;
}
}
public enum WordPosition
{
None,
Start,
Middle,
End,
}
}

View File

@ -70,9 +70,7 @@ namespace NadekoBot.Modules.CustomReactions.Services
var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
var trigger = cr.TriggerWithContext(umsg, _client).Trim().ToLowerInvariant();
return ((cr.ContainsAnywhere &&
(content.StartsWith(trigger + " ")
|| content.EndsWith(" " + trigger)
|| content.Contains(" " + trigger + " ")))
(content.GetWordPosition(trigger) != WordPosition.None))
|| (hasTarget && content.StartsWith(trigger + " "))
|| (_bc.BotConfig.CustomReactionsStartWith && content.StartsWith(trigger + " "))
|| content == trigger);