Beispiel #1
0
        public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, bool sanitize)
        {
            var channel = cr.DmResponse ? await ctx.Author.GetOrCreateDMChannelAsync().ConfigureAwait(false) : ctx.Channel;

            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.AsSpan().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.InvariantCulture);
                    }
                }

                var canMentionEveryone = (ctx.Author as IGuildUser)?.GuildPermissions.MentionEveryone ?? true;

                var rep = new ReplacementBuilder()
                          .WithDefault(ctx.Author, ctx.Channel, (ctx.Channel as ITextChannel)?.Guild as SocketGuild, client)
                          .WithOverride("%target%", () => canMentionEveryone
                        ? ctx.Content.Substring(substringIndex).Trim()
                        : ctx.Content.Substring(substringIndex).Trim().SanitizeMentions(true))
                          .Build();

                rep.Replace(crembed);

                return(await channel.EmbedAsync(crembed, sanitize).ConfigureAwait(false));
            }
            return(await channel.SendMessageAsync((await cr.ResponseWithContextAsync(ctx, client, cr.ContainsAnywhere).ConfigureAwait(false)).SanitizeMentions(sanitize)).ConfigureAwait(false));
        }
Beispiel #2
0
        public static async Task <IUserMessage> Send(this CustomReaction cr, IUserMessage ctx, DiscordSocketClient client, CustomReactionsService crs)
        {
            var channel = cr.DmResponse ? await ctx.Author.GetOrCreateDMChannelAsync() : ctx.Channel;

            crs.ReactionStats.AddOrUpdate(cr.Trigger, 1, (k, old) => ++ old);

            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(substringIndex).Trim())
                          .Build();

                rep.Replace(crembed);

                return(await channel.EmbedAsync(crembed.ToEmbed(), crembed.PlainText?.SanitizeMentions() ?? ""));
            }
            return(await channel.SendMessageAsync((await cr.ResponseWithContextAsync(ctx, client, cr.ContainsAnywhere)).SanitizeMentions()));
        }