From 0bdc5501bee6874ab9f77812a0235e414f30e0b3 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 27 Aug 2023 13:28:29 +0200 Subject: [PATCH] fix: annotated system commands --- src/commands/player/filters.ts | 2 +- src/commands/player/nowplaying.ts | 14 ++++++------- src/commands/system/guilds.ts | 15 +++++++++----- src/commands/system/reload.ts | 22 ++++++++++++++------ src/commands/system/shards.ts | 31 ++++++++++++++++++++--------- src/commands/system/systemstatus.ts | 25 ++++++++++++++--------- src/types/clientTypes.ts | 10 +++++++--- src/types/commandTypes.ts | 12 +++++++++++ src/types/globalTypes.ts | 5 +++++ 9 files changed, 96 insertions(+), 40 deletions(-) create mode 100644 src/types/globalTypes.ts diff --git a/src/commands/player/filters.ts b/src/commands/player/filters.ts index 4651dd0..00aaa08 100644 --- a/src/commands/player/filters.ts +++ b/src/commands/player/filters.ts @@ -1,5 +1,5 @@ import config from 'config'; -import { EmbedOptions } from '../../types/configTypes'; +import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); import { notInVoiceChannel, notInSameVoiceChannel } from '../../utils/validation/voiceChannelValidator'; diff --git a/src/commands/player/nowplaying.ts b/src/commands/player/nowplaying.ts index dee9a61..16a7cd0 100644 --- a/src/commands/player/nowplaying.ts +++ b/src/commands/player/nowplaying.ts @@ -138,8 +138,8 @@ module.exports = { queue.repeatMode === 0 ? '' : `**${ - queue.repeatMode === 3 ? embedOptions.icons.autoplay : embedOptions.icons.loop - } Looping**\nLoop mode is set to ${loopModeUserString}. You can change it with **\`/loop\`**.` + queue.repeatMode === 3 ? embedOptions.icons.autoplay : embedOptions.icons.loop + } Looping**\nLoop mode is set to ${loopModeUserString}. You can change it with **\`/loop\`**.` }` ) .addFields( @@ -170,7 +170,7 @@ module.exports = { components: [nowPlayingActionRow] }); - logger.debug(`User used command '${interaction.commandName}', finished sending response.`); + logger.debug('Finished sending response.'); const collectorFilter = (i) => i.user.id === interaction.user.id; try { @@ -249,10 +249,10 @@ module.exports = { queue.repeatMode === 0 ? '' : `\n\n**${ - queue.repeatMode === 3 - ? embedOptions.icons.autoplaying - : embedOptions.icons.looping - } Looping**\nLoop mode is set to ${repeatModeUserString}. You can change it with **\`/loop\`**.` + queue.repeatMode === 3 + ? embedOptions.icons.autoplaying + : embedOptions.icons.looping + } Looping**\nLoop mode is set to ${repeatModeUserString}. You can change it with **\`/loop\`**.` }` ) .setThumbnail(skippedTrack.thumbnail) diff --git a/src/commands/system/guilds.ts b/src/commands/system/guilds.ts index a468f5b..d9745a1 100644 --- a/src/commands/system/guilds.ts +++ b/src/commands/system/guilds.ts @@ -2,7 +2,7 @@ import config from 'config'; import { EmbedOptions } from '../../types/configTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; -import{ SlashCommandBuilder, EmbedBuilder } from 'discord.js'; +import { SlashCommandBuilder, EmbedBuilder, Guild } from 'discord.js'; import loggerModule from '../../services/logger'; import { CommandParams } from '../../types/commandTypes'; @@ -21,17 +21,22 @@ module.exports = { module: 'slashCommand', name: '/guilds', executionId: executionId, - shardId: interaction.guild.shardId, - guildId: interaction.guild.id + shardId: interaction.guild?.shardId, + guildId: interaction.guild?.id }); if (await notValidGuildId({ interaction, executionId })) { return; } - let shardGuilds = []; + let shardGuilds: Guild[] = []; let totalGuildCount = 0; + if (!client || !client.shard) { + logger.error('Client is undefined or does not have shard property.'); + return; + } + logger.debug('Fetching guilds from all shards.'); await client.shard .broadcastEval((c) => { @@ -40,7 +45,7 @@ module.exports = { }); }) .then((guilds) => { - shardGuilds = guilds.flat(1); + shardGuilds = guilds.flat(1) as Guild[]; }); totalGuildCount = shardGuilds.length; diff --git a/src/commands/system/reload.ts b/src/commands/system/reload.ts index 0de7fa3..c89f4cf 100644 --- a/src/commands/system/reload.ts +++ b/src/commands/system/reload.ts @@ -2,9 +2,10 @@ import config from 'config'; import { EmbedOptions } from '../../types/configTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; -import{ SlashCommandBuilder, EmbedBuilder } from 'discord.js'; +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; import loggerModule from '../../services/logger'; import { CommandParams } from '../../types/commandTypes'; +import { ExtendedClient } from '../../types/clientTypes'; module.exports = { isSystemCommand: true, @@ -21,19 +22,28 @@ module.exports = { module: 'slashCommand', name: '/reload', executionId: executionId, - shardId: interaction.guild.shardId, - guildId: interaction.guild.id + shardId: interaction.guild?.shardId, + guildId: interaction.guild?.id }); if (await notValidGuildId({ interaction, executionId })) { return; } + if (!client || !client.shard) { + logger.error('Client is undefined or does not have shard property.'); + return; + } + try { logger.debug('Reloading commands across all shards.'); await client.shard .broadcastEval( - async (shardClient, { executionId }) => { + async (shardClient: ExtendedClient, { executionId }) => { + if (!shardClient.registerClientCommands) { + return; + } + shardClient.registerClientCommands({ client: shardClient, executionId }); }, { context: { executionId: executionId } } @@ -58,12 +68,12 @@ module.exports = { }); } - const commands = client.commands.map((command) => { + const commands = client.commands?.map((command) => { const params = command.data.options[0] ? `**\`${command.data.options[0].name}\`**` + ' ' : ''; return `- **\`/${command.data.name}\`** ${params}- ${command.data.description}`; }); - const embedDescription = `**${embedOptions.icons.bot} Reloaded commands**\n` + commands.join('\n'); + const embedDescription = `**${embedOptions.icons.bot} Reloaded commands**\n` + commands?.join('\n'); logger.debug('Responding with success embed.'); return await interaction.editReply({ diff --git a/src/commands/system/shards.ts b/src/commands/system/shards.ts index 458d32d..5434278 100644 --- a/src/commands/system/shards.ts +++ b/src/commands/system/shards.ts @@ -2,9 +2,10 @@ import config from 'config'; import { EmbedOptions } from '../../types/configTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; -import{ SlashCommandBuilder, EmbedBuilder } from 'discord.js'; +import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; +import { CommandParams, ShardInfo } from '../../types/commandTypes'; +import { ExtendedClient } from '../../types/clientTypes'; module.exports = { isSystemCommand: true, @@ -38,23 +39,35 @@ module.exports = { module: 'slashCommand', name: '/shards', executionId: executionId, - shardId: interaction.guild.shardId, - guildId: interaction.guild.id + shardId: interaction.guild?.shardId, + guildId: interaction.guild?.id }); if (await notValidGuildId({ interaction, executionId })) { return; } - let shardInfoList = []; + let shardInfoList: ShardInfo[] = []; + + if (!client || !client.shard) { + logger.error('Client is undefined or does not have shard property.'); + return; + } logger.debug('Fetching player statistics and client values from each shard.'); try { await client.shard - .broadcastEval((shardClient) => { + .broadcastEval((shardClient: ExtendedClient) => { + if (!shardClient.shard) { + return; + } + /* eslint-disable no-undef */ const playerStats = player.generateStatistics(); - const nodeProcessMemUsageInMb = (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2); + const nodeProcessMemUsageInMb = parseFloat( + (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2) + ); + const shardInfo = { shardId: shardClient.shard.ids[0], memUsage: nodeProcessMemUsageInMb, @@ -70,7 +83,7 @@ module.exports = { return shardInfo; }) .then((results) => { - shardInfoList = results; + shardInfoList = results.filter(Boolean) as ShardInfo[]; switch (interaction.options.getString('sort')) { case 'memory': @@ -132,7 +145,7 @@ module.exports = { const evenShardIndexes = currentPageShards.filter((shard, index) => index % 2 === 0); const oddShardIndexes = currentPageShards.filter((shard, index) => index % 2 !== 0); - function shardInfoToString(shard) { + function shardInfoToString(shard: ShardInfo) { let string = ''; string += `**Shard ${shard.shardId}** - Guilds: ${shard.guildCount.toLocaleString( 'en-US' diff --git a/src/commands/system/systemstatus.ts b/src/commands/system/systemstatus.ts index ee60f42..4fb6370 100644 --- a/src/commands/system/systemstatus.ts +++ b/src/commands/system/systemstatus.ts @@ -1,7 +1,7 @@ import config from 'config'; import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; import { getUptimeFormatted } from '../../utils/system/getUptimeFormatted'; -import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; +import { SlashCommandBuilder, EmbedBuilder, Guild, Collection } from 'discord.js'; import osu from 'node-os-utils'; // @ts-ignore import { version, dependencies } from '../../../package.json'; @@ -26,8 +26,8 @@ module.exports = { module: 'slashCommand', name: '/systemstatus', executionId: executionId, - shardId: interaction.guild.shardId, - guildId: interaction.guild.id + shardId: interaction.guild?.shardId, + guildId: interaction.guild?.id }); if (await notValidGuildId({ interaction, executionId })) { @@ -59,15 +59,21 @@ module.exports = { const distubeYtdlVersion = dependencies['@distube/ytdl-core']; logger.debug('Fetching player statistics from all shards.'); + + if (!client || !client.shard) { + logger.error('Client is undefined or does not have shard property.'); + return; + } + await client.shard .broadcastEval(() => { /* eslint-disable no-undef */ return player.generateStatistics(); }) .then((results) => { - const queueCountList = []; - const trackCountList = []; - const listenerCountList = []; + const queueCountList: number[] = []; + const trackCountList: number[] = []; + const listenerCountList: number[] = []; results.map((result) => { queueCountList.push(result.queues.length); if (result.queues.length > 0) { @@ -89,10 +95,11 @@ module.exports = { await client.shard .fetchClientValues('guilds.cache') .then((results) => { - results.map((guildCache) => { + const guildCaches = results as Collection[]; + guildCaches.map((guildCache) => { if (guildCache) { - guildCount += guildCache.length; - memberCount += guildCache.reduce((acc, guildCache) => acc + guildCache.memberCount, 0); + guildCount += guildCache.size; + memberCount += guildCache.reduce((acc, guild) => acc + guild.memberCount, 0); } }); logger.debug('Successfully fetched client values from shards.'); diff --git a/src/types/clientTypes.ts b/src/types/clientTypes.ts index 2429adb..9b38d2f 100644 --- a/src/types/clientTypes.ts +++ b/src/types/clientTypes.ts @@ -1,16 +1,20 @@ -import { Client, Collection } from 'discord.js'; +import { Client, Collection, SharedNameAndDescription } from 'discord.js'; type RegisterClientCommandsFunction = (params: { client: Client; executionId: string }) => void; export interface Command { isNew: boolean; isBeta: boolean; - data: object; + data: { + name: string; + description: string; + options: SharedNameAndDescription[] + }; execute: (params: { interaction: object; client: ExtendedClient | undefined; executionId: string }) => void; autocomplete?: (params: { interaction: object; executionId: string }) => void; } export interface ExtendedClient extends Client { registerClientCommands?: RegisterClientCommandsFunction; - commands?: Collection; + commands?: Collection; } diff --git a/src/types/commandTypes.ts b/src/types/commandTypes.ts index e17181d..d7e5e41 100644 --- a/src/types/commandTypes.ts +++ b/src/types/commandTypes.ts @@ -12,3 +12,15 @@ export interface CommandAutocompleteParams { client: ExtendedClient | undefined; executionId: string; } + +export interface ShardInfo { + shardId: number; + memUsage: number; + guildCount: number; + guildMemberCount: number; + playerStatistics: { + activeVoiceConnections: number; + totalTracks: number; + totalListeners: number; + }; +} diff --git a/src/types/globalTypes.ts b/src/types/globalTypes.ts new file mode 100644 index 0000000..749c65f --- /dev/null +++ b/src/types/globalTypes.ts @@ -0,0 +1,5 @@ +import { Player } from 'discord-player'; + +declare global { + const player: Player; +}