Skip to content

Commit

Permalink
Auto connect to Discord, if the script is launched BEFORE Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarLothbrok-Odin committed Jul 3, 2022
1 parent adfb15e commit 2cb193e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Trakt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ trakt.import_token(oAuth);

// Create the Discord RPC client
// Create client
const rpc = new DiscordRPC.Client({ transport: 'ipc' });

// Set rpc as null
let rpc;

const spawnRPC = async () => {
try {
// Attempt to spawn an RPC Client
rpc = new DiscordRPC.Client({ transport: 'ipc' });

// Log when error is thrown
rpc.on('error', (err) => {
console.log(err);
});
// Log when connected
await rpc.on('ready', () => {
console.log(chalk.green.bold('Successfully connected to Discord!'));
Expand All @@ -50,10 +59,9 @@ const spawnRPC = async () => {
updateStatus();
}, 15000);
} catch (err) {
rpc.clearActivity();
console.log(chalk.red.bold('Failed to connect to Discord. Retrying in 15 seconds.'));
// Retry every 15 seconds until successful.
setInterval(() => {
setTimeout(() => {
spawnRPC();
}, 15000);
return;
Expand All @@ -65,6 +73,7 @@ spawnRPC();

// Get Trakt user
async function updateStatus() {
// TODO Check if RPC is still connected
const user = await trakt.users.settings();
const watching = await trakt.users.watching({ username: user.user.username });

Expand Down

0 comments on commit 2cb193e

Please sign in to comment.