Skip to content

Commit

Permalink
✨ feat(index.ts): add playing property to TraktInstance class to keep…
Browse files Browse the repository at this point in the history
… track of whether the user is currently playing something or not

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
  • Loading branch information
RagnarLothbrok-Odin committed Dec 18, 2023
1 parent c422d5c commit 4474a2f
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class DiscordRPC {
class TraktInstance {
trakt: Trakt;

playing: boolean = false;

private credentials: Configuration | null = null;

/**
Expand Down Expand Up @@ -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',
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4474a2f

Please sign in to comment.