From aec542e95e2837f54bf19de675f311444789ea4e Mon Sep 17 00:00:00 2001 From: TC Date: Wed, 13 Jan 2021 22:22:22 +0100 Subject: [PATCH] Update discord plugin for new provider + wait for ready --- plugins/discord-rpc/back.js | 72 +++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/plugins/discord-rpc/back.js b/plugins/discord-rpc/back.js index d984a300..3e4ab629 100644 --- a/plugins/discord-rpc/back.js +++ b/plugins/discord-rpc/back.js @@ -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); + 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, + }; - // Register the callback - global.songInfo.onNewData(songInfo => { - // Song information changed, so lets update the rich presence + 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; + } - 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); + rpc.setActivity(activityInfo); + }); }); + + // Startup the rpc client + rpc + .login({ + clientId, + }) + .catch(console.error); }); };