fix: refactored status commands

This commit is contained in:
Marius Begby committed 2023-09-13 22:48:03 +02:00
1 parent 9259bc2ffa
commit 20bc4aa88f
3 files changed
+83 -85

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
import osu from 'node-os-utils';
import { ExtendedClient } from '../types/clientTypes';
import { fetchTotalGuildStatistics, fetchTotalPlayerStatistics } from '../utils/shardUtils';
import { getUptimeFormatted } from '../utils/system/getUptimeFormatted';
export async function getBotStatistics(client: ExtendedClient, version: string): Promise<string> {
const releaseVersion: string = version;
const { totalGuildCount, totalMemberCount } = await fetchTotalGuildStatistics(client);
return (
`**${totalGuildCount.toLocaleString('en-US')}** Joined servers\n` +
`**${totalMemberCount.toLocaleString('en-US')}** Total members\n` +
`**v${releaseVersion}** Release version`
);
}
export async function getPlayerStatistics(client: ExtendedClient): Promise<string> {
const { totalVoiceConnections, totalTracksInQueues, totalListeners } = await fetchTotalPlayerStatistics(client);
return (
`**${totalVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
`**${totalTracksInQueues.toLocaleString('en-US')}** Tracks in queues\n` +
`**${totalListeners.toLocaleString('en-US')}** Users listening`
);
}
export async function getSystemStatus(executionId: string, extended: boolean): Promise<string> {
if (!extended) {
const uptimeString: string = await getUptimeFormatted({ executionId });
const usedMemoryInMB: string = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US');
const cpuUsage: number = await osu.cpu.usage();
return (
`**${uptimeString}** Uptime\n` + `**${cpuUsage}%** CPU usage\n` + `**${usedMemoryInMB} MB** Memory usage`
);
}
const uptimeString: string = await getUptimeFormatted({ executionId });
const totalMemoryInMb: string = Math.ceil((await osu.mem.info()).totalMemMb).toLocaleString('en-US');
const cpuCores: number = osu.cpu.count();
const platform: string = osu.os.platform();
const usedMemoryInMB: string = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US');
const cpuUsage: number = await osu.cpu.usage();
return (
`**${platform}** Platform\n` +
`**${uptimeString}** Uptime\n` +
`**${cpuUsage}% @ ${cpuCores} cores** CPU usage\n` +
`**${usedMemoryInMB} / ${totalMemoryInMb} MB** Memory usage`
);
}
export function getDiscordStatus(client: ExtendedClient): string {
return `**${client!.ws.ping} ms** Discord API latency`;
}
+7 -28
View File
@@ -1,11 +1,9 @@
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import osu from 'node-os-utils';
// @ts-ignore
import { version } from '../../../../package.json';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { getBotStatistics, getDiscordStatus, getPlayerStatistics, getSystemStatus } from '../../../common/statusUtils';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { fetchTotalGuildStatistics, fetchTotalPlayerStatistics } from '../../../utils/shardUtils';
import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted';
class StatusCommand extends BaseSlashCommandInteraction {
constructor() {
@@ -17,29 +15,10 @@ class StatusCommand extends BaseSlashCommandInteraction {
const { executionId, interaction, client } = params;
const logger = this.getLogger(this.name, executionId, interaction);
const uptimeString: string = await getUptimeFormatted({ executionId });
const usedMemoryInMB: string = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US');
const cpuUsage: number = await osu.cpu.usage();
const releaseVersion: string = version;
const { totalGuildCount, totalMemberCount } = await fetchTotalGuildStatistics(client);
const { totalVoiceConnections, totalTracksInQueues, totalListeners } = await fetchTotalPlayerStatistics(client);
const botStatisticsEmbedString =
`**${totalGuildCount.toLocaleString('en-US')}** Joined servers\n` +
`**${totalMemberCount.toLocaleString('en-US')}** Total members\n` +
`**v${releaseVersion}** Release version`;
const playerStatisticsEmbedString =
`**${totalVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
`**${totalTracksInQueues.toLocaleString('en-US')}** Tracks in queues\n` +
`**${totalListeners.toLocaleString('en-US')}** Users listening`;
const systemStatusEmbedString =
`**${uptimeString}** Uptime\n` + `**${cpuUsage}%** CPU usage\n` + `**${usedMemoryInMB} MB** Memory usage`;
const discordWebsocketPingEmbedString: string = `**${client!.ws.ping} ms** Discord API latency`;
logger.debug('Transformed status info into embed description.');
const botStatisticsEmbedString = await getBotStatistics(client!, version);
const playerStatisticsEmbedString = await getPlayerStatistics(client!);
const systemStatusEmbedString = await getSystemStatus(executionId, false);
const discordStatus = getDiscordStatus(client!);
logger.debug('Responding with info embed.');
return await interaction.editReply({
@@ -57,7 +36,7 @@ class StatusCommand extends BaseSlashCommandInteraction {
},
{
name: `**${this.embedOptions.icons.discord} Discord status**`,
value: discordWebsocketPingEmbedString
value: discordStatus
}
)
.setColor(this.embedOptions.colors.info)
@@ -1,11 +1,9 @@
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import osu from 'node-os-utils';
// @ts-ignore
import { dependencies, version } from '../../../../package.json';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { getBotStatistics, getDiscordStatus, getPlayerStatistics, getSystemStatus } from '../../../common/statusUtils';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { fetchTotalGuildStatistics, fetchTotalPlayerStatistics } from '../../../utils/shardUtils';
import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted';
import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator';
class SystemStatusCommand extends BaseSlashCommandInteraction {
@@ -23,58 +21,11 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
await this.runValidators({ interaction, executionId }, [checkValidGuildId]);
// from normal /status command
const uptimeString: string = await getUptimeFormatted({ executionId });
const usedMemoryInMB: string = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US');
const cpuUsage: number = await osu.cpu.usage();
const releaseVersion: string = version;
const { totalGuildCount, totalMemberCount } = await fetchTotalGuildStatistics(client);
const { totalVoiceConnections, totalTracksInQueues, totalListeners } = await fetchTotalPlayerStatistics(client);
// specific to /systemstatus command
const totalMemoryInMb: string = Math.ceil((await osu.mem.info()).totalMemMb).toLocaleString('en-US');
const cpuCores: number = osu.cpu.count();
const platform: string = osu.os.platform();
const discordJsVersion: string = dependencies['discord.js'];
const opusVersion: string = dependencies['@discord-player/opus'];
const restVersion: string = dependencies['@discordjs/rest'];
const voiceVersion: string = dependencies['discord-voip'];
const discordPlayerVersion: string = dependencies['discord-player'];
const extractorVersion: string = dependencies['@discord-player/extractor'];
const mediaplexVersion: string = dependencies['mediaplex'];
const youtubeExtVersion: string = dependencies['youtube-ext'];
logger.debug('Fetching player statistics from all shards.');
const botStatisticsEmbedString =
`**${totalGuildCount.toLocaleString('en-US')}** Joined servers\n` +
`**${totalMemberCount.toLocaleString('en-US')}** Total members\n` +
`**v${releaseVersion}** Release version`;
const playerStatisticsEmbedString =
`**${totalVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
`**${totalTracksInQueues.toLocaleString('en-US')}** Tracks in queues\n` +
`**${totalListeners.toLocaleString('en-US')}** Users listening`;
const systemStatusEmbedString =
`**${platform}** Platform\n` +
`**${uptimeString}** Uptime\n` +
`**${cpuUsage}% @ ${cpuCores} cores** CPU usage\n` +
`**${usedMemoryInMB} / ${totalMemoryInMb} MB** Memory usage`;
const dependenciesEmbedString =
`**${discordJsVersion}** discord.js\n` +
`**┗ ${restVersion}** @discordjs/rest\n` +
`**${discordPlayerVersion}** discord-player\n` +
`**┗ ${opusVersion}** @discord-player/opus\n` +
`**┗ ${extractorVersion}** @discord-player/extractor\n` +
`**${voiceVersion}** discord-voip\n` +
`**${mediaplexVersion}** mediaplex\n` +
`**${youtubeExtVersion}** youtube-ext`;
const discordWebsocketPingEmbedString: string = `**${client!.ws.ping} ms** Discord API latency`;
logger.debug('Transformed system status into embed description.');
const botStatisticsEmbedString = await getBotStatistics(client!, version);
const playerStatisticsEmbedString = await getPlayerStatistics(client!);
const systemStatusEmbedString = await getSystemStatus(executionId, true);
const discordStatusEmbedString = getDiscordStatus(client!);
const dependenciesEmbedString = this.getDependencies();
logger.debug('Responding with info embed.');
return await interaction.editReply({
@@ -92,7 +43,7 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
},
{
name: `**${this.embedOptions.icons.discord} Discord status**`,
value: discordWebsocketPingEmbedString
value: discordStatusEmbedString
},
{
name: `**${this.embedOptions.icons.bot} Dependencies**`,
@@ -103,6 +54,19 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
]
});
}
private getDependencies(): string {
return (
`**${dependencies['discord.js']}** discord.js\n` +
`**┗ ${dependencies['@discordjs/rest']}** @discordjs/rest\n` +
`**${dependencies['discord-player']}** discord-player\n` +
`**┗ ${dependencies['@discord-player/opus']}** @discord-player/opus\n` +
`**┗ ${dependencies['@discord-player/extractor']}** @discord-player/extractor\n` +
`**${dependencies['discord-voip']}** discord-voip\n` +
`**${dependencies['mediaplex']}** mediaplex\n` +
`**${dependencies['youtube-ext']}** youtube-ext`
);
}
}
export default new SystemStatusCommand();