From 9b02b21283e3b66a2aa0197b39b31a8a48ff4abf Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Sun, 27 Aug 2023 23:20:32 +0200 Subject: [PATCH] feat!: Change interaction structure and handling of component responses --- src/events/interactions/interactionCreate.ts | 16 +- src/{ => interactions}/commands/info/help.ts | 6 +- .../commands/info/status.ts | 10 +- .../commands/player/filters.ts | 10 +- .../commands/player/leave.ts | 8 +- .../commands/player/loop.ts | 10 +- .../commands/player/lyrics.ts | 10 +- .../commands/player/nowplaying.ts | 147 ++---------------- .../commands/player/pause.ts | 10 +- .../commands/player/play.ts | 12 +- .../commands/player/queue.ts | 8 +- .../commands/player/remove.ts | 10 +- .../commands/player/seek.ts | 10 +- .../commands/player/shuffle.ts | 10 +- .../commands/player/skip.ts | 10 +- .../commands/player/stop.ts | 8 +- .../commands/player/volume.ts | 10 +- .../commands/system/guilds.ts | 8 +- .../commands/system/reload.ts | 10 +- .../commands/system/shards.ts | 10 +- .../commands/system/systemstatus.ts | 12 +- .../component/nowplaying-skip-button.ts | 111 +++++++++++++ src/types/interactionTypes.ts | 7 + src/utils/registerClientCommands.ts | 8 +- 24 files changed, 232 insertions(+), 239 deletions(-) rename src/{ => interactions}/commands/info/help.ts (93%) rename src/{ => interactions}/commands/info/status.ts (94%) rename src/{ => interactions}/commands/player/filters.ts (96%) rename src/{ => interactions}/commands/player/leave.ts (92%) rename src/{ => interactions}/commands/player/loop.ts (95%) rename src/{ => interactions}/commands/player/lyrics.ts (97%) rename src/{ => interactions}/commands/player/nowplaying.ts (53%) rename src/{ => interactions}/commands/player/pause.ts (90%) rename src/{ => interactions}/commands/player/play.ts (98%) rename src/{ => interactions}/commands/player/queue.ts (97%) rename src/{ => interactions}/commands/player/remove.ts (92%) rename src/{ => interactions}/commands/player/seek.ts (96%) rename src/{ => interactions}/commands/player/shuffle.ts (88%) rename src/{ => interactions}/commands/player/skip.ts (95%) rename src/{ => interactions}/commands/player/stop.ts (92%) rename src/{ => interactions}/commands/player/volume.ts (94%) rename src/{ => interactions}/commands/system/guilds.ts (91%) rename src/{ => interactions}/commands/system/reload.ts (90%) rename src/{ => interactions}/commands/system/shards.ts (96%) rename src/{ => interactions}/commands/system/systemstatus.ts (94%) create mode 100644 src/interactions/component/nowplaying-skip-button.ts create mode 100644 src/types/interactionTypes.ts diff --git a/src/events/interactions/interactionCreate.ts b/src/events/interactions/interactionCreate.ts index d313058..a704a74 100644 --- a/src/events/interactions/interactionCreate.ts +++ b/src/events/interactions/interactionCreate.ts @@ -1,5 +1,5 @@ import config from 'config'; -import { EmbedBuilder, Events, Interaction, StringSelectMenuInteraction } from 'discord.js'; +import { EmbedBuilder, Events, Interaction } from 'discord.js'; import { v4 as uuidv4 } from 'uuid'; import loggerModule from '../../services/logger'; @@ -24,15 +24,11 @@ module.exports = { }); if (interaction.isMessageComponent()) { - if (interaction instanceof StringSelectMenuInteraction) { - logger.debug( - `Message component interaction created for id ${interaction.customId}${ - interaction.values ? ' and values ' + interaction.values : '' - }.` - ); - } - - return; + await interaction.deferReply(); + const componentId = interaction.customId.split('_')[0]; + const trackId = interaction.customId.split('_')[1]; + const componentModule = await import(`../../interactions/component/${componentId}.js`); + return componentModule.execute({ interaction, trackId, executionId }); } if (interaction.isChatInputCommand() || interaction.isAutocomplete()) { diff --git a/src/commands/info/help.ts b/src/interactions/commands/info/help.ts similarity index 93% rename from src/commands/info/help.ts rename to src/interactions/commands/info/help.ts index 7e98801..693c21b 100644 --- a/src/commands/info/help.ts +++ b/src/interactions/commands/info/help.ts @@ -1,9 +1,9 @@ import config from 'config'; import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { BotOptions, EmbedOptions } from '../../types/configTypes'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { BotOptions, EmbedOptions } from '../../../types/configTypes'; const embedOptions: EmbedOptions = config.get('embedOptions'); const botOptions: BotOptions = config.get('botOptions'); diff --git a/src/commands/info/status.ts b/src/interactions/commands/info/status.ts similarity index 94% rename from src/commands/info/status.ts rename to src/interactions/commands/info/status.ts index 7105116..619cff7 100644 --- a/src/commands/info/status.ts +++ b/src/interactions/commands/info/status.ts @@ -3,11 +3,11 @@ import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js'; import osu from 'node-os-utils'; // @ts-ignore -import { version } from '../../../package.json'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { getUptimeFormatted } from '../../utils/system/getUptimeFormatted'; +import { version } from '../../../../package.json'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/filters.ts b/src/interactions/commands/player/filters.ts similarity index 96% rename from src/commands/player/filters.ts rename to src/interactions/commands/player/filters.ts index fb25c8e..454dbb4 100644 --- a/src/commands/player/filters.ts +++ b/src/interactions/commands/player/filters.ts @@ -14,11 +14,11 @@ import { StringSelectMenuOptionBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams, CustomError } from '../../types/commandTypes'; -import { EmbedOptions, FFmpegFilterOption, FFmpegFilterOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams, CustomError } from '../../../types/commandTypes'; +import { EmbedOptions, FFmpegFilterOption, FFmpegFilterOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions'); diff --git a/src/commands/player/leave.ts b/src/interactions/commands/player/leave.ts similarity index 92% rename from src/commands/player/leave.ts rename to src/interactions/commands/player/leave.ts index 9b5308e..894a0df 100644 --- a/src/commands/player/leave.ts +++ b/src/interactions/commands/player/leave.ts @@ -2,10 +2,10 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/loop.ts b/src/interactions/commands/player/loop.ts similarity index 95% rename from src/commands/player/loop.ts rename to src/interactions/commands/player/loop.ts index 1424691..3f6a42d 100644 --- a/src/commands/player/loop.ts +++ b/src/interactions/commands/player/loop.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { BotOptions, EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { BotOptions, EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); const botOptions: BotOptions = config.get('botOptions'); diff --git a/src/commands/player/lyrics.ts b/src/interactions/commands/player/lyrics.ts similarity index 97% rename from src/commands/player/lyrics.ts rename to src/interactions/commands/player/lyrics.ts index 332fa83..4f90ec1 100644 --- a/src/commands/player/lyrics.ts +++ b/src/interactions/commands/player/lyrics.ts @@ -4,11 +4,11 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import { lyricsExtractor } from '@discord-player/extractor'; -import loggerModule from '../../services/logger'; -import { CommandAutocompleteParams, CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandAutocompleteParams, CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); const recentQueries = new Map(); diff --git a/src/commands/player/nowplaying.ts b/src/interactions/commands/player/nowplaying.ts similarity index 53% rename from src/commands/player/nowplaying.ts rename to src/interactions/commands/player/nowplaying.ts index f56a8d9..9154b1d 100644 --- a/src/commands/player/nowplaying.ts +++ b/src/interactions/commands/player/nowplaying.ts @@ -7,19 +7,18 @@ import { ButtonStyle, ComponentType, EmbedBuilder, - GuildMember, - Interaction, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams, TrackMetadata, CustomError } from '../../types/commandTypes'; -import { EmbedOptions, PlayerOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams, TrackMetadata } from '../../../types/commandTypes'; +import { EmbedOptions, PlayerOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); const playerOptions: PlayerOptions = config.get('playerOptions'); + module.exports = { isNew: false, isBeta: false, @@ -110,15 +109,18 @@ module.exports = { bar = `${embedOptions.icons.liveTrack} **\`LIVE\`** - Playing continuously from live source.`; } + const customId = `nowplaying-skip-button_${currentTrack.id}`; + logger.debug(`Generated custom id for skip button: ${customId}`); + const nowPlayingButton = new ButtonBuilder() - .setCustomId('nowplaying-skip') + .setCustomId(customId) .setLabel('Skip track') .setStyle(ButtonStyle.Secondary) .setEmoji(embedOptions.icons.nextTrack) - .toJSON(); // Convert the builder to a raw object + .toJSON(); const nowPlayingActionRow: APIActionRowComponent = { - type: ComponentType.ActionRow, // This might vary based on your discord.js version + type: ComponentType.ActionRow, components: [nowPlayingButton] }; @@ -134,7 +136,7 @@ module.exports = { logger.debug('Successfully retrieved information about the current track.'); logger.debug('Sending info embed with action row components.'); - const response = await interaction.editReply({ + await interaction.editReply({ embeds: [ new EmbedBuilder() .setAuthor({ @@ -185,128 +187,5 @@ module.exports = { }); logger.debug('Finished sending response.'); - - const collectorFilter = (i: Interaction) => i.user.id === interaction.user.id; - try { - const confirmation = await response.awaitMessageComponent({ - filter: collectorFilter, - time: 300_000 - }); - - confirmation.deferUpdate(); - - logger.debug('Received component interaction response.'); - - if (confirmation.customId === 'nowplaying-skip') { - logger.debug('Received skip confirmation.'); - if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) { - logger.debug('Tried skipping track but there was no queue.'); - - logger.debug('Responding with warning embed.'); - return await interaction.followUp({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!` - ) - .setColor(embedOptions.colors.warning) - ], - components: [] - }); - } - - if (queue.currentTrack !== currentTrack) { - logger.debug( - 'Tried skipping track but it is not the current track and therefore already skipped/removed.' - ); - - logger.debug('Responding with warning embed.'); - return await interaction.followUp({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**${embedOptions.icons.warning} Oops!**\nThis track has already been skipped or is no longer playing.` - ) - .setColor(embedOptions.colors.warning) - ], - components: [] - }); - } - - const skippedTrack = queue.currentTrack; - let durationFormat = - Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00' - ? '' - : `\`${skippedTrack.duration}\``; - - if (skippedTrack.raw.live) { - durationFormat = `${embedOptions.icons.liveTrack} \`LIVE\``; - } - queue.node.skip(); - logger.debug('Skipped the track.'); - - const repeatModeUserString = loopModesFormatted.get(queue.repeatMode); - - let authorName: string; - - if (interaction.member instanceof GuildMember) { - authorName = interaction.member.nickname || interaction.user.username; - } else { - authorName = interaction.user.username; - } - - logger.debug('Responding with success embed.'); - return await interaction.followUp({ - embeds: [ - new EmbedBuilder() - .setAuthor({ - name: authorName, - iconURL: interaction.user.avatarURL() || '' - }) - .setDescription( - `**${embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${ - skippedTrack.title - }](${skippedTrack.raw.url ?? skippedTrack.url})**` + - `${ - 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\`**.` - }` - ) - .setThumbnail(skippedTrack.thumbnail) - .setColor(embedOptions.colors.success) - ], - components: [] - }); - } - } catch (error) { - if (error instanceof CustomError) { - if (error.code === 'InteractionCollectorError') { - logger.debug('Interaction response timed out.'); - return; - } - - if (error.message === 'Collector received no interactions before ending with reason: time') { - logger.debug('Interaction response timed out.'); - return; - } - - logger.error(error, 'Unhandled error while awaiting or handling component interaction.'); - return; - } else { - if ( - error instanceof Error && - error.message === 'Collector received no interactions before ending with reason: time' - ) { - logger.debug('Interaction response timed out.'); - return; - } - throw error; - } - } } }; diff --git a/src/commands/player/pause.ts b/src/interactions/commands/player/pause.ts similarity index 90% rename from src/commands/player/pause.ts rename to src/interactions/commands/player/pause.ts index 029cf31..0b8c484 100644 --- a/src/commands/player/pause.ts +++ b/src/interactions/commands/player/pause.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/play.ts b/src/interactions/commands/player/play.ts similarity index 98% rename from src/commands/player/play.ts rename to src/interactions/commands/player/play.ts index 371689c..53ac4d5 100644 --- a/src/commands/player/play.ts +++ b/src/interactions/commands/player/play.ts @@ -2,12 +2,12 @@ import config from 'config'; import { useMainPlayer, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandAutocompleteParams, CommandParams } from '../../types/commandTypes'; -import { BotOptions, EmbedOptions, PlayerOptions } from '../../types/configTypes'; -import { cannotJoinVoiceOrTalk } from '../../utils/validation/permissionValidator'; -import { transformQuery } from '../../utils/validation/searchQueryValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandAutocompleteParams, CommandParams } from '../../../types/commandTypes'; +import { BotOptions, EmbedOptions, PlayerOptions } from '../../../types/configTypes'; +import { cannotJoinVoiceOrTalk } from '../../../utils/validation/permissionValidator'; +import { transformQuery } from '../../../utils/validation/searchQueryValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); const botOptions: BotOptions = config.get('botOptions'); diff --git a/src/commands/player/queue.ts b/src/interactions/commands/player/queue.ts similarity index 97% rename from src/commands/player/queue.ts rename to src/interactions/commands/player/queue.ts index 02208fd..62a3896 100644 --- a/src/commands/player/queue.ts +++ b/src/interactions/commands/player/queue.ts @@ -2,10 +2,10 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions, PlayerOptions } from '../../types/configTypes'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions, PlayerOptions } from '../../../types/configTypes'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); const playerOptions: PlayerOptions = config.get('playerOptions'); diff --git a/src/commands/player/remove.ts b/src/interactions/commands/player/remove.ts similarity index 92% rename from src/commands/player/remove.ts rename to src/interactions/commands/player/remove.ts index de23dc2..511e448 100644 --- a/src/commands/player/remove.ts +++ b/src/interactions/commands/player/remove.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/seek.ts b/src/interactions/commands/player/seek.ts similarity index 96% rename from src/commands/player/seek.ts rename to src/interactions/commands/player/seek.ts index 2d18851..b88198e 100644 --- a/src/commands/player/seek.ts +++ b/src/interactions/commands/player/seek.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/shuffle.ts b/src/interactions/commands/player/shuffle.ts similarity index 88% rename from src/commands/player/shuffle.ts rename to src/interactions/commands/player/shuffle.ts index e5692e8..82a9dd8 100644 --- a/src/commands/player/shuffle.ts +++ b/src/interactions/commands/player/shuffle.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueIsEmpty } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueIsEmpty } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/skip.ts b/src/interactions/commands/player/skip.ts similarity index 95% rename from src/commands/player/skip.ts rename to src/interactions/commands/player/skip.ts index 7724852..a284b26 100644 --- a/src/commands/player/skip.ts +++ b/src/interactions/commands/player/skip.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist, queueNoCurrentTrack } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist, queueNoCurrentTrack } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/stop.ts b/src/interactions/commands/player/stop.ts similarity index 92% rename from src/commands/player/stop.ts rename to src/interactions/commands/player/stop.ts index c3c15c7..f5e3564 100644 --- a/src/commands/player/stop.ts +++ b/src/interactions/commands/player/stop.ts @@ -2,10 +2,10 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/player/volume.ts b/src/interactions/commands/player/volume.ts similarity index 94% rename from src/commands/player/volume.ts rename to src/interactions/commands/player/volume.ts index 989e652..6cb9cc3 100644 --- a/src/commands/player/volume.ts +++ b/src/interactions/commands/player/volume.ts @@ -2,11 +2,11 @@ import config from 'config'; import { NodeResolvable, useQueue } from 'discord-player'; import { EmbedBuilder, GuildMember, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { queueDoesNotExist } from '../../utils/validation/queueValidator'; -import { notInSameVoiceChannel, notInVoiceChannel } from '../../utils/validation/voiceChannelValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { queueDoesNotExist } from '../../../utils/validation/queueValidator'; +import { notInSameVoiceChannel, notInVoiceChannel } from '../../../utils/validation/voiceChannelValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/system/guilds.ts b/src/interactions/commands/system/guilds.ts similarity index 91% rename from src/commands/system/guilds.ts rename to src/interactions/commands/system/guilds.ts index 5f45a35..292bfda 100644 --- a/src/commands/system/guilds.ts +++ b/src/interactions/commands/system/guilds.ts @@ -1,10 +1,10 @@ import config from 'config'; import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/system/reload.ts b/src/interactions/commands/system/reload.ts similarity index 90% rename from src/commands/system/reload.ts rename to src/interactions/commands/system/reload.ts index dc401ae..d3ac0c4 100644 --- a/src/commands/system/reload.ts +++ b/src/interactions/commands/system/reload.ts @@ -1,11 +1,11 @@ import config from 'config'; import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { ExtendedClient } from '../../types/clientTypes'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; +import loggerModule from '../../../services/logger'; +import { ExtendedClient } from '../../../types/clientTypes'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/system/shards.ts b/src/interactions/commands/system/shards.ts similarity index 96% rename from src/commands/system/shards.ts rename to src/interactions/commands/system/shards.ts index fa24251..efc9204 100644 --- a/src/commands/system/shards.ts +++ b/src/interactions/commands/system/shards.ts @@ -1,11 +1,11 @@ import config from 'config'; import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; -import loggerModule from '../../services/logger'; -import { ExtendedClient } from '../../types/clientTypes'; -import { CommandParams, ShardInfo } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; +import loggerModule from '../../../services/logger'; +import { ExtendedClient } from '../../../types/clientTypes'; +import { CommandParams, ShardInfo } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); module.exports = { diff --git a/src/commands/system/systemstatus.ts b/src/interactions/commands/system/systemstatus.ts similarity index 94% rename from src/commands/system/systemstatus.ts rename to src/interactions/commands/system/systemstatus.ts index 19c7a60..c1d957c 100644 --- a/src/commands/system/systemstatus.ts +++ b/src/interactions/commands/system/systemstatus.ts @@ -3,12 +3,12 @@ import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js'; import osu from 'node-os-utils'; // @ts-ignore -import { dependencies, version } from '../../../package.json'; -import loggerModule from '../../services/logger'; -import { CommandParams } from '../../types/commandTypes'; -import { EmbedOptions } from '../../types/configTypes'; -import { getUptimeFormatted } from '../../utils/system/getUptimeFormatted'; -import { notValidGuildId } from '../../utils/validation/systemCommandValidator'; +import { dependencies, version } from '../../../../package.json'; +import loggerModule from '../../../services/logger'; +import { CommandParams } from '../../../types/commandTypes'; +import { EmbedOptions } from '../../../types/configTypes'; +import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted'; +import { notValidGuildId } from '../../../utils/validation/systemCommandValidator'; const embedOptions: EmbedOptions = config.get('embedOptions'); diff --git a/src/interactions/component/nowplaying-skip-button.ts b/src/interactions/component/nowplaying-skip-button.ts new file mode 100644 index 0000000..7b2c489 --- /dev/null +++ b/src/interactions/component/nowplaying-skip-button.ts @@ -0,0 +1,111 @@ +import config from 'config'; +import { NodeResolvable, useQueue } from 'discord-player'; +import { EmbedBuilder, GuildMember } from 'discord.js'; + +import loggerModule from '../../services/logger'; +import { EmbedOptions } from '../../types/configTypes'; +import { ComponentInteractionParams } from '../../types/interactionTypes'; +const embedOptions: EmbedOptions = config.get('embedOptions'); + +export const execute = async ({ interaction, trackId, executionId }: ComponentInteractionParams) => { + const logger = loggerModule.child({ + source: 'nowplaying-skip.js', + module: 'componentInteraction', + name: 'nowplaying-skip', + executionId: executionId, + shardId: interaction.guild?.shardId, + guildId: interaction.guild?.id + }); + + const queue: NodeResolvable = useQueue(interaction.guild!.id)!; + + logger.debug(`Received skip confirmation for track id ${trackId}.`); + if (!queue || (queue.tracks.data.length === 0 && !queue.currentTrack)) { + logger.debug('Tried skipping track but there was no queue.'); + + logger.debug('Responding with warning embed.'); + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${embedOptions.icons.warning} Oops!**\nThere is nothing currently playing. First add some tracks with **\`/play\`**!` + ) + .setColor(embedOptions.colors.warning) + ], + components: [] + }); + } + + if (queue.currentTrack!.id !== trackId) { + logger.debug('Tried skipping track but it is not the current track and therefore already skipped/removed.'); + + logger.debug('Responding with warning embed.'); + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setDescription( + `**${embedOptions.icons.warning} Oops!**\nThis track has already been skipped or is no longer playing.` + ) + .setColor(embedOptions.colors.warning) + ], + components: [] + }); + } + + const skippedTrack = queue.currentTrack!; + let durationFormat = + Number(skippedTrack.raw.duration) === 0 || skippedTrack.duration === '0:00' + ? '' + : `\`${skippedTrack.duration}\``; + + if (skippedTrack.raw.live) { + durationFormat = `${embedOptions.icons.liveTrack} \`LIVE\``; + } + queue.node.skip(); + logger.debug('Skipped the track.'); + + const loopModesFormatted = new Map([ + [0, 'disabled'], + [1, 'track'], + [2, 'queue'], + [3, 'autoplay'] + ]); + + const repeatModeUserString = loopModesFormatted.get(queue.repeatMode); + + let authorName: string; + + if (interaction.member instanceof GuildMember) { + authorName = interaction.member.nickname || interaction.user.username; + } else { + authorName = interaction.user.username; + } + + logger.debug('Responding with success embed.'); + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: authorName, + iconURL: interaction.user.avatarURL() || '' + }) + .setDescription( + `**${embedOptions.icons.skipped} Skipped track**\n**${durationFormat} [${skippedTrack.title}](${ + skippedTrack.raw.url ?? skippedTrack.url + })**` + + `${ + 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\`**.` + }` + ) + .setThumbnail(skippedTrack.thumbnail) + .setColor(embedOptions.colors.success) + ], + components: [] + }); +}; diff --git a/src/types/interactionTypes.ts b/src/types/interactionTypes.ts new file mode 100644 index 0000000..7aa72a7 --- /dev/null +++ b/src/types/interactionTypes.ts @@ -0,0 +1,7 @@ +import { MessageComponentInteraction } from 'discord.js'; + +export interface ComponentInteractionParams { + interaction: MessageComponentInteraction; + trackId?: string; + executionId: string; +} diff --git a/src/utils/registerClientCommands.ts b/src/utils/registerClientCommands.ts index 6a4b664..ef16c69 100644 --- a/src/utils/registerClientCommands.ts +++ b/src/utils/registerClientCommands.ts @@ -17,21 +17,21 @@ export const registerClientCommands = ({ client, executionId }: RegisterClientCo logger.debug('Registering client commands...'); client.commands = new Collection(); - const commandFolders = fs.readdirSync(path.resolve('./dist/commands')); + const commandFolders = fs.readdirSync(path.resolve('./dist/interactions/commands')); for (const folder of commandFolders) { logger.trace(`Registering client commands for folder '${folder}'...`); const commandFiles = fs - .readdirSync(path.resolve(`./dist/commands/${folder}`)) + .readdirSync(path.resolve(`./dist/interactions/commands/${folder}`)) .filter((file) => file.endsWith('.js')); for (const file of commandFiles) { try { // delete command from require cache - delete require.cache[require.resolve(`../commands/${folder}/${file}`)]; + delete require.cache[require.resolve(`../interactions/commands/${folder}/${file}`)]; // register command /* eslint-disable @typescript-eslint/no-var-requires */ - const command = require(`../commands/${folder}/${file}`); + const command = require(`../interactions/commands/${folder}/${file}`); client.commands.delete(command.data.name); client.commands.set(command.data.name, command); } catch (error) {