2021-04-19 20:43:25 +02:00
|
|
|
const discordrpc = require("discord-rpc");
|
2021-04-20 21:56:45 +02:00
|
|
|
const { app, ipcMain } = require("electron");
|
2021-04-19 23:08:19 +02:00
|
|
|
const globalEvents = require("../constants/globalEvents");
|
|
|
|
const clientId = "833617820704440341";
|
2021-04-19 20:43:25 +02:00
|
|
|
const mediaInfoModule = require("./mediaInfo");
|
|
|
|
const discordModule = [];
|
|
|
|
|
2021-04-20 21:56:45 +02:00
|
|
|
function timeToSeconds(timeArray) {
|
|
|
|
let minutes = (timeArray[0] * 1);
|
|
|
|
let seconds = (minutes * 60) + (timeArray[1] * 1);
|
|
|
|
return seconds;
|
|
|
|
}
|
|
|
|
|
2021-04-19 20:43:25 +02:00
|
|
|
let rpc;
|
2021-04-19 23:08:19 +02:00
|
|
|
const observer = (event, arg) => {
|
|
|
|
if (mediaInfoModule.mediaInfo.status == "paused" && rpc) {
|
|
|
|
rpc.setActivity(idleStatus);
|
|
|
|
} else if (rpc) {
|
2021-04-20 21:56:45 +02:00
|
|
|
let csec = timeToSeconds(mediaInfoModule.mediaInfo.current.split(":"));
|
|
|
|
let dsec = timeToSeconds(mediaInfoModule.mediaInfo.duration.split(":"));
|
|
|
|
const date = new Date();
|
|
|
|
let now = date.getTime() / 1000 | 0;
|
|
|
|
let remaining = date.setSeconds(date.getSeconds() + (dsec - csec));
|
2021-04-21 03:31:44 +02:00
|
|
|
if (mediaInfoModule.mediaInfo.url) {
|
|
|
|
rpc.setActivity({
|
|
|
|
...idleStatus,
|
|
|
|
...{
|
|
|
|
details: `Listening to ${mediaInfoModule.mediaInfo.title}`,
|
|
|
|
state: mediaInfoModule.mediaInfo.artist,
|
|
|
|
startTimestamp: parseInt(now),
|
|
|
|
endTimestamp: parseInt(remaining),
|
|
|
|
buttons: [{ label: "Play on Tidal", url: mediaInfoModule.mediaInfo.url }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
rpc.setActivity({
|
|
|
|
...idleStatus,
|
|
|
|
...{
|
2021-04-21 04:56:59 +02:00
|
|
|
details: `Watching ${mediaInfoModule.mediaInfo.title}`,
|
2021-04-21 03:31:44 +02:00
|
|
|
state: mediaInfoModule.mediaInfo.artist,
|
|
|
|
startTimestamp: parseInt(now),
|
|
|
|
endTimestamp: parseInt(remaining),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-04-19 23:08:19 +02:00
|
|
|
}
|
|
|
|
};
|
2021-04-19 20:43:25 +02:00
|
|
|
|
|
|
|
const idleStatus = {
|
2021-04-19 23:08:19 +02:00
|
|
|
details: `Browsing Tidal`,
|
|
|
|
largeImageKey: "tidal-hifi-icon",
|
2021-04-20 21:56:45 +02:00
|
|
|
largeImageText: `Tidal HiFi ${app.getVersion()}`,
|
2021-04-19 23:08:19 +02:00
|
|
|
instance: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the discord rpc and listen on globalEvents.updateInfo
|
|
|
|
*/
|
2021-04-19 20:43:25 +02:00
|
|
|
discordModule.initRPC = function () {
|
2021-04-19 23:08:19 +02:00
|
|
|
rpc = new discordrpc.Client({ transport: "ipc" });
|
|
|
|
rpc.login({ clientId }).catch(console.error);
|
|
|
|
discordModule.rpc = rpc;
|
|
|
|
|
|
|
|
rpc.on("ready", () => {
|
|
|
|
rpc.setActivity(idleStatus);
|
|
|
|
});
|
|
|
|
ipcMain.on(globalEvents.updateInfo, observer);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any RPC connection with discord and remove the event listener on globalEvents.updateInfo
|
|
|
|
*/
|
2021-04-19 20:43:25 +02:00
|
|
|
discordModule.unRPC = function () {
|
2021-04-19 23:08:19 +02:00
|
|
|
rpc.clearActivity();
|
|
|
|
rpc.destroy();
|
|
|
|
rpc = false;
|
|
|
|
discordModule.rpc = rpc;
|
|
|
|
ipcMain.removeListener(globalEvents.updateInfo, observer);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = discordModule;
|