Ejemplo n.º 1
0
        public async Task Add(CommandContext ctx, [RemainingText, Description("A plain string to match")] string trigger)
        {
            using (var db = new BotDb())
            {
                Piracystring filter;
                if (string.IsNullOrEmpty(trigger))
                {
                    filter = new Piracystring();
                }
                else
                {
                    filter = await db.Piracystring.FirstOrDefaultAsync(ps => ps.String == trigger && ps.Disabled).ConfigureAwait(false);

                    if (filter == null)
                    {
                        filter = new Piracystring {
                            String = trigger
                        }
                    }
                    ;
                    else
                    {
                        filter.Disabled = false;
                    }
                }
                var isNewFilter = filter.Id == default;
                if (isNewFilter)
                {
                    filter.Context = FilterContext.Chat | FilterContext.Log;
                    filter.Actions = FilterAction.RemoveContent | FilterAction.IssueWarning | FilterAction.SendMessage;
                }

                var(success, msg) = await EditFilterPropertiesAsync(ctx, db, filter).ConfigureAwait(false);

                if (success)
                {
                    if (isNewFilter)
                    {
                        await db.Piracystring.AddAsync(filter).ConfigureAwait(false);
                    }
                    await db.SaveChangesAsync().ConfigureAwait(false);

                    await msg.UpdateOrCreateMessageAsync(ctx.Channel, embed : FormatFilter(filter).WithTitle("Created a new content filter")).ConfigureAwait(false);

                    var member    = ctx.Member ?? ctx.Client.GetMember(ctx.User);
                    var reportMsg = $"{member.GetMentionWithNickname()} added a new content filter: `{filter.String.Sanitize()}`";
                    if (!string.IsNullOrEmpty(filter.ValidatingRegex))
                    {
                        reportMsg += $"\nValidation: `{filter.ValidatingRegex}`";
                    }
                    await ctx.Client.ReportAsync("🆕 Content filter created", reportMsg, null, ReportSeverity.Low).ConfigureAwait(false);

                    ContentFilter.RebuildMatcher();
                }
                else
                {
                    await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Content filter creation aborted").ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task Remove(CommandContext ctx, [Description("Trigger to remove"), RemainingText] string trigger)
        {
            if (string.IsNullOrWhiteSpace(trigger))
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, "No trigger was specified").ConfigureAwait(false);

                return;
            }

            using (var db = new BotDb())
            {
                var f = await db.Piracystring.FirstOrDefaultAsync(ps => ps.String.Equals(trigger, StringComparison.InvariantCultureIgnoreCase) && !ps.Disabled).ConfigureAwait(false);

                if (f == null)
                {
                    await ctx.ReactWithAsync(Config.Reactions.Failure, "Specified filter does not exist").ConfigureAwait(false);

                    return;
                }

                f.Disabled = true;
                await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
            }

            await ctx.ReactWithAsync(Config.Reactions.Success, "Trigger was removed").ConfigureAwait(false);

            var member = ctx.Member ?? ctx.Client.GetMember(ctx.User);
            await ctx.Client.ReportAsync("📴 Piracy filter removed", $"{member.GetMentionWithNickname()} removed 1 piracy filter: `{trigger.Sanitize()}`", null, ReportSeverity.Medium).ConfigureAwait(false);

            ContentFilter.RebuildMatcher();
        }
Ejemplo n.º 3
0
        private async Task EditFilterCmd(CommandContext ctx, BotDb db, Piracystring filter)
        {
            var(success, msg) = await EditFilterPropertiesAsync(ctx, db, filter).ConfigureAwait(false);

            if (success)
            {
                await db.SaveChangesAsync().ConfigureAwait(false);

                await msg.UpdateOrCreateMessageAsync(ctx.Channel, embed : FormatFilter(filter).WithTitle("Updated content filter")).ConfigureAwait(false);

                var member    = ctx.Member ?? ctx.Client.GetMember(ctx.User);
                var reportMsg = $"{member.GetMentionWithNickname()} changed content filter: `{filter.String.Sanitize()}`";
                if (!string.IsNullOrEmpty(filter.ValidatingRegex))
                {
                    reportMsg += $"\nValidation: `{filter.ValidatingRegex}`";
                }
                await ctx.Client.ReportAsync("🆙 Content filter updated", reportMsg, null, ReportSeverity.Low).ConfigureAwait(false);

                ContentFilter.RebuildMatcher();
            }
            else
            {
                await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Content filter update aborted").ConfigureAwait(false);
            }
        }
Ejemplo n.º 4
0
        public async Task Remove(CommandContext ctx, [Description("Filter IDs to remove, separated with spaces")] params int[] ids)
        {
            int removedFilters;
            var removedTriggers = new StringBuilder();

            using (var db = new BotDb())
            {
                foreach (var f in db.Piracystring.Where(ps => ids.Contains(ps.Id) && !ps.Disabled))
                {
                    f.Disabled = true;
                    removedTriggers.Append($"\n`{f.String.Sanitize()}`");
                }
                removedFilters = await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
            }

            if (removedFilters < ids.Length)
            {
                await ctx.RespondAsync("Some ids couldn't be removed.").ConfigureAwait(false);
            }
            else
            {
                await ctx.ReactWithAsync(Config.Reactions.Success, $"Trigger{StringUtils.GetSuffix(ids.Length)} successfully removed!").ConfigureAwait(false);

                var member     = ctx.Member ?? ctx.Client.GetMember(ctx.User);
                var s          = removedFilters == 1 ? "" : "s";
                var filterList = removedTriggers.ToString();
                if (removedFilters == 1)
                {
                    filterList = filterList.TrimStart();
                }
                await ctx.Client.ReportAsync($"📴 Piracy filter{s} removed", $"{member.GetMentionWithNickname()} removed {removedFilters} piracy filter{s}: {filterList}".Trim(EmbedPager.MaxDescriptionLength), null, ReportSeverity.Medium).ConfigureAwait(false);
            }
            ContentFilter.RebuildMatcher();
        }