From c9d5f230fdb0cb7b4e4fb496ab6e7c282c05834f Mon Sep 17 00:00:00 2001 From: nikosszzz <19nikospap@gmail.com> Date: Fri, 24 May 2024 15:15:04 +0300 Subject: [PATCH] bot: v3.3.2 * playlist: Removed unnecessary similar code for the playlist message * music(internals): Added one check in the inactivity leave * handlers(command): Fix crash when replying to a already replied and/or deferred interaction * lyrics: Fix possible crash --- package-lock.json | 2 +- package.json | 2 +- src/commands/info/changelogs.ts | 7 +++--- src/commands/music/lyrics.ts | 22 +++++++++--------- src/commands/music/nowplaying.ts | 6 ++--- src/commands/music/play.ts | 3 ++- src/commands/music/playlist.ts | 39 ++++++-------------------------- src/commands/music/stop.ts | 1 + src/components/Bot.ts | 2 +- src/components/MusicQueue.ts | 20 ++++++++-------- src/components/Playlist.ts | 2 +- src/manager/modules/commands.ts | 4 ++-- 12 files changed, 44 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7b817005..1fca3246 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "music-bot", - "version": "3.1.1", + "version": "3.3.2", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 4361bd3e..e99737f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "music-bot", - "version": "3.3.1", + "version": "3.3.2", "description": "A general music Discord bot.", "main": "./src/bot.ts", "type": "module", diff --git a/src/commands/info/changelogs.ts b/src/commands/info/changelogs.ts index 7f3a54fc..d599509f 100644 --- a/src/commands/info/changelogs.ts +++ b/src/commands/info/changelogs.ts @@ -7,11 +7,12 @@ export default { .setDescription("Displays information about the latest Music Bot update."), async execute(interaction) { - const bugfixes = `- None`; + const bugfixes = `- handlers(command): Fix crash when replying to a already replied and/or deferred interaction + - lyrics: Fix possible crash`; const whatsnew = `- bot: Updated and upgraded - - codebase: Update to ESM format - - lyrics: New package used for lyrics`; + - playlist: Removed unnecessary similar code for the playlist message + - music(internals): Added one check in the inactivity leave`; const UpdateEmbed = new EmbedBuilder() .setColor("NotQuiteBlack") diff --git a/src/commands/music/lyrics.ts b/src/commands/music/lyrics.ts index 8949c3c9..0e63d8a1 100644 --- a/src/commands/music/lyrics.ts +++ b/src/commands/music/lyrics.ts @@ -35,20 +35,20 @@ export default { await interaction.reply({ embeds: [lyricsFetch], ephemeral: true }); try { - lyricsSearch = await lyrics.default(queue.songs[0].title); - if (!lyricsSearch.lyrics) return interaction.editReply({ embeds: [lyricsNotFound] }); + lyricsSearch = await lyrics.default(title); + if (!lyricsSearch?.lyrics) return interaction.editReply({ embeds: [lyricsNotFound] }); + + const lyricsEmbed = new EmbedBuilder() + .setColor("NotQuiteBlack") + .setTitle(`${queue.songs[0].title} - Lyrics`) + .setDescription(lyricsSearch.lyrics); + + if ((lyricsEmbed.data.description?.length as number) >= 2048) + lyricsEmbed.data.description = lyricsEmbed.data.description?.substring(0, 2045) + "..."; + return interaction.editReply({ embeds: [lyricsEmbed] }); } catch (err: any) { Logger.error({ type: "MUSICCMDS", err: err }); return interaction.editReply({ embeds: [lyricsNotFound] }); } - - const lyricsEmbed = new EmbedBuilder() - .setColor("NotQuiteBlack") - .setTitle(`${queue.songs[0].title} - Lyrics`) - .setDescription(lyricsSearch.lyrics); - - if ((lyricsEmbed.data.description?.length as number) >= 2048) - lyricsEmbed.data.description = lyricsEmbed.data.description?.substr(0, 2045) + "..." as string; - return interaction.editReply({ embeds: [lyricsEmbed] }); }, } as Command; diff --git a/src/commands/music/nowplaying.ts b/src/commands/music/nowplaying.ts index 2f98ac81..5d83d19b 100644 --- a/src/commands/music/nowplaying.ts +++ b/src/commands/music/nowplaying.ts @@ -33,15 +33,15 @@ export default { nowPlaying.addFields( { name: "\u200b", - value: new Date(seek * 1000).toISOString().substr(11, 8) + + value: new Date(seek * 1000).toISOString().substring(11, 8) + "** **[" + splitBar(song.duration == 0 ? seek : song.duration, seek, 20)[0] + "]** **" + - (song.duration == 0 ? " ◉ LIVE" : new Date(song.duration * 1000).toISOString().substr(11, 8)), + (song.duration == 0 ? " ◉ LIVE" : new Date(song.duration * 1000).toISOString().substring(11, 8)), inline: false }); - nowPlaying.setFooter({ text: "Time Remaining: " + new Date(left * 1000).toISOString().substr(11, 8) }); + nowPlaying.setFooter({ text: "Time Remaining: " + new Date(left * 1000).toISOString().substring(11, 8) }); } return interaction.reply({ embeds: [nowPlaying] }); diff --git a/src/commands/music/play.ts b/src/commands/music/play.ts index a4dd4995..96c1d207 100644 --- a/src/commands/music/play.ts +++ b/src/commands/music/play.ts @@ -47,7 +47,7 @@ export default { } await interaction.reply({ content: "⏳ Loading..." }); - + let song: Song; try { song = await Song.from({ search: url, interaction }); @@ -83,6 +83,7 @@ export default { }); interaction.client.queues.set(interaction.guild?.id as string, newQueue); + await interaction.deleteReply(); return await newQueue.enqueue({ songs: [song] }); } diff --git a/src/commands/music/playlist.ts b/src/commands/music/playlist.ts index 45bb52ff..f2a058e5 100644 --- a/src/commands/music/playlist.ts +++ b/src/commands/music/playlist.ts @@ -17,7 +17,7 @@ export default { ), async execute(interaction) { const { channel } = (interaction.member as GuildMember).voice; - const queue = interaction.client.queues.get(interaction.guild?.id as string); + let queue = interaction.client.queues.get(interaction.guild?.id as string); const url = interaction.options.getString("query") as string; /* Embeds for music */ @@ -54,35 +54,12 @@ export default { if (queue) { if (!queue.songs.length) { - const playlistEmbed = new EmbedBuilder() - .setColor("NotQuiteBlack") - .setTitle("Track Player") - .setDescription("The following playlist is now playing:") - .addFields( - { - name: playlist.data instanceof SpotifyPlaylist || playlist.data instanceof SoundCloudPlaylist ? playlist.data.name : playlist.data.title!, value: "** **" - }) - .setURL(playlist.data.url as string); - - interaction.editReply({ embeds: [playlistEmbed] }).catch((err: Error) => Logger.error({ type: "MUSICCMDS", err })); - return queue.enqueue({ songs: playlist.videos }); - } else { + queue.enqueue({ songs: playlist.videos }); + } else { queue.songs.push(...playlist.videos); - - const playlistEmbed = new EmbedBuilder() - .setColor("NotQuiteBlack") - .setTitle("Track Player") - .setDescription("The following playlist has been added to the queue:") - .addFields( - { - name: playlist.data instanceof SpotifyPlaylist || playlist.data instanceof SoundCloudPlaylist ? playlist.data.name : playlist.data.title!, value: "** **" - }) - .setURL(playlist.data.url as string); - - return interaction.editReply({ embeds: [playlistEmbed] }).catch((err: Error) => Logger.error({ type: "MUSICCMDS", err })); } } else { - const newQueue = new MusicQueue({ + queue = new MusicQueue({ options: { interaction, connection: joinVoiceChannel({ @@ -92,16 +69,14 @@ export default { }) } }); + interaction.client.queues.set(interaction.guild?.id as string, queue); - interaction.client.queues.set(interaction.guild?.id as string, newQueue); - - await newQueue.enqueue({ songs: playlist.videos }); + await queue.enqueue({ songs: playlist.videos }); } - const playlistEmbed = new EmbedBuilder() .setColor("NotQuiteBlack") .setTitle("Track Player") - .setDescription("The following playlist is now playing:") + .setDescription("The following playlist has been queued") .addFields( { name: playlist.data instanceof SpotifyPlaylist || playlist.data instanceof SoundCloudPlaylist ? playlist.data.name : playlist.data.title!, value: "** **" diff --git a/src/commands/music/stop.ts b/src/commands/music/stop.ts index bff78a06..dc0ebfa8 100644 --- a/src/commands/music/stop.ts +++ b/src/commands/music/stop.ts @@ -22,6 +22,7 @@ export default { if (!canModifyQueue({ member: interaction.member as GuildMember })) return interaction.reply({ embeds: [notInBotChannel], ephemeral: true }); if (!queue) return interaction.reply({ embeds: [nothingPlaying], ephemeral: true }); + queue.songs = []; queue.player.stop(true); const stopEmbed = new EmbedBuilder() diff --git a/src/components/Bot.ts b/src/components/Bot.ts index ddbf044c..d9d04465 100644 --- a/src/components/Bot.ts +++ b/src/components/Bot.ts @@ -10,7 +10,7 @@ export class Bot extends Client { public commands = new Collection(); public queues = new Collection(); public readonly debug: boolean = false; - public readonly version: string = "3.3.1"; + public readonly version: string = "3.3.2"; public readonly branch: string; constructor(options: ClientOptions) { diff --git a/src/components/MusicQueue.ts b/src/components/MusicQueue.ts index ab1140cd..dc1611ee 100644 --- a/src/components/MusicQueue.ts +++ b/src/components/MusicQueue.ts @@ -12,7 +12,7 @@ import { VoiceConnectionStatus, type AudioPlayerPlayingState } from "@discordjs/voice"; -import type { CacheType, ChatInputCommandInteraction, Message, TextChannel } from "discord.js"; +import type { CacheType, ChatInputCommandInteraction, GuildTextBasedChannel } from "discord.js"; import { promisify } from "node:util"; import type { QueueOptions } from "@common"; import { config } from "@components/config"; @@ -31,7 +31,7 @@ export class MusicQueue { public readonly interaction!: ChatInputCommandInteraction; public readonly connection!: VoiceConnection; public readonly player: AudioPlayer; - public readonly textChannel: TextChannel; + public readonly textChannel: GuildTextBasedChannel; public readonly bot: Bot; public resource!: AudioResource; @@ -47,7 +47,7 @@ export class MusicQueue { Object.assign(this, options); this.bot = options.interaction.client; - this.textChannel = options.interaction.channel as TextChannel; + this.textChannel = options.interaction.channel as GuildTextBasedChannel; this.player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Play } }); options.connection.subscribe(this.player); @@ -139,6 +139,8 @@ export class MusicQueue { } catch { } } + if (this.player.state.status == AudioPlayerStatus.Playing || this.songs.length) return; + this.bot.queues.delete(this.interaction.guild?.id as string); this.textChannel.send("Left channel due to inactivity."); }, config.STAY_TIME * 1000); @@ -173,16 +175,14 @@ export class MusicQueue { } private async sendPlayingMessage({ newState }: { newState: AudioPlayerPlayingState; }): Promise { - let playingMessage: Message; - try { - playingMessage = this.interaction.replied || this.interaction.deferred ? await this.interaction.editReply((newState.resource as AudioResource).metadata.startMessage()) : await this.textChannel.send((newState.resource as AudioResource).metadata.startMessage()); + const playingMessage = await this.textChannel.send((newState.resource as AudioResource).metadata.startMessage()); + + if (config.PRUNING) setTimeout((): void => { + playingMessage.delete().catch(); + }, 3000); } catch (err: any | Error) { Logger.error({ type: "INTERNAL:MUSIC", err: err }); } - - if (config.PRUNING) setTimeout((): void => { - playingMessage.delete().catch(); - }, 3000); } } \ No newline at end of file diff --git a/src/components/Playlist.ts b/src/components/Playlist.ts index cdc24a72..6078ba7f 100644 --- a/src/components/Playlist.ts +++ b/src/components/Playlist.ts @@ -54,7 +54,7 @@ export class Playlist { pl = { data: playlist, tracks: await playlist.all_tracks() }; } else { - const playlist = await playlist_info(search as string, { incomplete: true }); + const playlist = await playlist_info((await playSearch(search, { source: { youtube: "playlist" } }))[0].url!, { incomplete: true }); pl = { data: playlist, tracks: await playlist.all_videos() }; } diff --git a/src/manager/modules/commands.ts b/src/manager/modules/commands.ts index 6a29b198..ea33a740 100644 --- a/src/manager/modules/commands.ts +++ b/src/manager/modules/commands.ts @@ -13,8 +13,8 @@ export async function commands(client: Bot): Promise { try { await command.execute(interaction); } catch (err: any) { - Logger.error({ type: "INTERNALS/CMDHANDLER", err: err.stack }); - await interaction.reply({ content: "An error occurred while executing this command.", ephemeral: true }); + Logger.error({ type: "INTERNALS/CMDHANDLER", err: err }); + interaction.replied || interaction.deferred ? await interaction.editReply({ content: "An error occurred while executing this command.", embeds: [] }) : await interaction.reply({ content: "An error occurred while executing this command.", embeds: [], ephemeral: true }); } }); }