Skip to content

Commit

Permalink
Merge pull request #473 from Loukious/main
Browse files Browse the repository at this point in the history
Fixing inconsistent skip embed and other minor changes
  • Loading branch information
appujet authored Aug 21, 2023
2 parents e6516e2 + 92868a1 commit b6a49f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/events/player/TrackEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export default class TrackEnd extends Event {
public async run(player: Player, track: Song, dispatcher: Dispatcher): Promise<void> {
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(() => { });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/events/player/TrackStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default class TrackStart extends Event {
iconURL: interaction.user.avatarURL({}),
}),
],
components: [buttonBuilder()],
components: [],
});
break;
case 'loop':
Expand Down
37 changes: 32 additions & 5 deletions src/structures/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default class Dispatcher {
public filters: Array<string>;
public autoplay: boolean;
public nowPlayingMessage: Message | null;
public history: Song[] = [];

constructor(options: DispatcherOptions) {
this.client = options.client;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit b6a49f1

Please sign in to comment.