Skip to content

Commit 34f9639

Browse files
committed
Refactor video playback logic to improve controller handling and remove redundant playing messages
1 parent d965bd1 commit 34f9639

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

src/index.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ streamer.client.on('messageCreate', async (message) => {
178178
// Log playing video
179179
logger.info(`Playing local video: ${video.path}`);
180180

181-
// Send playing message
182-
sendPlaying(message, videoname || "Local Video");
183-
184181
// Play video
185182
playVideo(message, video.path, videoname);
186183
}
@@ -222,14 +219,12 @@ streamer.client.on('messageCreate', async (message) => {
222219
const twitchId = link.split('/').pop() as string;
223220
const twitchUrl = await getTwitchStreamUrl(link);
224221
if (twitchUrl) {
225-
sendPlaying(message, `${twitchId}'s Twitch Stream`);
226222
playVideo(message, twitchUrl, `twitch.tv/${twitchId}`);
227223
}
228224
}
229225
break;
230226
default:
231227
{
232-
sendPlaying(message, "URL");
233228
playVideo(message, link, "URL");
234229
}
235230
}
@@ -502,13 +497,16 @@ async function playVideo(message: Message, videoSource: string, title?: string)
502497
controller?.abort();
503498
controller = new AbortController();
504499

500+
if (!controller) {
501+
throw new Error('Controller is not initialized');
502+
}
505503
const { command, output: ffmpegOutput } = prepareStream(inputForFfmpeg, streamOpts, controller.signal);
506504

507505
command.on("error", (err, stdout, stderr) => {
508506
logger.error("An error happened with ffmpeg:", err.message);
509507
if (stdout) logger.error("ffmpeg stdout:", stdout);
510508
if (stderr) logger.error("ffmpeg stderr:", stderr);
511-
if (!controller.signal.aborted) controller.abort();
509+
if (controller && !controller.signal.aborted) controller.abort();
512510
});
513511

514512
command.on("end", (stdout, stderr) => {
@@ -517,21 +515,21 @@ async function playVideo(message: Message, videoSource: string, title?: string)
517515

518516
await playStream(ffmpegOutput, streamer, undefined, controller.signal)
519517
.catch((err) => {
520-
if (!controller.signal.aborted) {
518+
if (controller && !controller.signal.aborted) {
521519
logger.error('playStream error:', err);
522520
}
523-
if (!controller.signal.aborted) controller.abort();
521+
if (controller && !controller.signal.aborted) controller.abort();
524522
});
525523

526-
if (!controller.signal.aborted) {
524+
if (controller && !controller.signal.aborted) {
527525
logger.info(`Finished playing: ${title || videoSource}`);
528526
}
529527

530528
} catch (error) {
531529
logger.error(`Error in playVideo for ${title || videoSource}:`, error);
532-
if (!controller.signal.aborted) controller?.abort();
530+
if (controller && !controller.signal.aborted) controller.abort();
533531
} finally {
534-
if (!streamStatus.manualStop && !controller.signal.aborted) {
532+
if (!streamStatus.manualStop && controller && !controller.signal.aborted) {
535533
await sendFinishMessage();
536534
}
537535

0 commit comments

Comments
 (0)