Skip to content

Commit

Permalink
Refactor song selection logic in Dispatcher class (autoplay)
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Dec 31, 2023
1 parent 463c907 commit 575bf01
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/structures/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,21 @@ export default class Dispatcher {
);
if (!resolve || !resolve?.data || !Array.isArray(resolve.data)) return this.destroy();

const metadata = (resolve.data as Array<any>).shift();
const metadata = (resolve.data as Array<any>) as any;
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(
metadata.encoded[Math.floor(Math.random() * metadata.encoded.length)],
this.client.user
);

// Check if the chosen song is not already in the queue or history
const potentialChoice = this.buildTrack(metadata[Math.floor(Math.random() * metadata.length)], this.client.user);
if (
!this.queue.some(s => s.encoded === potentialChoice.encoded) &&
!this.history.some(s => s.encoded === potentialChoice.encoded)
) {
choosed = potentialChoice;
break;
}

attempts++;
}

if (choosed) {
this.queue.push(choosed);
return await this.isPlaying();
Expand Down

0 comments on commit 575bf01

Please sign in to comment.