From 14cb6b4936f5e005081d713104398e187e3fccd8 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Mon, 28 Aug 2023 15:31:54 +0200 Subject: [PATCH] fix: Ensure promise is returned where applicable --- src/interactions/commands/info/help.ts | 7 +---- src/interactions/commands/info/status.ts | 11 ++------ src/interactions/commands/player/filters.ts | 8 +++--- src/interactions/commands/player/leave.ts | 4 +-- src/interactions/commands/player/loop.ts | 6 ++-- src/interactions/commands/player/lyrics.ts | 10 +++---- .../commands/player/nowplaying.ts | 8 +++--- src/interactions/commands/player/pause.ts | 8 +++--- src/interactions/commands/player/play.ts | 6 ++-- src/interactions/commands/player/queue.ts | 4 +-- src/interactions/commands/player/remove.ts | 6 ++-- src/interactions/commands/player/seek.ts | 8 +++--- src/interactions/commands/player/shuffle.ts | 8 +++--- src/interactions/commands/player/skip.ts | 8 +++--- src/interactions/commands/player/stop.ts | 4 +-- src/interactions/commands/player/volume.ts | 6 ++-- src/interactions/commands/system/guilds.ts | 9 ++---- src/interactions/commands/system/reload.ts | 19 ++++--------- src/interactions/commands/system/shards.ts | 19 ++++--------- .../commands/system/systemstatus.ts | 17 ++++------- .../components/filters-select-menu.ts | 28 +++++++++---------- src/utils/other/postBotStats.ts | 5 ++-- src/utils/other/startLoadTest.ts | 2 +- 23 files changed, 86 insertions(+), 125 deletions(-) diff --git a/src/interactions/commands/info/help.ts b/src/interactions/commands/info/help.ts index 5caedc7..d9f77fc 100644 --- a/src/interactions/commands/info/help.ts +++ b/src/interactions/commands/info/help.ts @@ -26,12 +26,7 @@ const command: CustomSlashCommandInteraction = { guildId: interaction.guild?.id }); - if (!client || !client.commands) { - logger.error('Client is undefined or does not have commands property.'); - return; - } - - const commandList = client.commands + const commandList = client!.commands! .filter((command) => { // don't include system commands if (command.isSystemCommand) { diff --git a/src/interactions/commands/info/status.ts b/src/interactions/commands/info/status.ts index 68022a7..e4422bd 100644 --- a/src/interactions/commands/info/status.ts +++ b/src/interactions/commands/info/status.ts @@ -39,12 +39,7 @@ const command: CustomSlashCommandInteraction = { let totalTracks: number = 0; let totalListeners: number = 0; - if (!client || !client.shard) { - logger.error('Client is undefined or does not have shard property.'); - return; - } - - await client.shard + await client!.shard! .broadcastEval(() => { /* eslint-disable no-undef */ return player.generateStatistics(); @@ -73,7 +68,7 @@ const command: CustomSlashCommandInteraction = { logger.error(error, 'Failed to fetch player statistics from shards.'); }); - await client.shard + await client!.shard! .fetchClientValues('guilds.cache') .then((results) => { const guildCaches = results as Guild[][]; @@ -103,7 +98,7 @@ const command: CustomSlashCommandInteraction = { const systemStatusString = `**${uptimeString}** Uptime\n` + `**${cpuUsage}%** CPU usage\n` + `**${usedMemoryInMB} MB** Memory usage`; - const discordStatusString = `**${client.ws.ping} ms** Discord API latency`; + const discordStatusString = `**${client!.ws.ping} ms** Discord API latency`; logger.debug('Transformed status into into embed description.'); diff --git a/src/interactions/commands/player/filters.ts b/src/interactions/commands/player/filters.ts index 9aabc74..a1dad46 100644 --- a/src/interactions/commands/player/filters.ts +++ b/src/interactions/commands/player/filters.ts @@ -40,21 +40,21 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueNoCurrentTrack({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const filterOptions: StringSelectMenuOptionBuilder[] = []; diff --git a/src/interactions/commands/player/leave.ts b/src/interactions/commands/player/leave.ts index a4bab4e..071c18b 100644 --- a/src/interactions/commands/player/leave.ts +++ b/src/interactions/commands/player/leave.ts @@ -28,7 +28,7 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; @@ -49,7 +49,7 @@ const command: CustomSlashCommandInteraction = { } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (!queue.deleted) { diff --git a/src/interactions/commands/player/loop.ts b/src/interactions/commands/player/loop.ts index 46528b7..f2a16bd 100644 --- a/src/interactions/commands/player/loop.ts +++ b/src/interactions/commands/player/loop.ts @@ -42,17 +42,17 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const loopModesFormatted = new Map([ diff --git a/src/interactions/commands/player/lyrics.ts b/src/interactions/commands/player/lyrics.ts index f8b99f3..261288e 100644 --- a/src/interactions/commands/player/lyrics.ts +++ b/src/interactions/commands/player/lyrics.ts @@ -48,19 +48,19 @@ const command: CustomSlashCommandInteraction = { if (!query) { if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueNoCurrentTrack({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } geniusSearchQuery = queue.currentTrack!.title.slice(0, 50); @@ -188,7 +188,7 @@ const command: CustomSlashCommandInteraction = { } } - return; + return Promise.resolve(); } logger.debug('Responding with info embed.'); diff --git a/src/interactions/commands/player/nowplaying.ts b/src/interactions/commands/player/nowplaying.ts index e456db7..5e2efcc 100644 --- a/src/interactions/commands/player/nowplaying.ts +++ b/src/interactions/commands/player/nowplaying.ts @@ -38,21 +38,21 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueNoCurrentTrack({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const sourceStringsFormatted = new Map([ diff --git a/src/interactions/commands/player/pause.ts b/src/interactions/commands/player/pause.ts index 3c014cf..312d499 100644 --- a/src/interactions/commands/player/pause.ts +++ b/src/interactions/commands/player/pause.ts @@ -29,21 +29,21 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueNoCurrentTrack({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const currentTrack = queue.currentTrack!; diff --git a/src/interactions/commands/player/play.ts b/src/interactions/commands/player/play.ts index 5c4bb75..87a7458 100644 --- a/src/interactions/commands/player/play.ts +++ b/src/interactions/commands/player/play.ts @@ -44,16 +44,16 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } if (await cannotJoinVoiceOrTalk({ interaction, executionId })) { - return; + return Promise.resolve(); } let queue = useQueue(interaction.guild!.id); if (queue && (await notInSameVoiceChannel({ interaction, queue, executionId }))) { - return; + return Promise.resolve(); } const player = useMainPlayer()!; diff --git a/src/interactions/commands/player/queue.ts b/src/interactions/commands/player/queue.ts index 8b27b7d..ef4c26e 100644 --- a/src/interactions/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -30,13 +30,13 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const pageIndex = (interaction.options.getNumber('page') || 1) - 1; diff --git a/src/interactions/commands/player/remove.ts b/src/interactions/commands/player/remove.ts index 1e3275c..f5a5071 100644 --- a/src/interactions/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -36,17 +36,17 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const removeTrackNumber = interaction.options.getNumber('tracknumber')!; diff --git a/src/interactions/commands/player/seek.ts b/src/interactions/commands/player/seek.ts index cf3e6ec..4ff8589 100644 --- a/src/interactions/commands/player/seek.ts +++ b/src/interactions/commands/player/seek.ts @@ -35,21 +35,21 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueNoCurrentTrack({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const durationInput = interaction.options.getString('duration'); diff --git a/src/interactions/commands/player/shuffle.ts b/src/interactions/commands/player/shuffle.ts index 81e4027..a64c05f 100644 --- a/src/interactions/commands/player/shuffle.ts +++ b/src/interactions/commands/player/shuffle.ts @@ -29,21 +29,21 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueIsEmpty({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } queue.tracks.shuffle(); diff --git a/src/interactions/commands/player/skip.ts b/src/interactions/commands/player/skip.ts index ffd4664..aa98e20 100644 --- a/src/interactions/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -32,21 +32,21 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await queueNoCurrentTrack({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const skipToTrack = interaction.options.getNumber('tracknumber'); diff --git a/src/interactions/commands/player/stop.ts b/src/interactions/commands/player/stop.ts index dcc2cb8..3737f14 100644 --- a/src/interactions/commands/player/stop.ts +++ b/src/interactions/commands/player/stop.ts @@ -28,7 +28,7 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; @@ -49,7 +49,7 @@ const command: CustomSlashCommandInteraction = { } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (!queue.deleted) { diff --git a/src/interactions/commands/player/volume.ts b/src/interactions/commands/player/volume.ts index 88e2b98..8e97159 100644 --- a/src/interactions/commands/player/volume.ts +++ b/src/interactions/commands/player/volume.ts @@ -36,17 +36,17 @@ const command: CustomSlashCommandInteraction = { }); if (await notInVoiceChannel({ interaction, executionId })) { - return; + return Promise.resolve(); } const queue: NodeResolvable = useQueue(interaction.guild!.id)!; if (await queueDoesNotExist({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } if (await notInSameVoiceChannel({ interaction, queue, executionId })) { - return; + return Promise.resolve(); } const volume = interaction.options.getNumber('percentage'); diff --git a/src/interactions/commands/system/guilds.ts b/src/interactions/commands/system/guilds.ts index 1f5609f..696b89b 100644 --- a/src/interactions/commands/system/guilds.ts +++ b/src/interactions/commands/system/guilds.ts @@ -28,19 +28,14 @@ const command: CustomSlashCommandInteraction = { }); if (await notValidGuildId({ interaction, executionId })) { - return; + return Promise.resolve(); } 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 + await client!.shard! .broadcastEval((c) => { return c.guilds.cache.map((guild) => { return guild; diff --git a/src/interactions/commands/system/reload.ts b/src/interactions/commands/system/reload.ts index 551eb63..52b67d7 100644 --- a/src/interactions/commands/system/reload.ts +++ b/src/interactions/commands/system/reload.ts @@ -29,24 +29,15 @@ const command: CustomSlashCommandInteraction = { }); if (await notValidGuildId({ interaction, executionId })) { - return; - } - - if (!client || !client.shard) { - logger.error('Client is undefined or does not have shard property.'); - return; + return Promise.resolve(); } try { logger.debug('Reloading commands across all shards.'); - await client.shard - .broadcastEval( + await client! + .shard!.broadcastEval( async (shardClient: ExtendedClient, { executionId }) => { - if (!shardClient.registerClientCommands) { - return; - } - - shardClient.registerClientCommands({ client: shardClient, executionId }); + shardClient.registerClientCommands!({ client: shardClient, executionId }); }, { context: { executionId: executionId } } ) @@ -70,7 +61,7 @@ const command: CustomSlashCommandInteraction = { }); } - 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}`; }); diff --git a/src/interactions/commands/system/shards.ts b/src/interactions/commands/system/shards.ts index 56a894c..9985d04 100644 --- a/src/interactions/commands/system/shards.ts +++ b/src/interactions/commands/system/shards.ts @@ -46,24 +46,15 @@ const command: CustomSlashCommandInteraction = { }); if (await notValidGuildId({ interaction, executionId })) { - return; + return Promise.resolve(); } 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: ExtendedClient) => { - if (!shardClient.shard) { - return; - } - + await client! + .shard!.broadcastEval((shardClient: ExtendedClient) => { /* eslint-disable no-undef */ const playerStats = player.generateStatistics(); const nodeProcessMemUsageInMb = parseFloat( @@ -71,7 +62,7 @@ const command: CustomSlashCommandInteraction = { ); const shardInfo = { - shardId: shardClient.shard.ids[0], + shardId: shardClient.shard!.ids[0], memUsage: nodeProcessMemUsageInMb, guildCount: shardClient.guilds.cache.size, guildMemberCount: shardClient.guilds.cache.reduce((acc, guild) => acc + guild.memberCount, 0), @@ -194,7 +185,7 @@ const command: CustomSlashCommandInteraction = { .setDescription(`**${embedOptions.icons.server} Shard overview - ${shardCount} total shards**\n`) .addFields(...embedFields) .setColor(embedOptions.colors.info) - .setFooter({ text: `Shard id: ${client.shard.ids[0]}, page ${pageIndex + 1} of ${totalPages}` }) + .setFooter({ text: `Shard id: ${client!.shard!.ids[0]}, page ${pageIndex + 1} of ${totalPages}` }) ] }); } diff --git a/src/interactions/commands/system/systemstatus.ts b/src/interactions/commands/system/systemstatus.ts index fe32b4e..2996ab4 100644 --- a/src/interactions/commands/system/systemstatus.ts +++ b/src/interactions/commands/system/systemstatus.ts @@ -32,7 +32,7 @@ const command: CustomSlashCommandInteraction = { }); if (await notValidGuildId({ interaction, executionId })) { - return; + return Promise.resolve(); } // from normal /status command @@ -61,13 +61,8 @@ const command: CustomSlashCommandInteraction = { 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(() => { + await client! + .shard!.broadcastEval(() => { /* eslint-disable no-undef */ return player.generateStatistics(); }) @@ -93,8 +88,8 @@ const command: CustomSlashCommandInteraction = { }); logger.debug('Fetching client values from all shards.'); - await client.shard - .fetchClientValues('guilds.cache') + await client! + .shard!.fetchClientValues('guilds.cache') .then((results) => { const guildCaches = results as Guild[][]; guildCaches.map((guildCache: Guild[]) => { @@ -135,7 +130,7 @@ const command: CustomSlashCommandInteraction = { `**${mediaplexVersion}** mediaplex\n` + `**${distubeYtdlVersion}** @distube/ytdl-core`; - const discordStatusString = `**${client.ws.ping} ms** Discord API latency`; + const discordStatusString = `**${client!.ws.ping} ms** Discord API latency`; logger.debug('Transformed system status into embed description.'); diff --git a/src/interactions/components/filters-select-menu.ts b/src/interactions/components/filters-select-menu.ts index 600e10b..70a4c5a 100644 --- a/src/interactions/components/filters-select-menu.ts +++ b/src/interactions/components/filters-select-menu.ts @@ -21,7 +21,8 @@ const component: CustomComponentInteraction = { logger.info('Received select menu confirmation.'); - const queue = useQueue(interaction.guild!.id)!; + const selectMenuInteraction = interaction as StringSelectMenuInteraction; + const queue = useQueue(selectMenuInteraction.guild!.id)!; queue.filters.ffmpeg.setInputArgs([ '-threads', @@ -41,42 +42,39 @@ const component: CustomComponentInteraction = { logger.debug('Reset queue filters.'); } - if (!(interaction instanceof StringSelectMenuInteraction)) { - return; - } // if bassboost is enabled and not normalizer, also enable normalizer to avoid distrorion if ( - (interaction.values.includes('bassboost_low') || interaction.values.includes('bassboost')) && - !interaction.values.includes('normalizer') + (selectMenuInteraction.values.includes('bassboost_low') || selectMenuInteraction.values.includes('bassboost')) && + !selectMenuInteraction.values.includes('normalizer') ) { - interaction.values.push('normalizer'); + selectMenuInteraction.values.push('normalizer'); } // Enable provided filters - queue.filters.ffmpeg.toggle(interaction.values as (keyof QueueFilters)[]); - logger.debug(`Enabled filters ${interaction.values.join(', ')}.`); + queue.filters.ffmpeg.toggle(selectMenuInteraction.values as (keyof QueueFilters)[]); + logger.debug(`Enabled filters ${selectMenuInteraction.values.join(', ')}.`); let authorName: string; - if (interaction.member instanceof GuildMember) { - authorName = interaction.member.nickname || interaction.user.username; + if (selectMenuInteraction.member instanceof GuildMember) { + authorName = selectMenuInteraction.member.nickname || selectMenuInteraction.user.username; } else { - authorName = interaction.user.username; + authorName = selectMenuInteraction.user.username; } logger.debug('Responding with success embed.'); - return await interaction.editReply({ + return await selectMenuInteraction.editReply({ embeds: [ new EmbedBuilder() .setAuthor({ name: authorName, - iconURL: interaction.user.avatarURL() || '' + iconURL: selectMenuInteraction.user.avatarURL() || '' }) .setDescription( `**${ embedOptions.icons.success - } Filters toggled**\nNow using these filters:\n${interaction.values + } Filters toggled**\nNow using these filters:\n${selectMenuInteraction.values .map((enabledFilter: string) => { const filter = ffmpegFilterOptions.availableFilters.find( (filter) => enabledFilter == filter.value diff --git a/src/utils/other/postBotStats.ts b/src/utils/other/postBotStats.ts index e85aaa5..89e083e 100644 --- a/src/utils/other/postBotStats.ts +++ b/src/utils/other/postBotStats.ts @@ -15,7 +15,7 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams) try { if (client.shard?.ids[0] !== 0) { - return; + return Promise.resolve(); } let guildCount = 0; @@ -128,7 +128,8 @@ export const postBotStats = async ({ client, executionId }: PostBotStatsParams) sites.map((site) => { if (!site.enabled) { - return; + // return if site is disabled + return Promise.resolve(); } const options = { diff --git a/src/utils/other/startLoadTest.ts b/src/utils/other/startLoadTest.ts index a29ef40..91cdb65 100644 --- a/src/utils/other/startLoadTest.ts +++ b/src/utils/other/startLoadTest.ts @@ -18,7 +18,7 @@ export const startLoadTest = async ({ client, executionId }: StartLoadTestParams if (!loadTestOptions.enabled) { logger.debug('Load test is disabled in options, cancelling.'); - return; + return Promise.resolve(); } const channelIds = loadTestOptions.channelIdsToJoin;