2017-06-17 13:23:13 +00:00
|
|
|
|
using AngleSharp;
|
|
|
|
|
using AngleSharp.Dom.Html;
|
|
|
|
|
using Discord;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
using Discord.WebSocket;
|
2016-12-15 04:57:33 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2017-07-17 02:37:51 +00:00
|
|
|
|
using NadekoBot.Modules.CustomReactions.Services;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services.Database.Models;
|
2016-10-09 04:38:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-06-17 13:23:13 +00:00
|
|
|
|
using System.Linq;
|
2016-12-13 15:17:40 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NadekoBot.Common.Replacements;
|
2016-10-09 04:38:46 +00:00
|
|
|
|
|
2017-07-17 02:37:51 +00:00
|
|
|
|
namespace NadekoBot.Modules.CustomReactions.Extensions
|
2016-10-09 04:38:46 +00:00
|
|
|
|
{
|
|
|
|
|
public static class Extensions
|
|
|
|
|
{
|
2017-06-17 13:23:13 +00:00
|
|
|
|
private static readonly Regex imgRegex = new Regex("%(img|image):(?<tag>.*?)%", RegexOptions.Compiled);
|
2016-12-13 15:17:40 +00:00
|
|
|
|
|
|
|
|
|
private static readonly NadekoRandom rng = new NadekoRandom();
|
|
|
|
|
|
2017-06-17 13:23:13 +00:00
|
|
|
|
public static Dictionary<Regex, Func<Match, Task<string>>> regexPlaceholders = new Dictionary<Regex, Func<Match, Task<string>>>()
|
2016-12-13 15:17:40 +00:00
|
|
|
|
{
|
2017-06-17 13:23:13 +00:00
|
|
|
|
{ imgRegex, async (match) => {
|
|
|
|
|
var tag = match.Groups["tag"].ToString();
|
|
|
|
|
if(string.IsNullOrWhiteSpace(tag))
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
var fullQueryLink = $"http://imgur.com/search?q={ tag }";
|
|
|
|
|
var config = Configuration.Default.WithDefaultLoader();
|
|
|
|
|
var document = await BrowsingContext.New(config).OpenAsync(fullQueryLink);
|
|
|
|
|
|
|
|
|
|
var elems = document.QuerySelectorAll("a.image-list-link").ToArray();
|
|
|
|
|
|
|
|
|
|
if (!elems.Any())
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
var img = (elems.ElementAtOrDefault(new NadekoRandom().Next(0, elems.Length))?.Children?.FirstOrDefault() as IHtmlImageElement);
|
|
|
|
|
|
|
|
|
|
if (img?.Source == null)
|
2016-12-13 15:17:40 +00:00
|
|
|
|
return "";
|
|
|
|
|
|
2017-06-29 15:58:16 +00:00
|
|
|
|
return " " + img.Source.Replace("b.", ".") + " ";
|
2016-12-13 15:17:40 +00:00
|
|
|
|
} }
|
2016-10-09 04:38:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private static string ResolveTriggerString(this string str, IUserMessage ctx, DiscordSocketClient client)
|
2016-10-09 04:38:46 +00:00
|
|
|
|
{
|
2017-06-28 00:44:30 +00:00
|
|
|
|
var rep = new ReplacementBuilder()
|
|
|
|
|
.WithUser(ctx.Author)
|
|
|
|
|
.WithClient(client)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
str = rep.Replace(str.ToLowerInvariant());
|
|
|
|
|
|
2016-10-16 03:22:54 +00:00
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-06 12:31:28 +00:00
|
|
|
|
private static async Task<string> ResolveResponseStringAsync(this string str, IUserMessage ctx, DiscordSocketClient client, string resolvedTrigger, bool containsAnywhere)
|
2016-10-16 03:22:54 +00:00
|
|
|
|
{
|
2017-08-06 12:31:28 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
|
var rep = new ReplacementBuilder()
|
|
|
|
|
.WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild, client)
|
2017-08-11 12:02:48 +00:00
|
|
|
|
.WithOverride("%target%", () => ctx.Content.Substring(substringIndex).Trim())
|
2017-06-28 00:44:30 +00:00
|
|
|
|
.Build();
|
2016-10-16 03:22:54 +00:00
|
|
|
|
|
2017-06-29 03:07:48 +00:00
|
|
|
|
str = rep.Replace(str);
|
2016-12-13 15:17:40 +00:00
|
|
|
|
|
|
|
|
|
foreach (var ph in regexPlaceholders)
|
|
|
|
|
{
|
2017-06-17 13:23:13 +00:00
|
|
|
|
str = await ph.Key.ReplaceAsync(str, ph.Value);
|
2016-12-13 15:17:40 +00:00
|
|
|
|
}
|
2016-10-09 04:38:46 +00:00
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public static string TriggerWithContext(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client)
|
2017-05-25 02:24:43 +00:00
|
|
|
|
=> cr.Trigger.ResolveTriggerString(ctx, client);
|
2016-10-09 04:38:46 +00:00
|
|
|
|
|
2017-08-06 12:31:28 +00:00
|
|
|
|
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);
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
|
public static async Task<IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, CustomReactionsService crs)
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-07-05 15:38:38 +00:00
|
|
|
|
var channel = cr.DmResponse ? await ctx.Author.GetOrCreateDMChannelAsync() : ctx.Channel;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-05-25 02:24:43 +00:00
|
|
|
|
crs.ReactionStats.AddOrUpdate(cr.Trigger, 1, (k, old) => ++old);
|
2017-05-24 04:43:00 +00:00
|
|
|
|
|
2017-05-25 02:24:43 +00:00
|
|
|
|
if (CREmbed.TryParse(cr.Response, out CREmbed crembed))
|
2017-05-24 04:43:00 +00:00
|
|
|
|
{
|
2017-08-06 12:31:28 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 00:44:30 +00:00
|
|
|
|
var rep = new ReplacementBuilder()
|
|
|
|
|
.WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild, client)
|
2017-08-06 12:31:28 +00:00
|
|
|
|
.WithOverride("%target%", () => ctx.Content.Substring(substringIndex).Trim())
|
2017-06-28 00:44:30 +00:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
rep.Replace(crembed);
|
|
|
|
|
|
|
|
|
|
return await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? "");
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}
|
2017-08-06 12:31:28 +00:00
|
|
|
|
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;
|
2017-05-24 04:43:00 +00:00
|
|
|
|
}
|
2016-10-09 04:38:46 +00:00
|
|
|
|
}
|
2017-08-06 12:31:28 +00:00
|
|
|
|
|
|
|
|
|
public enum WordPosition
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Start,
|
|
|
|
|
Middle,
|
|
|
|
|
End,
|
|
|
|
|
}
|
2016-10-09 04:38:46 +00:00
|
|
|
|
}
|