fix: annotated system commands

This commit is contained in:
Marius Begby committed 2023-08-27 13:28:29 +02:00
1 parent f4f418292a
commit 0bdc5501be
9 files changed
+96 -40

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
import config from 'config'; import config from 'config';
import { EmbedOptions } from '../../types/configTypes'; import { EmbedOptions, FFmpegFilterOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions'); const embedOptions: EmbedOptions = config.get('embedOptions');
const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');
import { notInVoiceChannel, notInSameVoiceChannel } from '../../utils/validation/voiceChannelValidator'; import { notInVoiceChannel, notInSameVoiceChannel } from '../../utils/validation/voiceChannelValidator';
+7 -7
View File
@@ -138,8 +138,8 @@ module.exports = {
queue.repeatMode === 0 queue.repeatMode === 0
? '' ? ''
: `**${ : `**${
queue.repeatMode === 3 ? embedOptions.icons.autoplay : embedOptions.icons.loop queue.repeatMode === 3 ? embedOptions.icons.autoplay : embedOptions.icons.loop
} Looping**\nLoop mode is set to ${loopModeUserString}. You can change it with **\`/loop\`**.` } Looping**\nLoop mode is set to ${loopModeUserString}. You can change it with **\`/loop\`**.`
}` }`
) )
.addFields( .addFields(
@@ -170,7 +170,7 @@ module.exports = {
components: [nowPlayingActionRow] 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; const collectorFilter = (i) => i.user.id === interaction.user.id;
try { try {
@@ -249,10 +249,10 @@ module.exports = {
queue.repeatMode === 0 queue.repeatMode === 0
? '' ? ''
: `\n\n**${ : `\n\n**${
queue.repeatMode === 3 queue.repeatMode === 3
? embedOptions.icons.autoplaying ? embedOptions.icons.autoplaying
: embedOptions.icons.looping : embedOptions.icons.looping
} Looping**\nLoop mode is set to ${repeatModeUserString}. You can change it with **\`/loop\`**.` } Looping**\nLoop mode is set to ${repeatModeUserString}. You can change it with **\`/loop\`**.`
}` }`
) )
.setThumbnail(skippedTrack.thumbnail) .setThumbnail(skippedTrack.thumbnail)
+10 -5
View File
@@ -2,7 +2,7 @@ import config from 'config';
import { EmbedOptions } from '../../types/configTypes'; import { EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions'); const embedOptions: EmbedOptions = config.get('embedOptions');
import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; 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 loggerModule from '../../services/logger';
import { CommandParams } from '../../types/commandTypes'; import { CommandParams } from '../../types/commandTypes';
@@ -21,17 +21,22 @@ module.exports = {
module: 'slashCommand', module: 'slashCommand',
name: '/guilds', name: '/guilds',
executionId: executionId, executionId: executionId,
shardId: interaction.guild.shardId, shardId: interaction.guild?.shardId,
guildId: interaction.guild.id guildId: interaction.guild?.id
}); });
if (await notValidGuildId({ interaction, executionId })) { if (await notValidGuildId({ interaction, executionId })) {
return; return;
} }
let shardGuilds = []; let shardGuilds: Guild[] = [];
let totalGuildCount = 0; 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.'); logger.debug('Fetching guilds from all shards.');
await client.shard await client.shard
.broadcastEval((c) => { .broadcastEval((c) => {
@@ -40,7 +45,7 @@ module.exports = {
}); });
}) })
.then((guilds) => { .then((guilds) => {
shardGuilds = guilds.flat(1); shardGuilds = guilds.flat(1) as Guild[];
}); });
totalGuildCount = shardGuilds.length; totalGuildCount = shardGuilds.length;
+16 -6
View File
@@ -2,9 +2,10 @@ import config from 'config';
import { EmbedOptions } from '../../types/configTypes'; import { EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions'); const embedOptions: EmbedOptions = config.get('embedOptions');
import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; import { notValidGuildId } from '../../utils/validation/systemCommandValidator';
import{ SlashCommandBuilder, EmbedBuilder } from 'discord.js'; import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import loggerModule from '../../services/logger'; import loggerModule from '../../services/logger';
import { CommandParams } from '../../types/commandTypes'; import { CommandParams } from '../../types/commandTypes';
import { ExtendedClient } from '../../types/clientTypes';
module.exports = { module.exports = {
isSystemCommand: true, isSystemCommand: true,
@@ -21,19 +22,28 @@ module.exports = {
module: 'slashCommand', module: 'slashCommand',
name: '/reload', name: '/reload',
executionId: executionId, executionId: executionId,
shardId: interaction.guild.shardId, shardId: interaction.guild?.shardId,
guildId: interaction.guild.id guildId: interaction.guild?.id
}); });
if (await notValidGuildId({ interaction, executionId })) { if (await notValidGuildId({ interaction, executionId })) {
return; return;
} }
if (!client || !client.shard) {
logger.error('Client is undefined or does not have shard property.');
return;
}
try { try {
logger.debug('Reloading commands across all shards.'); logger.debug('Reloading commands across all shards.');
await client.shard await client.shard
.broadcastEval( .broadcastEval(
async (shardClient, { executionId }) => { async (shardClient: ExtendedClient, { executionId }) => {
if (!shardClient.registerClientCommands) {
return;
}
shardClient.registerClientCommands({ client: shardClient, executionId }); shardClient.registerClientCommands({ client: shardClient, executionId });
}, },
{ context: { executionId: 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}\`**` + ' ' : ''; const params = command.data.options[0] ? `**\`${command.data.options[0].name}\`**` + ' ' : '';
return `- **\`/${command.data.name}\`** ${params}- ${command.data.description}`; 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.'); logger.debug('Responding with success embed.');
return await interaction.editReply({ return await interaction.editReply({
+22 -9
View File
@@ -2,9 +2,10 @@ import config from 'config';
import { EmbedOptions } from '../../types/configTypes'; import { EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions'); const embedOptions: EmbedOptions = config.get('embedOptions');
import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; import { notValidGuildId } from '../../utils/validation/systemCommandValidator';
import{ SlashCommandBuilder, EmbedBuilder } from 'discord.js'; import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import loggerModule from '../../services/logger'; import loggerModule from '../../services/logger';
import { CommandParams } from '../../types/commandTypes'; import { CommandParams, ShardInfo } from '../../types/commandTypes';
import { ExtendedClient } from '../../types/clientTypes';
module.exports = { module.exports = {
isSystemCommand: true, isSystemCommand: true,
@@ -38,23 +39,35 @@ module.exports = {
module: 'slashCommand', module: 'slashCommand',
name: '/shards', name: '/shards',
executionId: executionId, executionId: executionId,
shardId: interaction.guild.shardId, shardId: interaction.guild?.shardId,
guildId: interaction.guild.id guildId: interaction.guild?.id
}); });
if (await notValidGuildId({ interaction, executionId })) { if (await notValidGuildId({ interaction, executionId })) {
return; 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.'); logger.debug('Fetching player statistics and client values from each shard.');
try { try {
await client.shard await client.shard
.broadcastEval((shardClient) => { .broadcastEval((shardClient: ExtendedClient) => {
if (!shardClient.shard) {
return;
}
/* eslint-disable no-undef */ /* eslint-disable no-undef */
const playerStats = player.generateStatistics(); 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 = { const shardInfo = {
shardId: shardClient.shard.ids[0], shardId: shardClient.shard.ids[0],
memUsage: nodeProcessMemUsageInMb, memUsage: nodeProcessMemUsageInMb,
@@ -70,7 +83,7 @@ module.exports = {
return shardInfo; return shardInfo;
}) })
.then((results) => { .then((results) => {
shardInfoList = results; shardInfoList = results.filter(Boolean) as ShardInfo[];
switch (interaction.options.getString('sort')) { switch (interaction.options.getString('sort')) {
case 'memory': case 'memory':
@@ -132,7 +145,7 @@ module.exports = {
const evenShardIndexes = currentPageShards.filter((shard, index) => index % 2 === 0); const evenShardIndexes = currentPageShards.filter((shard, index) => index % 2 === 0);
const oddShardIndexes = 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 = ''; let string = '';
string += `**Shard ${shard.shardId}** - Guilds: ${shard.guildCount.toLocaleString( string += `**Shard ${shard.shardId}** - Guilds: ${shard.guildCount.toLocaleString(
'en-US' 'en-US'
+16 -9
View File
@@ -1,7 +1,7 @@
import config from 'config'; import config from 'config';
import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; import { notValidGuildId } from '../../utils/validation/systemCommandValidator';
import { getUptimeFormatted } from '../../utils/system/getUptimeFormatted'; 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'; import osu from 'node-os-utils';
// @ts-ignore // @ts-ignore
import { version, dependencies } from '../../../package.json'; import { version, dependencies } from '../../../package.json';
@@ -26,8 +26,8 @@ module.exports = {
module: 'slashCommand', module: 'slashCommand',
name: '/systemstatus', name: '/systemstatus',
executionId: executionId, executionId: executionId,
shardId: interaction.guild.shardId, shardId: interaction.guild?.shardId,
guildId: interaction.guild.id guildId: interaction.guild?.id
}); });
if (await notValidGuildId({ interaction, executionId })) { if (await notValidGuildId({ interaction, executionId })) {
@@ -59,15 +59,21 @@ module.exports = {
const distubeYtdlVersion = dependencies['@distube/ytdl-core']; const distubeYtdlVersion = dependencies['@distube/ytdl-core'];
logger.debug('Fetching player statistics from all shards.'); 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 await client.shard
.broadcastEval(() => { .broadcastEval(() => {
/* eslint-disable no-undef */ /* eslint-disable no-undef */
return player.generateStatistics(); return player.generateStatistics();
}) })
.then((results) => { .then((results) => {
const queueCountList = []; const queueCountList: number[] = [];
const trackCountList = []; const trackCountList: number[] = [];
const listenerCountList = []; const listenerCountList: number[] = [];
results.map((result) => { results.map((result) => {
queueCountList.push(result.queues.length); queueCountList.push(result.queues.length);
if (result.queues.length > 0) { if (result.queues.length > 0) {
@@ -89,10 +95,11 @@ module.exports = {
await client.shard await client.shard
.fetchClientValues('guilds.cache') .fetchClientValues('guilds.cache')
.then((results) => { .then((results) => {
results.map((guildCache) => { const guildCaches = results as Collection<string, Guild>[];
guildCaches.map((guildCache) => {
if (guildCache) { if (guildCache) {
guildCount += guildCache.length; guildCount += guildCache.size;
memberCount += guildCache.reduce((acc, guildCache) => acc + guildCache.memberCount, 0); memberCount += guildCache.reduce((acc, guild) => acc + guild.memberCount, 0);
} }
}); });
logger.debug('Successfully fetched client values from shards.'); logger.debug('Successfully fetched client values from shards.');
+7 -3
View File
@@ -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; type RegisterClientCommandsFunction = (params: { client: Client; executionId: string }) => void;
export interface Command { export interface Command {
isNew: boolean; isNew: boolean;
isBeta: boolean; isBeta: boolean;
data: object; data: {
name: string;
description: string;
options: SharedNameAndDescription[]
};
execute: (params: { interaction: object; client: ExtendedClient | undefined; executionId: string }) => void; execute: (params: { interaction: object; client: ExtendedClient | undefined; executionId: string }) => void;
autocomplete?: (params: { interaction: object; executionId: string }) => void; autocomplete?: (params: { interaction: object; executionId: string }) => void;
} }
export interface ExtendedClient extends Client { export interface ExtendedClient extends Client {
registerClientCommands?: RegisterClientCommandsFunction; registerClientCommands?: RegisterClientCommandsFunction;
commands?: Collection<string, NodeModule | Command>; commands?: Collection<string, Command>;
} }
+12
View File
@@ -12,3 +12,15 @@ export interface CommandAutocompleteParams {
client: ExtendedClient | undefined; client: ExtendedClient | undefined;
executionId: string; executionId: string;
} }
export interface ShardInfo {
shardId: number;
memUsage: number;
guildCount: number;
guildMemberCount: number;
playerStatistics: {
activeVoiceConnections: number;
totalTracks: number;
totalListeners: number;
};
}
+5
View File
@@ -0,0 +1,5 @@
import { Player } from 'discord-player';
declare global {
const player: Player;
}