Skip to content

Commit

Permalink
Update discord plugin for new provider + wait for ready
Browse files Browse the repository at this point in the history
  • Loading branch information
th-ch committed Jan 13, 2021
1 parent eae95be commit aec542e
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions plugins/discord-rpc/back.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
const Discord = require('discord-rpc');
const Discord = require("discord-rpc");

const getSongInfo = require("../../providers/song-info");

const rpc = new Discord.Client({
transport: 'ipc'
transport: "ipc",
});

const clientId = '790655993809338398';
// Application ID registered by @semvis123
const clientId = "790655993809338398";

module.exports = (win) => {
const registerCallback = getSongInfo(win);

module.exports = win => {
// If the page is ready, register the callback
win.on('ready-to-show', () => {
// Startup the rpc client
rpc.login({
clientId
}).catch(console.error);

// Register the callback
global.songInfo.onNewData(songInfo => {
// Song information changed, so lets update the rich presence

const activityInfo = {
details: songInfo.title,
state: songInfo.artist,
largeImageKey: 'logo',
largeImageText: songInfo.views + ' - ' + songInfo.likes
};

if (songInfo.isPaused) {
// Add an idle icon to show that the song is paused
activityInfo.smallImageKey = 'idle';
activityInfo.smallImageText = 'idle/paused';
} else {
// Add the start and end time of the song
const songStartTime = Date.now() - (songInfo.elapsedSeconds * 1000);
activityInfo.startTimestamp = songStartTime;
activityInfo.endTimestamp = songStartTime + (songInfo.songDuration * 1000);
}

rpc.setActivity(activityInfo);
win.on("ready-to-show", () => {
rpc.on("ready", () => {
// Register the callback
registerCallback((songInfo) => {
// Song information changed, so lets update the rich presence
const activityInfo = {
details: songInfo.title,
state: songInfo.artist,
largeImageKey: "logo",
largeImageText: songInfo.views + " - " + songInfo.likes,
};

if (songInfo.isPaused) {
// Add an idle icon to show that the song is paused
activityInfo.smallImageKey = "idle";
activityInfo.smallImageText = "idle/paused";
} else {
// Add the start and end time of the song
const songStartTime = Date.now() - songInfo.elapsedSeconds * 1000;
activityInfo.startTimestamp = songStartTime;
activityInfo.endTimestamp =
songStartTime + songInfo.songDuration * 1000;
}

rpc.setActivity(activityInfo);
});
});

// Startup the rpc client
rpc
.login({
clientId,
})
.catch(console.error);
});
};

0 comments on commit aec542e

Please sign in to comment.