From 943ab1d153f5bb7af4da09a579173571c59be545 Mon Sep 17 00:00:00 2001 From: Loukious Date: Sun, 20 Aug 2023 17:26:38 +0100 Subject: [PATCH 1/2] Better YT icon + fixed inconsistent skip embed --- src/config.ts | 2 +- src/events/player/TrackEnd.ts | 4 ++-- src/events/player/TrackStart.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config.ts b/src/config.ts index 76f11c990..509bc01f9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -27,7 +27,7 @@ export default { img: process.env.IMG_LINK || 'https://i.imgur.com/ud3EWNh.jpg' }, icons: { - youtube: 'https://media.discordapp.net/attachments/963097935820750878/1054328059639111700/3670147.png', + youtube: 'https://cdn.discordapp.com/attachments/852316384289619968/1142853793822822551/3670147.png', spotify: 'https://media.discordapp.net/attachments/963097935820750878/1054333449252655104/spotify.png', soundcloud: 'https://media.discordapp.net/attachments/963097935820750878/1054333449638526986/145809.png', applemusic: 'https://media.discordapp.net/attachments/963097935820750878/1054333450368340018/apple-music-icon.png', diff --git a/src/events/player/TrackEnd.ts b/src/events/player/TrackEnd.ts index 5144e5c23..2b2997c41 100644 --- a/src/events/player/TrackEnd.ts +++ b/src/events/player/TrackEnd.ts @@ -11,14 +11,14 @@ export default class TrackEnd extends Event { public async run(player: Player, track: Song, dispatcher: Dispatcher): Promise { dispatcher.previous = dispatcher.current; dispatcher.current = null; + const m = await dispatcher.nowPlayingMessage?.fetch().catch(() => { }); if (dispatcher.loop === 'repeat') dispatcher.queue.unshift(track); if (dispatcher.loop === 'queue') dispatcher.queue.push(track); await dispatcher.play(); if (dispatcher.autoplay) { await dispatcher.Autoplay(track); } - const m = await dispatcher.nowPlayingMessage?.fetch().catch(() => {}); - if (m && m.deletable) m.delete().catch(() => {}); + if (m && m.deletable) await m.delete().catch(() => { }); } } diff --git a/src/events/player/TrackStart.ts b/src/events/player/TrackStart.ts index 0051b1dab..b10c9a98f 100644 --- a/src/events/player/TrackStart.ts +++ b/src/events/player/TrackStart.ts @@ -189,7 +189,7 @@ export default class TrackStart extends Event { iconURL: interaction.user.avatarURL({}), }), ], - components: [buttonBuilder()], + components: [], }); break; case 'loop': From 92868a1650256a124dbb09081a969cc4b1dfc3c9 Mon Sep 17 00:00:00 2001 From: Loukious Date: Sun, 20 Aug 2023 19:20:16 +0100 Subject: [PATCH 2/2] Rewrote Autoplay + added history + auto disconnect --- src/structures/Dispatcher.ts | 37 +++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/structures/Dispatcher.ts b/src/structures/Dispatcher.ts index 6fecaf8cc..5116eb5d5 100644 --- a/src/structures/Dispatcher.ts +++ b/src/structures/Dispatcher.ts @@ -46,6 +46,7 @@ export default class Dispatcher { public filters: Array; public autoplay: boolean; public nowPlayingMessage: Message | null; + public history: Song[] = []; constructor(options: DispatcherOptions) { this.client = options.client; @@ -95,6 +96,12 @@ export default class Dispatcher { )) as any; this.matchedTracks.push(...search.tracks); this.player.playTrack({ track: this.current?.track }); + if (this.current) { + this.history.push(this.current); + if (this.history.length > 100) { + this.history.shift(); + } + } } public pause() { if (!this.player) return; @@ -119,6 +126,7 @@ export default class Dispatcher { } public destroy() { this.queue.length = 0; + this.history = []; this.player.connection.disconnect(); this.client.queue.delete(this.guildId); if (this.stopped) return; @@ -153,10 +161,13 @@ export default class Dispatcher { public stop() { if (!this.player) return; this.queue.length = 0; + this.history = []; this.loop = 'off'; + this.autoplay = false; this.repeat = 0; this.stopped = true; this.player.stopTrack(); + this.player.connection.disconnect(); } public setLoop(loop: any) { this.loop = loop; @@ -173,12 +184,28 @@ export default class Dispatcher { public async Autoplay(song: Song) { const resolve = await this.node.rest.resolve(`${this.client.config.searchEngine}:${song.info.author}`); if (!resolve || !resolve.tracks.length) return this.destroy(); - let choosed = new Song(resolve.tracks[Math.floor(Math.random() * resolve.tracks.length)], this.client.user); - if (this.queue.some((s) => s.track === choosed.track)) { - return this.Autoplay(song); + + let choosed: Song | null = null; + const maxAttempts = 10; // Maximum number of attempts to find a unique song + let attempts = 0; + + while (attempts < maxAttempts) { + const potentialChoice = new Song(resolve.tracks[Math.floor(Math.random() * resolve.tracks.length)], this.client.user); + + // Check if the chosen song is not already in the queue or history + if (!this.queue.some((s) => s.track === potentialChoice.track) && !this.history.some((s) => s.track === potentialChoice.track)) { + choosed = potentialChoice; + break; + } + + attempts++; + } + + if (choosed) { + this.queue.push(choosed); + return this.isPlaying(); } - this.queue.push(choosed); - return this.isPlaying(); + return this.destroy(); } public async setAutoplay(autoplay: boolean) { this.autoplay = autoplay;