mirror of
https://github.com/darksaid98/cadence-discord-bot.git
synced 2026-12-03 14:38:49 +00:00
fix: extracted logic for retrieving shard status
This commit is contained in:
1 parent
922337769d
commit
8da6f44ebb
4 files changed
+142
-125
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js';
|
||||
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||
import osu from 'node-os-utils';
|
||||
// @ts-ignore
|
||||
import { version } from '../../../../package.json';
|
||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||
import { fetchTotalGuildStatistics, fetchTotalPlayerStatistics } from '../../../utils/shardUtils';
|
||||
import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted';
|
||||
|
||||
class StatusCommand extends BaseSlashCommandInteraction {
|
||||
@@ -20,86 +21,43 @@ class StatusCommand extends BaseSlashCommandInteraction {
|
||||
const usedMemoryInMB: string = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US');
|
||||
const cpuUsage: number = await osu.cpu.usage();
|
||||
const releaseVersion: string = version;
|
||||
let guildCount: number = 0;
|
||||
let memberCount: number = 0;
|
||||
let activeVoiceConnections: number = 0;
|
||||
let totalTracks: number = 0;
|
||||
let totalListeners: number = 0;
|
||||
const { totalGuildCount, totalMemberCount } = await fetchTotalGuildStatistics(client);
|
||||
const { totalVoiceConnections, totalTracksInQueues, totalListeners } = await fetchTotalPlayerStatistics(client);
|
||||
|
||||
await client!
|
||||
.shard!.broadcastEval(() => {
|
||||
/* eslint-disable no-undef */
|
||||
return player.generateStatistics();
|
||||
})
|
||||
.then((results) => {
|
||||
results.map((result) => {
|
||||
activeVoiceConnections += result.queues.length;
|
||||
result.queues.map((queue) => {
|
||||
totalTracks += queue.status.playing ? queue.tracksCount + 1 : queue.tracksCount;
|
||||
totalListeners += queue.listeners;
|
||||
});
|
||||
});
|
||||
|
||||
logger.debug('Successfully fetched player statistics from shards.');
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error, 'Failed to fetch player statistics from shards.');
|
||||
});
|
||||
|
||||
await client!
|
||||
.shard!.fetchClientValues('guilds.cache')
|
||||
.then((results) => {
|
||||
const guildCaches: Guild[][] = results as Guild[][];
|
||||
guildCaches.map((guildCache: Guild[]) => {
|
||||
if (guildCache) {
|
||||
guildCount += guildCache.length;
|
||||
memberCount += guildCache.reduce((acc: number, guild: Guild) => acc + guild.memberCount, 0);
|
||||
}
|
||||
});
|
||||
|
||||
logger.debug('Successfully fetched client values from shards.');
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error, 'Failed to fetch client values from shards.');
|
||||
});
|
||||
|
||||
const botStatusString =
|
||||
`**${guildCount.toLocaleString('en-US')}** Joined servers\n` +
|
||||
`**${memberCount.toLocaleString('en-US')}** Total members\n` +
|
||||
const botStatisticsEmbedString =
|
||||
`**${totalGuildCount.toLocaleString('en-US')}** Joined servers\n` +
|
||||
`**${totalMemberCount.toLocaleString('en-US')}** Total members\n` +
|
||||
`**v${releaseVersion}** Release version`;
|
||||
|
||||
const queueStatusString =
|
||||
`**${activeVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
|
||||
`**${totalTracks.toLocaleString('en-US')}** Tracks in queues\n` +
|
||||
const playerStatisticsEmbedString =
|
||||
`**${totalVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
|
||||
`**${totalTracksInQueues.toLocaleString('en-US')}** Tracks in queues\n` +
|
||||
`**${totalListeners.toLocaleString('en-US')}** Users listening`;
|
||||
|
||||
const systemStatusString =
|
||||
const systemStatusEmbedString =
|
||||
`**${uptimeString}** Uptime\n` + `**${cpuUsage}%** CPU usage\n` + `**${usedMemoryInMB} MB** Memory usage`;
|
||||
|
||||
const discordStatusString: string = `**${client!.ws.ping} ms** Discord API latency`;
|
||||
const discordWebsocketPingEmbedString: string = `**${client!.ws.ping} ms** Discord API latency`;
|
||||
|
||||
logger.debug('Transformed status into into embed description.');
|
||||
logger.debug('Transformed status info into embed description.');
|
||||
|
||||
logger.debug('Responding with info embed.');
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(`**${this.embedOptions.icons.bot} Bot status**\n` + botStatusString)
|
||||
.setDescription(`**${this.embedOptions.icons.bot} Bot status**\n` + botStatisticsEmbedString)
|
||||
.addFields(
|
||||
{
|
||||
name: `**${this.embedOptions.icons.queue} Queue status**`,
|
||||
value: queueStatusString,
|
||||
inline: false
|
||||
value: playerStatisticsEmbedString
|
||||
},
|
||||
{
|
||||
name: `**${this.embedOptions.icons.server} System status**`,
|
||||
value: systemStatusString,
|
||||
inline: false
|
||||
value: systemStatusEmbedString
|
||||
},
|
||||
{
|
||||
name: `**${this.embedOptions.icons.discord} Discord status**`,
|
||||
value: discordStatusString,
|
||||
inline: false
|
||||
value: discordWebsocketPingEmbedString
|
||||
}
|
||||
)
|
||||
.setColor(this.embedOptions.colors.info)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js';
|
||||
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
|
||||
import osu from 'node-os-utils';
|
||||
// @ts-ignore
|
||||
import { dependencies, version } from '../../../../package.json';
|
||||
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
|
||||
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
|
||||
import { fetchTotalGuildStatistics, fetchTotalPlayerStatistics } from '../../../utils/shardUtils';
|
||||
import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted';
|
||||
import { checkValidGuildId } from '../../../utils/validation/systemCommandValidator';
|
||||
|
||||
@@ -14,31 +15,26 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
|
||||
.setDescription('Show operational status of the bot with additional technical information.');
|
||||
const isSystemCommand: boolean = true;
|
||||
super(data, isSystemCommand);
|
||||
|
||||
this.validators = [(args) => checkValidGuildId(args)];
|
||||
}
|
||||
|
||||
async execute(params: BaseSlashCommandParams): BaseSlashCommandReturnType {
|
||||
const { executionId, interaction, client } = params;
|
||||
const logger = this.getLogger(this.name, executionId, interaction);
|
||||
|
||||
await this.runValidators({ interaction, executionId });
|
||||
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;
|
||||
let guildCount: number = 0;
|
||||
let memberCount: number = 0;
|
||||
let activeVoiceConnections: number = 0;
|
||||
let totalTracks: number = 0;
|
||||
let totalListeners: number = 0;
|
||||
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 = await osu.cpu.count();
|
||||
const platform: string = await osu.os.platform();
|
||||
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'];
|
||||
@@ -50,60 +46,23 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
|
||||
|
||||
logger.debug('Fetching player statistics from all shards.');
|
||||
|
||||
await client!
|
||||
.shard!.broadcastEval(() => {
|
||||
/* eslint-disable no-undef */
|
||||
return player.generateStatistics();
|
||||
})
|
||||
.then((results) => {
|
||||
results.map((result) => {
|
||||
activeVoiceConnections += result.queues.length;
|
||||
result.queues.map((queue) => {
|
||||
totalTracks += queue.status.playing ? queue.tracksCount + 1 : queue.tracksCount;
|
||||
totalListeners += queue.listeners;
|
||||
});
|
||||
});
|
||||
|
||||
logger.debug('Successfully fetched player statistics from shards.');
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error, 'Failed to fetch player statistics from shards.');
|
||||
});
|
||||
|
||||
logger.debug('Fetching client values from all shards.');
|
||||
await client!
|
||||
.shard!.fetchClientValues('guilds.cache')
|
||||
.then((results) => {
|
||||
const guildCaches: Guild[][] = results as Guild[][];
|
||||
guildCaches.map((guildCache: Guild[]) => {
|
||||
if (guildCache) {
|
||||
guildCount += guildCache.length;
|
||||
memberCount += guildCache.reduce((acc: number, guild: Guild) => acc + guild.memberCount, 0);
|
||||
}
|
||||
});
|
||||
logger.debug('Successfully fetched client values from shards.');
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error, 'Failed to fetch client values from shards.');
|
||||
});
|
||||
|
||||
const botStatusString =
|
||||
`**${guildCount.toLocaleString('en-US')}** Joined servers\n` +
|
||||
`**${memberCount.toLocaleString('en-US')}** Total members\n` +
|
||||
const botStatisticsEmbedString =
|
||||
`**${totalGuildCount.toLocaleString('en-US')}** Joined servers\n` +
|
||||
`**${totalMemberCount.toLocaleString('en-US')}** Total members\n` +
|
||||
`**v${releaseVersion}** Release version`;
|
||||
|
||||
const queueStatusString =
|
||||
`**${activeVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
|
||||
`**${totalTracks.toLocaleString('en-US')}** Tracks in queues\n` +
|
||||
const playerStatisticsEmbedString =
|
||||
`**${totalVoiceConnections.toLocaleString('en-US')}** Voice connections\n` +
|
||||
`**${totalTracksInQueues.toLocaleString('en-US')}** Tracks in queues\n` +
|
||||
`**${totalListeners.toLocaleString('en-US')}** Users listening`;
|
||||
|
||||
const systemStatusString =
|
||||
const systemStatusEmbedString =
|
||||
`**${platform}** Platform\n` +
|
||||
`**${uptimeString}** Uptime\n` +
|
||||
`**${cpuUsage}% @ ${cpuCores} cores** CPU usage\n` +
|
||||
`**${usedMemoryInMB} / ${totalMemoryInMb} MB** Memory usage`;
|
||||
|
||||
const dependenciesString =
|
||||
const dependenciesEmbedString =
|
||||
`**${discordJsVersion}** discord.js\n` +
|
||||
`**┗ ${restVersion}** @discordjs/rest\n` +
|
||||
`**${discordPlayerVersion}** discord-player\n` +
|
||||
@@ -113,7 +72,7 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
|
||||
`**${mediaplexVersion}** mediaplex\n` +
|
||||
`**${distubeYtdlVersion}** @distube/ytdl-core`;
|
||||
|
||||
const discordStatusString: string = `**${client!.ws.ping} ms** Discord API latency`;
|
||||
const discordWebsocketPingEmbedString: string = `**${client!.ws.ping} ms** Discord API latency`;
|
||||
|
||||
logger.debug('Transformed system status into embed description.');
|
||||
|
||||
@@ -121,27 +80,23 @@ class SystemStatusCommand extends BaseSlashCommandInteraction {
|
||||
return await interaction.editReply({
|
||||
embeds: [
|
||||
new EmbedBuilder()
|
||||
.setDescription(`**${this.embedOptions.icons.bot} Bot status**\n` + botStatusString)
|
||||
.setDescription(`**${this.embedOptions.icons.bot} Bot status**\n` + botStatisticsEmbedString)
|
||||
.addFields(
|
||||
{
|
||||
name: `**${this.embedOptions.icons.queue} Queue status**`,
|
||||
value: queueStatusString,
|
||||
inline: false
|
||||
value: playerStatisticsEmbedString
|
||||
},
|
||||
{
|
||||
name: `**${this.embedOptions.icons.server} System status**`,
|
||||
value: systemStatusString,
|
||||
inline: false
|
||||
value: systemStatusEmbedString
|
||||
},
|
||||
{
|
||||
name: `**${this.embedOptions.icons.discord} Discord status**`,
|
||||
value: discordStatusString,
|
||||
inline: false
|
||||
value: discordWebsocketPingEmbedString
|
||||
},
|
||||
{
|
||||
name: `**${this.embedOptions.icons.bot} Dependencies**`,
|
||||
value: dependenciesString,
|
||||
inline: false
|
||||
value: dependenciesEmbedString
|
||||
}
|
||||
)
|
||||
.setColor(this.embedOptions.colors.info)
|
||||
|
||||
Reference in new issue
Block a user