Ejemplo n.º 1
0
        public async Task List(CommandContext ctx)
        {
            var table = new AsciiTable(
                new AsciiColumn("ID", alignToRight: true),
                new AsciiColumn("Trigger"),
                new AsciiColumn("Validation"),
                new AsciiColumn("Context"),
                new AsciiColumn("Actions"),
                new AsciiColumn("Custom message")
                );

            using (var db = new BotDb())
            {
                var duplicates = new Dictionary <string, FilterContext>(StringComparer.InvariantCultureIgnoreCase);
                var filters    = await db.Piracystring.Where(ps => !ps.Disabled).OrderBy(ps => ps.String.ToUpperInvariant()).AsNoTracking().ToListAsync().ConfigureAwait(false);

                var nonUniqueTriggers = (
                    from f in filters
                    group f by f.String.ToUpperInvariant()
                    into g
                    where g.Count() > 1
                    select g.Key
                    ).ToList();
                foreach (var t in nonUniqueTriggers)
                {
                    var duplicateFilters = filters.Where(ps => ps.String.Equals(t, StringComparison.InvariantCultureIgnoreCase)).ToList();
                    foreach (FilterContext fctx in Enum.GetValues(typeof(FilterContext)))
                    {
                        if (duplicateFilters.Count(f => (f.Context & fctx) == fctx) > 1)
                        {
                            if (duplicates.TryGetValue(t, out var fctxs))
                            {
                                duplicates[t] = fctxs | fctx;
                            }
                            else
                            {
                                duplicates[t] = fctx;
                            }
                        }
                    }
                }
                foreach (var item in filters)
                {
                    var ctxl = item.Context.ToString();
                    if (duplicates.Count > 0 &&
                        duplicates.TryGetValue(item.String, out var fctx) &&
                        (item.Context & fctx) != 0)
                    {
                        ctxl = "❗ " + ctxl;
                    }
                    table.Add(
                        item.Id.ToString(),
                        item.String.Sanitize(),
                        item.ValidatingRegex,
                        ctxl,
                        item.Actions.ToFlagsString(),
                        string.IsNullOrEmpty(item.CustomMessage) ? "" : "✅"
                        );
                }
            }
            await ctx.SendAutosplitMessageAsync(table.ToString()).ConfigureAwait(false);

            await ctx.RespondAsync(FilterActionExtensions.GetLegend()).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        public async Task List(CommandContext ctx)
        {
            var table = new AsciiTable(
                new AsciiColumn("ID", alignToRight: true),
                new AsciiColumn("Trigger"),
                new AsciiColumn("Validation", maxWidth: 2048),
                new AsciiColumn("Context", maxWidth: 4096),
                new AsciiColumn("Actions"),
                new AsciiColumn("Custom message", maxWidth: 2048)
                );

            await using var db = new BotDb();
            var duplicates        = new Dictionary <string, FilterContext>(StringComparer.InvariantCultureIgnoreCase);
            var filters           = db.Piracystring.Where(ps => !ps.Disabled).AsNoTracking().AsEnumerable().OrderBy(ps => ps.String.ToUpperInvariant()).ToList();
            var nonUniqueTriggers = (
                from f in filters
                group f by f.String.ToUpperInvariant()
                into g
                where g.Count() > 1
                select g.Key
                ).ToList();

            foreach (var t in nonUniqueTriggers)
            {
                var duplicateFilters = filters.Where(ps => ps.String.Equals(t, StringComparison.InvariantCultureIgnoreCase)).ToList();
                foreach (FilterContext fctx in Enum.GetValues(typeof(FilterContext)))
                {
                    if (duplicateFilters.Count(f => (f.Context & fctx) == fctx) > 1)
                    {
                        if (duplicates.TryGetValue(t, out var fctxDup))
                        {
                            duplicates[t] = fctxDup | fctx;
                        }
                        else
                        {
                            duplicates[t] = fctx;
                        }
                    }
                }
            }
            foreach (var item in filters)
            {
                var ctxl = item.Context.ToString();
                if (duplicates.Count > 0 &&
                    duplicates.TryGetValue(item.String, out var fctx) &&
                    (item.Context & fctx) != 0)
                {
                    ctxl = "❗ " + ctxl;
                }
                table.Add(
                    item.Id.ToString(),
                    item.String.Sanitize(),
                    item.ValidatingRegex ?? "",
                    ctxl,
                    item.Actions.ToFlagsString(),
                    item.CustomMessage ?? ""
                    );
            }
            var result = new StringBuilder(table.ToString(false)).AppendLine()
                         .AppendLine(FilterActionExtensions.GetLegend(""));

            await using var output = Config.MemoryStreamManager.GetStream();
            //await using (var gzip = new GZipStream(output, CompressionLevel.Optimal, true))
            await using (var writer = new StreamWriter(output, leaveOpen: true))
                await writer.WriteAsync(result.ToString()).ConfigureAwait(false);
            output.Seek(0, SeekOrigin.Begin);
            await ctx.RespondAsync(new DiscordMessageBuilder().WithFile("filters.txt", output)).ConfigureAwait(false);
        }