public async Task List(CommandContext ctx)
        {
            await using var db = new BotDb();
            var selectExpr = db.ForcedNicknames.AsNoTracking();

            if (ctx.Guild != null)
            {
                selectExpr = selectExpr.Where(mem => mem.GuildId == ctx.Guild.Id);
            }

            var forcedNicknames = (
                from m in selectExpr.AsEnumerable()
                orderby m.UserId, m.Nickname
                let result = new { m.UserId, m.Nickname }
                select result
                ).ToList();

            if (forcedNicknames.Count == 0)
            {
                await ctx.RespondAsync("No users with forced nicknames").ConfigureAwait(false);

                return;
            }

            var table = new AsciiTable(
                new AsciiColumn("ID", !ctx.Channel.IsPrivate || !ctx.User.IsWhitelisted(ctx.Client)),
                new AsciiColumn("Username"),
                new AsciiColumn("Forced nickname")
                );
            var previousUser = 0ul;

            foreach (var forcedNickname in forcedNicknames.Distinct())
            {
                var sameUser = forcedNickname.UserId == previousUser;
                var username = sameUser ? "" : await ctx.GetUserNameAsync(forcedNickname.UserId).ConfigureAwait(false);

                table.Add(sameUser ? "" : forcedNickname.UserId.ToString(), username, forcedNickname.Nickname);
                previousUser = forcedNickname.UserId;
            }
            await ctx.SendAutosplitMessageAsync(table.ToString()).ConfigureAwait(false);
        }
        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);
        }
        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);
        }
Exemple #4
0
        public async Task List(CommandContext ctx)
        {
            const string linkPrefix = "discord.gg/";

            await using var db = new BotDb();
            var whitelistedInvites = await db.WhitelistedInvites.ToListAsync().ConfigureAwait(false);

            if (whitelistedInvites.Count == 0)
            {
                await ctx.Channel.SendMessageAsync("There are no whitelisted discord servers").ConfigureAwait(false);

                return;
            }

            var table = new AsciiTable(
                new AsciiColumn("ID", alignToRight: true),
                new AsciiColumn("Server ID", alignToRight: true),
                new AsciiColumn("Invite", disabled: !ctx.Channel.IsPrivate),
                new AsciiColumn("Server Name")
                );

            foreach (var item in whitelistedInvites)
            {
                string?guildName = null;
                if (!string.IsNullOrEmpty(item.InviteCode))
                {
                    try
                    {
                        var invite = await ctx.Client.GetInviteByCodeAsync(item.InviteCode).ConfigureAwait(false);

                        guildName = invite.Guild.Name;
                    }
                    catch { }
                }
                if (string.IsNullOrEmpty(guildName))
                {
                    try
                    {
                        var guild = await ctx.Client.GetGuildAsync(item.GuildId).ConfigureAwait(false);

                        guildName = guild.Name;
                    }
                    catch { }
                }
                if (string.IsNullOrEmpty(guildName))
                {
                    guildName = item.Name ?? "";
                }
                var link = "";
                if (!string.IsNullOrEmpty(item.InviteCode))
                {
                    link = linkPrefix + item.InviteCode;
                }
                //discord expands invite links even if they're inside the code block for some reason
                table.Add(item.Id.ToString(), item.GuildId.ToString(), link /* + StringUtils.InvisibleSpacer*/, guildName.Sanitize());
            }
            var result = new StringBuilder()
                         .AppendLine("Whitelisted discord servers:")
                         .Append(table.ToString(false));

            await using var output = Config.MemoryStreamManager.GetStream();
            await using (var writer = new StreamWriter(output, leaveOpen: true))
                await writer.WriteAsync(result.ToString()).ConfigureAwait(false);
            output.Seek(0, SeekOrigin.Begin);
            await ctx.Channel.SendMessageAsync(new DiscordMessageBuilder().WithFile("invites.txt", output)).ConfigureAwait(false);
        }