Skip to content

Commit

Permalink
🔧 chore(package.json): switch from discord-rpc to @xhayper/discord-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarLothbrok-Odin committed Dec 23, 2023
1 parent 3849d50 commit de982fa
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 120 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"type": "module",
"dependencies": {
"@types/luxon": "^3.3.7",
"@xhayper/discord-rpc": "^1.1.2",
"cli-progress": "^3.12.0",
"colors": "^1.4.0",
"discord-rpc": "^4.0.1",
"enquirer": "^2.4.1",
"luxon": "^3.4.4",
"pretty-ms": "^8.0.0",
Expand All @@ -26,7 +26,6 @@
},
"devDependencies": {
"@types/cli-progress": "^3.11.5",
"@types/discord-rpc": "^4.0.8",
"@types/node": "^20.10.5",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
Expand Down
35 changes: 24 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'colors';
import Enquirer from 'enquirer';
// @ts-expect-error [currently, no types file exists for trakt.tv, so this will cause an error]
import Trakt from 'trakt.tv';
import { Client } from 'discord-rpc';
import { Client } from '@xhayper/discord-rpc';
import { DateTime } from 'luxon';
import {
GenericFormatter, Options, Params, SingleBar,
Expand Down Expand Up @@ -75,7 +75,8 @@ enum ConnectionState {
Playing,
NotPlaying,
Connected,
Disconnected
Disconnected,
Connecting
}

/**
Expand Down Expand Up @@ -113,18 +114,23 @@ class DiscordRPC {
const traktCredentials = await fetchTraktCredentials();

// Initialize the RPC client with IPC transport
rpc = new Client({ transport: 'ipc' });
rpc = new Client({
clientId: traktCredentials.discordClientId,
transport: {
type: 'ipc',
},
});

// Event handler for when the RPC client is ready
rpc.on('ready', async () => {
instanceState = ConnectionState.Connected;
if (progressBar)progressBar.stop();
if (progressBar) progressBar.stop();
progressBar = await generateProgressBar();
progressBar.start(0, 0);
});

// Login to Discord using Trakt's Discord client ID
await rpc.login({ clientId: traktCredentials.discordClientId });
// Login to Discord
await rpc.login();

// Clear the retryInterval if it exists
if (retryInterval) clearInterval(retryInterval);
Expand Down Expand Up @@ -198,9 +204,8 @@ class TraktInstance {
async updateStatus(discordStatusInterval: NodeJS.Timeout | null) {
if (!rpc) return;

// Check if the RPC transport socket is not open
// @ts-expect-error [currently, no types file exists for trakt.tv, so this will cause an error]
if (rpc.transport.socket.readyState !== 'open') {
// Check if the RPC transport socket is connected
if (!rpc.transport.isConnected) {
// Clear the Discord status interval
if (discordStatusInterval) clearInterval(discordStatusInterval);

Expand Down Expand Up @@ -238,7 +243,7 @@ class TraktInstance {
}

// Set Discord activity with Trakt content
await rpc.setActivity({ ...traktContent });
await rpc.user?.setActivity({ ...traktContent });
return;
}

Expand All @@ -254,7 +259,7 @@ class TraktInstance {
progressBar.start(0, 0);

// Clear Discord activity
await rpc.clearActivity();
await rpc.user?.clearActivity();
}

/**
Expand Down Expand Up @@ -399,6 +404,9 @@ async function generateProgressBar() {

case ConnectionState.Disconnected:
return `${'Failed to connect to Discord. Retrying in'.red} ${countdownTimer.toString().green} ${'seconds.'.red}`;

case ConnectionState.Connecting:
return `${'Connecting...'.green}`;
default:
return `${formatDate()} | ${'Trakt:'.red} Not Playing.`;
}
Expand Down Expand Up @@ -642,6 +650,11 @@ async function main(): Promise<void> {
}
}

// Create an initial progress bar indicating progress.
instanceState = ConnectionState.Connecting;
progressBar = await generateProgressBar();
progressBar.start(0, 0);

// Initialize Trakt and Discord RPC
await initializeTraktAndDiscordRPC();
} catch (error) {
Expand Down
Loading

0 comments on commit de982fa

Please sign in to comment.