From 4474a2fce5c665a8c03a98d070fa6c89110c7b41 Mon Sep 17 00:00:00 2001 From: Ragnar Lothbrok <30740511+RagnarLothbrok-Odin@users.noreply.github.com> Date: Mon, 18 Dec 2023 15:02:14 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(index.ts):=20add=20playing=20p?= =?UTF-8?q?roperty=20to=20TraktInstance=20class=20to=20keep=20track=20of?= =?UTF-8?q?=20whether=20the=20user=20is=20currently=20playing=20something?= =?UTF-8?q?=20or=20not?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This update also includes a neat feature: no longer will your console be spammed with 'not playing', but instead, we will generate a progress bar (but without the bar) with a dynamic clock, we will then stop/start the progress bar when you start/stop watching content --- src/index.ts | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index bf429ef..d1ab133 100644 --- a/src/index.ts +++ b/src/index.ts @@ -125,6 +125,8 @@ class DiscordRPC { class TraktInstance { trakt: Trakt; + playing: boolean = false; + private credentials: Configuration | null = null; /** @@ -185,6 +187,13 @@ class TraktInstance { const watching = await this.trakt.users.watching({ username: user.user.username }); if (watching) { + if (!this.playing && progressBar) { + progressBar.stop(); + progressBar = null; + } + + this.playing = true; + // Prepare Trakt content for Discord RPC let traktContent: TraktContent = { smallImageKey: 'play', @@ -204,11 +213,16 @@ class TraktInstance { return; } - // Stop the progress bar if not watching anything, and it exists - if (progressBar) progressBar.stop(); + this.playing = false; - // Log that Trakt is not playing anything - console.log(`${formatDate()} | ${'Trakt:'.red} Not Playing.`.bold); + if (progressBar) { + progressBar.stop(); + progressBar = null; + } + + // Initialize progress bar if it doesn't exist + progressBar = await generateProgressBar(); + progressBar.start(0, 0); // Clear Discord activity await rpc.clearActivity(); @@ -338,22 +352,26 @@ function generateBarProgress(options: Options, params: Params): string { async function generateProgressBar() { // Define a custom format function for the progress bar const formatFunction: GenericFormatter = (options, params, payload) => { - // Extract relevant information from the payload - const { startedAt, endsAt, content } = payload; + if (payload && Object.keys(payload).length > 0) { + // Extract relevant information from the payload + const { startedAt, endsAt, content } = payload; - // Format start and end dates in local time - const localStartDate = formatDateTime(startedAt); - const localEndDate = formatDateTime(endsAt); + // Format start and end dates in local time + const localStartDate = formatDateTime(startedAt); + const localEndDate = formatDateTime(endsAt); - // Calculate elapsed duration and format it in a human-readable format - const elapsedDuration = calculateElapsedDuration(startedAt); - const prettyDuration = prettyMilliseconds(elapsedDuration * 1000, { secondsDecimalDigits: 0 }); + // Calculate elapsed duration and format it in a human-readable format + const elapsedDuration = calculateElapsedDuration(startedAt); + const prettyDuration = prettyMilliseconds(elapsedDuration * 1000, { secondsDecimalDigits: 0 }); - // Generate progress bar - const barProgress = generateBarProgress(options, params); + // Generate progress bar + const barProgress = generateBarProgress(options, params); + + // Construct the progress bar line with formatted information + return `${content.cyan.padStart(3)} ${barProgress} Started at ${localStartDate.green.bold} | Ends at ${localEndDate.green.bold} | Time Elapsed: ${prettyDuration.green.bold}`; + } - // Construct the progress bar line with formatted information - return `${content.cyan.padStart(3)} ${barProgress} Started at ${localStartDate.green.bold} | Ends at ${localEndDate.green.bold} | Time Elapsed: ${prettyDuration.green.bold}`; + return `${formatDate()} | ${'Trakt:'.red} Not Playing.`; }; // Create a new SingleBar instance with specified options