Ejemplo n.º 1
0
        public async Task <IActionResult> Status()
        {
            Identity currentIdentity = await GetIdentity();

            if (!currentIdentity.IsSiteAdmin())
            {
                return(Unauthorized());
            }

            List <string> currentLogins = new();

            foreach (var login in _identityManager.GetCurrentIdentities())
            {
                if (login is DiscordOAuthIdentity)
                {
                    try
                    {
                        var user = login.GetCurrentUser();
                        if (user == null)
                        {
                            currentLogins.Add($"Invalid user.");
                        }
                        else
                        {
                            currentLogins.Add($"{user.Username}#{user.Discriminator}");
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "Error getting logged in user.");
                        currentLogins.Add($"Invalid user.");
                    }
                }
            }

            StatusRepository repo = StatusRepository.CreateDefault(_serviceProvider);

            StatusDetail botDetails = repo.GetBotStatus();
            StatusDetail dbDetails  = await repo.GetDbStatus();

            StatusDetail cacheDetails = repo.GetCacheStatus();

            return(Ok(new
            {
                botStatus = botDetails,
                dbStatus = dbDetails,
                cacheStatus = cacheDetails,
                loginsInLast15Minutes = currentLogins,
                defaultLanguage = _config.GetDefaultLanguage(),
                trackedInvites = await InviteRepository.CreateDefault(_serviceProvider).CountInvites(),
                modCases = await ModCaseRepository.CreateDefault(_serviceProvider, currentIdentity).CountAllCases(),
                guilds = await GuildConfigRepository.CreateDefault(_serviceProvider).CountGuildConfigs(),
                automodEvents = await AutoModerationEventRepository.CreateDefault(_serviceProvider).CountEvents(),
                userNotes = await UserNoteRepository.CreateWithBotIdentity(_serviceProvider).CountUserNotes(),
                userMappings = await UserMapRepository.CreateWithBotIdentity(_serviceProvider).CountAllUserMaps(),
                apiTokens = await TokenRepository.CreateDefault(_serviceProvider).CountTokens(),
                nextCache = _scheduler.GetNextCacheSchedule(),
                cachedDataFromDiscord = _discordAPI.GetCache().Keys
            }));
        }
Ejemplo n.º 2
0
        public async Task Status()
        {
            await Context.Interaction.DeferAsync(ephemeral : true);

            EmbedBuilder embed = new EmbedBuilder()
                                 .WithTitle(Translator.T().CmdStatusTitle())
                                 .WithColor(Color.Green)
                                 .WithCurrentTimestamp();

            StatusRepository repo = StatusRepository.CreateDefault(ServiceProvider);

            StatusDetail botDetails = repo.GetBotStatus();
            StatusDetail dbDetails  = await repo.GetDbStatus();

            StatusDetail cacheDetails = repo.GetCacheStatus();

            string lastDisconnect = string.Empty;

            if (botDetails.LastDisconnect != null)
            {
                lastDisconnect = Translator.T().CmdStatusLastDisconnectAt(botDetails.LastDisconnect.Value.ToDiscordTS());
            }

            embed.AddField(
                GetCheckEmote(botDetails.Online) + " " + Translator.T().CmdStatusBot(),
                $"{botDetails.ResponseTime:0.0}ms\n{lastDisconnect}",
                false
                );
            embed.AddField(
                GetCheckEmote(dbDetails.Online) + " " + Translator.T().CmdStatusDatabase(),
                $"{dbDetails.ResponseTime:0.0}ms",
                false
                );
            embed.AddField(
                GetCheckEmote(cacheDetails.Online) + " " + Translator.T().CmdStatusInternalCache(),
                $"{cacheDetails.ResponseTime:0.0}ms",
                false
                );

            if (!(botDetails.Online && dbDetails.Online && cacheDetails.Online))
            {
                embed.WithColor(Color.Red);
            }

            StringBuilder loggedInString = new();
            int           loggedInCount  = 0;

            foreach (var item in IdentityManager.GetCurrentIdentities().Where(x => x is DiscordOAuthIdentity))
            {
                var user = item.GetCurrentUser();
                if (user != null)
                {
                    loggedInString.AppendLine($"{user.Username}#{user.Discriminator}");
                    loggedInCount++;
                }
            }
            if (loggedInCount != 0)
            {
                embed.AddField(
                    $"{Translator.T().CmdStatusCurrentlyLoggedIn()} [{loggedInCount}]",
                    loggedInString.ToString().Truncate(1024),
                    false
                    );
            }

            await Context.Interaction.ModifyOriginalResponseAsync((msg) => { msg.Embed = embed.Build(); });
        }