Example #1
0
    public async Task CollectStats()
    {
        var stopwatch = new Stopwatch();

        stopwatch.Start();

        // Aggregate guild/channel stats
        var guildCount   = 0;
        var channelCount = 0;

        // No LINQ today, sorry
        await foreach (var guild in _cache.GetAllGuilds())
        {
            guildCount++;
            foreach (var channel in await _cache.GetGuildChannels(guild.Id))
            {
                if (DiscordUtils.IsValidGuildChannel(channel))
                {
                    channelCount++;
                }
            }
        }

        if (_config.UseRedisMetrics)
        {
            var db = _redis.Connection.GetDatabase();
            await db.HashSetAsync("pluralkit:cluster_stats", new StackExchange.Redis.HashEntry[] {
                new(_botConfig.Cluster.NodeIndex, JsonConvert.SerializeObject(new ClusterMetricInfo
                {
                    GuildCount = guildCount,
                    ChannelCount = channelCount,
                    DatabaseConnectionCount = _countHolder.ConnectionCount,
                    WebhookCacheSize = _webhookCache.CacheSize,
                })),
            });
Example #2
0
        public async Task SetLogEnabled(Context ctx, bool enable)
        {
            ctx.CheckGuildContext().CheckAuthorPermission(PermissionSet.ManageGuild, "Manage Server");

            var affectedChannels = new List <Channel>();

            if (ctx.Match("all"))
            {
                affectedChannels = _cache.GetGuildChannels(ctx.Guild.Id).Where(x => x.Type == Channel.ChannelType.GuildText).ToList();
            }
            else if (!ctx.HasNext())
            {
                throw new PKSyntaxError("You must pass one or more #channels.");
            }
            else
            {
                while (ctx.HasNext())
                {
                    var channelString = ctx.PeekArgument();
                    var channel       = await ctx.MatchChannel();

                    if (channel == null || channel.GuildId != ctx.Guild.Id)
                    {
                        throw Errors.ChannelNotFound(channelString);
                    }
                    affectedChannels.Add(channel);
                }
            }

            ulong?logChannel = null;

            await using (var conn = await _db.Obtain())
            {
                var config = await _repo.GetGuild(conn, ctx.Guild.Id);

                logChannel = config.LogChannel;
                var blacklist = config.LogBlacklist.ToHashSet();
                if (enable)
                {
                    blacklist.ExceptWith(affectedChannels.Select(c => c.Id));
                }
                else
                {
                    blacklist.UnionWith(affectedChannels.Select(c => c.Id));
                }

                var patch = new GuildPatch {
                    LogBlacklist = blacklist.ToArray()
                };
                await _repo.UpsertGuild(conn, ctx.Guild.Id, patch);
            }

            await ctx.Reply(
                $"{Emojis.Success} Message logging for the given channels {(enable ? "enabled" : "disabled")}." +
                (logChannel == null ? $"\n{Emojis.Warn} Please note that no logging channel is set, so there is nowhere to log messages to. You can set a logging channel using `pk;log channel #your-log-channel`." : ""));
        }