mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 18:41:47 +00:00
The callback sends multiple events, in particular two pause when going to the next song, so the timeout wasn't properly cleared. Add menu buttons for the two options
64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
const Discord = require("discord-rpc");
|
|
|
|
const registerCallback = require("../../providers/song-info");
|
|
|
|
const rpc = new Discord.Client({
|
|
transport: "ipc",
|
|
});
|
|
|
|
// Application ID registered by @semvis123
|
|
const clientId = "790655993809338398";
|
|
|
|
let clearActivity;
|
|
|
|
module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => {
|
|
// If the page is ready, register the callback
|
|
win.once("ready-to-show", () => {
|
|
rpc.once("ready", () => {
|
|
// Register the callback
|
|
//
|
|
// We get multiple events
|
|
// Next song: PAUSE(n), PAUSE(n+1), PLAY(n+1)
|
|
// Skip time: PAUSE(N), PLAY(N)
|
|
registerCallback((songInfo) => {
|
|
if (songInfo.title.length === 0 && songInfo.artist.length === 0) {
|
|
return;
|
|
}
|
|
// Song information changed, so lets update the rich presence
|
|
const activityInfo = {
|
|
details: songInfo.title,
|
|
state: songInfo.artist,
|
|
largeImageKey: "logo",
|
|
largeImageText: [
|
|
songInfo.uploadDate,
|
|
songInfo.views.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " views"
|
|
].join(' || '),
|
|
};
|
|
|
|
// stop the clear activity timout
|
|
clearTimeout(clearActivity);
|
|
|
|
if (songInfo.isPaused) {
|
|
// Add an idle icon to show that the song is paused
|
|
activityInfo.smallImageKey = "idle";
|
|
activityInfo.smallImageText = "idle/paused";
|
|
// Set start the timer so the activity gets cleared after a while if enabled
|
|
if (activityTimoutEnabled)
|
|
clearActivity = setTimeout(() => rpc.clearActivity().catch(console.error), activityTimoutTime || 10000);
|
|
} 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).catch(console.error);
|
|
});
|
|
});
|
|
|
|
// Startup the rpc client
|
|
rpc.login({ clientId }).catch(console.error);
|
|
});
|
|
};
|