From aa82c6d44ddc11c698af4ee775b811a1ca72b038 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 23 Jul 2023 16:29:15 +0200 Subject: [PATCH] fix: create copy of status system command to be used by users --- src/commands/{system => info}/status.js | 41 +++--------- src/commands/system/systemstatus.js | 84 +++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 32 deletions(-) rename src/commands/{system => info}/status.js (75%) create mode 100644 src/commands/system/systemstatus.js diff --git a/src/commands/system/status.js b/src/commands/info/status.js similarity index 75% rename from src/commands/system/status.js rename to src/commands/info/status.js index ed20378..e37a275 100644 --- a/src/commands/system/status.js +++ b/src/commands/info/status.js @@ -1,24 +1,18 @@ const logger = require('../../services/logger'); const { embedOptions } = require('../../config'); -const { notValidGuildId } = require('../../utils/validation/systemCommandValidator'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const osu = require('node-os-utils'); const { version, dependencies } = require('../../../package.json'); module.exports = { - isSystemCommand: true, - isNew: false, + isNew: true, isBeta: false, data: new SlashCommandBuilder() .setName('status') - .setDescription('Show bot status.') + .setDescription('Show the bot and system status.') .setDMPermission(false) .setNSFW(false), execute: async ({ interaction, client }) => { - if (await notValidGuildId(interaction)) { - return; - } - const interactionLatency = Date.now() - interaction.createdTimestamp; const uptimeInSeconds = process.uptime(); let uptimeDate = new Date(0); @@ -37,34 +31,17 @@ module.exports = { let guildCount = 0; await client.shard - .broadcastEval((c) => { - return c.guilds.cache.map((guild) => { - return guild.id; - /* - const queue = useQueue(guild.id); - let shardVoiceConnections = 0; - if (queue) { - activeVoiceConnections++; - } - return shardVoiceConnections; - */ - }); + .broadcastEval(() => { + /* eslint-disable no-undef */ + return player.generateStatistics().queues.length; }) .then((results) => { - // array of arrays with guild ids for each shard - logger.debug(results, 'Shard guild ids'); + activeVoiceConnections = results.reduce( + (acc, activeVoiceConnections) => acc + activeVoiceConnections, + 0 + ); }); - /* - client.guilds.cache.forEach((guild) => { - const queue = useQueue(guild.id); - - if (queue) { - activeVoiceConnections++; - } - }); - */ - await client.shard .fetchClientValues('guilds.cache.size') .then((results) => { diff --git a/src/commands/system/systemstatus.js b/src/commands/system/systemstatus.js new file mode 100644 index 0000000..85c36f1 --- /dev/null +++ b/src/commands/system/systemstatus.js @@ -0,0 +1,84 @@ +const logger = require('../../services/logger'); +const { embedOptions } = require('../../config'); +const { notValidGuildId } = require('../../utils/validation/systemCommandValidator'); +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const osu = require('node-os-utils'); +const { version, dependencies } = require('../../../package.json'); + +module.exports = { + isSystemCommand: true, + isNew: false, + isBeta: false, + data: new SlashCommandBuilder() + .setName('systemstatus') + .setDescription('Show the bot and system status.') + .setDMPermission(false) + .setNSFW(false), + execute: async ({ interaction, client }) => { + if (await notValidGuildId(interaction)) { + return; + } + + const interactionLatency = Date.now() - interaction.createdTimestamp; + const uptimeInSeconds = process.uptime(); + let uptimeDate = new Date(0); + uptimeDate.setSeconds(uptimeInSeconds.toFixed(0)); + const uptimeString = uptimeDate.toISOString().substring(11, 19); + const nodeProcessMemUsageInMb = (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2); + const usedMemoryInMB = Math.ceil((await osu.mem.info()).usedMemMb).toLocaleString('en-US'); + const cpuUsage = await osu.cpu.usage(); + const cpuCores = await osu.cpu.count(); + const platform = await osu.os.platform(); + const releaseVersion = version; + const discordJsVersion = dependencies['discord.js']; + const discordPlayerVersion = dependencies['discord-player']; + const discordApiLatency = client.ws.ping; + let activeVoiceConnections = 0; + let guildCount = 0; + + await client.shard + .broadcastEval(() => { + /* eslint-disable no-undef */ + return player.generateStatistics().queues.length; + }) + .then((results) => { + activeVoiceConnections = results.reduce( + (acc, activeVoiceConnections) => acc + activeVoiceConnections, + 0 + ); + }); + + await client.shard + .fetchClientValues('guilds.cache.size') + .then((results) => { + guildCount = results.reduce((acc, guildCount) => acc + guildCount, 0); + }) + .catch((error) => { + logger.error(error, `[Shard ${client.shard.ids[0]}] Failed to fetch client values from shards.`); + }); + + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${embedOptions.icons.bot} System and bot status**\n` + + `Uptime: \`${uptimeString}\`\n` + + `Platform: \`${platform}\`\n` + + `CPU cores: \`${cpuCores}\`\n` + + `CPU usage: \`${cpuUsage}%\`\n` + + `Node.js mem usage: \`${nodeProcessMemUsageInMb} MB\`\n` + + `System mem usage: \`${usedMemoryInMB} MB\`\n` + + `Bot version: \`v${releaseVersion}\`\n` + + `Node.js version: \`${process.version}\`\n` + + `Discord.js version: \`${discordJsVersion}\`\n` + + `Discord-player version: \`${discordPlayerVersion}\`\n` + + `Discord API latency: \`${discordApiLatency} ms\`\n` + + `Interaction latency: \`${interactionLatency} ms\`\n` + + `Active voice connections: \`${activeVoiceConnections}\`\n` + + `Joined guilds: \`${guildCount}\`` + ) + .setColor(embedOptions.colors.info) + ] + }); + } +};