Skip to content

Commit

Permalink
Detect if Discord is closed during operation
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarLothbrok-Odin committed Jul 4, 2022
1 parent 3555122 commit e877496
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions Trakt.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ trakt.import_token(oAuth);

// Set rpc as null
let rpc;
// We define the interval so we can clear it, incase connection is lost
let statusInt;

const spawnRPC = async () => {
try {
Expand All @@ -55,7 +57,7 @@ const spawnRPC = async () => {
updateStatus();

// Update status every 15 seconds
setInterval(() => {
statusInt = setInterval(() => {
updateStatus();
}, 15000);
} catch (err) {
Expand All @@ -73,33 +75,48 @@ 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 });

if (watching) {
const type = {};

type.smallImageKey = 'play';
type.largeImageKey = 'trakt';
type.startTimestamp = new Date(watching.started_at);

// Set the activity
if (watching.type === 'movie') {
const { movie } = watching;
type.details = `${movie.title} (${movie.year})`;
} else if (watching.type === 'episode') {
const { show, episode } = watching;
type.details = `${show.title}`;
type.state = `S${episode.season}E${episode.number} (${episode.title})`;
// If the RPC connection is valid
if (rpc) {
// Check if RPC is still connected
if (rpc.transport.socket.readyState !== 'open') {
// Reset the updateStatus() interval due to connection loss
clearInterval(statusInt);
// Destroy the existing client
rpc.destroy();
// Set the RPC as null
rpc = null;
// Attempt to reconnect
spawnRPC();
return;
}
rpc.setActivity({ ...type });

console.log(`${formatDate()} | ${chalk.red.bold.underline('Trakt Playing:')} ${type.details}${type.state ? ` - ${type.state}` : ''}`);
} else {
// Check if the user is currently watching something and if not, run on a timeout.
console.log(`${formatDate()} | ${chalk.red.bold.underline('Trakt:')} Not Playing.`);
rpc.clearActivity();
const user = await trakt.users.settings();
const watching = await trakt.users.watching({ username: user.user.username });

if (watching) {
const type = {};

type.smallImageKey = 'play';
type.largeImageKey = 'trakt';
type.startTimestamp = new Date(watching.started_at);

// Set the activity
if (watching.type === 'movie') {
const { movie } = watching;
type.details = `${movie.title} (${movie.year})`;
} else if (watching.type === 'episode') {
const { show, episode } = watching;
type.details = `${show.title}`;
type.state = `S${episode.season}E${episode.number} (${episode.title})`;
}
rpc.setActivity({ ...type });

console.log(`${formatDate()} | ${chalk.red.bold.underline('Trakt Playing:')} ${type.details}${type.state ? ` - ${type.state}` : ''}`);
} else {
// Check if the user is currently watching something and if not, run on a timeout.
console.log(`${formatDate()} | ${chalk.red.bold.underline('Trakt:')} Not Playing.`);
rpc.clearActivity();
}
}
}

Expand Down

0 comments on commit e877496

Please sign in to comment.