public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage context) { var channel = cr.DmResponse ? await context.Author.CreateDMChannelAsync() : context.Channel; CustomReactions.ReactionStats.AddOrUpdate(cr.Trigger, 1, (k, old) => ++ old); CREmbed crembed; if (CREmbed.TryParse(cr.Response, out crembed)) { return(await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText ?? "")); } return(await channel.SendMessageAsync(cr.ResponseWithContext(context).SanitizeMentions())); }
public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, CustomReactionsService crs) { var channel = cr.DmResponse ? await ctx.Author.CreateDMChannelAsync() : ctx.Channel; crs.ReactionStats.AddOrUpdate(cr.Trigger, 1, (k, old) => ++ old); if (!CREmbed.TryParse(cr.Response, out var crembed)) { return(await channel.SendMessageAsync(cr.ResponseWithContext(ctx, client, cr.ContainsAnywhere).SanitizeMentions())); } var trigger = cr.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, StringComparison.Ordinal); } } var rep = new ReplacementBuilder() .WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild, client) .WithOverride("%target%", () => ctx.Content.Substring(substringIndex).Trim()) .Build(); rep.Replace(crembed); return(await channel.EmbedAsync(crembed.ToEmbedBuilder(), crembed.PlainText?.SanitizeMentions() ?? "")); }