Skip to content

Commit aec542e

Browse files
committed
Update discord plugin for new provider + wait for ready
1 parent eae95be commit aec542e

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

plugins/discord-rpc/back.js

+43-33
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
1-
const Discord = require('discord-rpc');
1+
const Discord = require("discord-rpc");
2+
3+
const getSongInfo = require("../../providers/song-info");
4+
25
const rpc = new Discord.Client({
3-
transport: 'ipc'
6+
transport: "ipc",
47
});
58

6-
const clientId = '790655993809338398';
9+
// Application ID registered by @semvis123
10+
const clientId = "790655993809338398";
11+
12+
module.exports = (win) => {
13+
const registerCallback = getSongInfo(win);
714

8-
module.exports = win => {
915
// If the page is ready, register the callback
10-
win.on('ready-to-show', () => {
11-
// Startup the rpc client
12-
rpc.login({
13-
clientId
14-
}).catch(console.error);
15-
16-
// Register the callback
17-
global.songInfo.onNewData(songInfo => {
18-
// Song information changed, so lets update the rich presence
19-
20-
const activityInfo = {
21-
details: songInfo.title,
22-
state: songInfo.artist,
23-
largeImageKey: 'logo',
24-
largeImageText: songInfo.views + ' - ' + songInfo.likes
25-
};
26-
27-
if (songInfo.isPaused) {
28-
// Add an idle icon to show that the song is paused
29-
activityInfo.smallImageKey = 'idle';
30-
activityInfo.smallImageText = 'idle/paused';
31-
} else {
32-
// Add the start and end time of the song
33-
const songStartTime = Date.now() - (songInfo.elapsedSeconds * 1000);
34-
activityInfo.startTimestamp = songStartTime;
35-
activityInfo.endTimestamp = songStartTime + (songInfo.songDuration * 1000);
36-
}
37-
38-
rpc.setActivity(activityInfo);
16+
win.on("ready-to-show", () => {
17+
rpc.on("ready", () => {
18+
// Register the callback
19+
registerCallback((songInfo) => {
20+
// Song information changed, so lets update the rich presence
21+
const activityInfo = {
22+
details: songInfo.title,
23+
state: songInfo.artist,
24+
largeImageKey: "logo",
25+
largeImageText: songInfo.views + " - " + songInfo.likes,
26+
};
27+
28+
if (songInfo.isPaused) {
29+
// Add an idle icon to show that the song is paused
30+
activityInfo.smallImageKey = "idle";
31+
activityInfo.smallImageText = "idle/paused";
32+
} else {
33+
// Add the start and end time of the song
34+
const songStartTime = Date.now() - songInfo.elapsedSeconds * 1000;
35+
activityInfo.startTimestamp = songStartTime;
36+
activityInfo.endTimestamp =
37+
songStartTime + songInfo.songDuration * 1000;
38+
}
39+
40+
rpc.setActivity(activityInfo);
41+
});
3942
});
43+
44+
// Startup the rpc client
45+
rpc
46+
.login({
47+
clientId,
48+
})
49+
.catch(console.error);
4050
});
4151
};

0 commit comments

Comments
 (0)