Skip to content

Commit

Permalink
feat: use timeout to track song end
Browse files Browse the repository at this point in the history
  • Loading branch information
miksuh-dev committed Nov 15, 2022
1 parent d2bb036 commit 1e089a7
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions server/common/playlist/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const youtubeDlWrap = new YoutubeDlWrap("/usr/bin/youtube-dl");

let playing: PlayingSong | undefined;

let stream: Readable | undefined;
let endTimeout: NodeJS.Timeout | undefined;

export const onSongEnd = async () => {
if (!playing) return;

Expand All @@ -27,17 +30,18 @@ export const onSongEnd = async () => {
},
});

await stopCurrentSong();
sendMessage(`Kappale ${playing.title} päättyi.`);

ee.emit(`onUpdate`, {
song: { setPlaying: undefined },
});
stopCurrentSong();

client.voiceConnection.stopStream();

const nextSong = await getNextSong();
if (nextSong) {
playSong(nextSong);
} else {
ee.emit(`onUpdate`, {
song: { setPlaying: undefined },
});
}
};

Expand All @@ -46,7 +50,7 @@ const createStream = (song: Song) => {
const readStream = youtubeDlWrap.execStream([
`https://www.youtube.com/watch?v=${song.videoId}`,
"-f",
"best",
"m4a",
]) as Readable;

return readStream;
Expand All @@ -61,14 +65,17 @@ const onPlayError = (song: Song, error: string) => {
);

setTimeout(() => {
onSongEnd().catch((e) => console.log("e", e));
if (playing?.id === song.id) {
onSongEnd().catch((e) => console.log("e", e));
}
}, 5000);
};

export const playSong = (song: Song) => {
console.log("play");
const currentSong = setCurrentSong({
...song,
startedAt: DateTime.now().plus({ seconds: 5 }),
startedAt: DateTime.now(),
});

ee.emit(`onUpdate`, {
Expand All @@ -79,36 +86,41 @@ export const playSong = (song: Song) => {
song: { remove: currentSong.id },
});

const stream = createStream(song);
sendMessage(`Soitetaan kappale ${currentSong.title}.`);

if (stream) {
stream.destroy();
}

if (endTimeout) {
clearTimeout(endTimeout);
}

const secondsLeft = currentSong.startedAt
.plus({ seconds: currentSong.duration })
.diffNow("seconds").seconds;

console.log("secondsLeft", secondsLeft);

endTimeout = setTimeout(() => {
onSongEnd().catch((e) => console.log("e", e));
}, secondsLeft * 1000);

stream = createStream(song);
if (!stream) {
sendErrorMessage("Failed to create stream");
sendErrorMessage("Virhe luodessa streamia");
return;
}

stream
.on("close", () => {
void (async () => {
sendMessage(`Kappale ${currentSong.title} päättyi.`);
await onSongEnd().catch((e) => {
if (e instanceof Error) {
onPlayError(currentSong, e.message);
}
});
})();

stream.destroy();
})
.on("error", (e) => {
if (e instanceof Error) {
onPlayError(currentSong, e.message);
}
});
stream.on("error", (e) => {
if (e instanceof Error) {
onPlayError(currentSong, e.message);
}
});

client.voiceConnection
.playStream(stream, "asd")
.playStream(stream, "0")
.on("start", async () => {
sendMessage(`Soitetaan kappale ${currentSong.title}.`);

await prisma.song.update({
where: {
id: currentSong.id,
Expand All @@ -131,19 +143,11 @@ export const setCurrentSong = (song: PlayingSong) => {
return (playing = song);
};

export const stopCurrentSong = async () => {
export const stopCurrentSong = () => {
playing = undefined;

client.voiceConnection.stopStream();

const nextSong = await getNextSong();
if (nextSong) {
playSong(nextSong);
} else {
ee.emit(`onUpdate`, {
song: { setPlaying: undefined },
});
}
clearTimeout(endTimeout);
};

export const getNextSong = async (): Promise<Song | null> => {
Expand Down

0 comments on commit 1e089a7

Please sign in to comment.