2021-04-19 20:43:25 +02:00
|
|
|
const discordrpc = require("discord-rpc");
|
2021-04-19 23:08:19 +02:00
|
|
|
const { ipcMain } = require("electron");
|
|
|
|
const electron = require("electron");
|
|
|
|
const globalEvents = require("../constants/globalEvents");
|
|
|
|
const clientId = "833617820704440341";
|
2021-04-19 20:43:25 +02:00
|
|
|
const mediaInfoModule = require("./mediaInfo");
|
|
|
|
const discordModule = [];
|
|
|
|
|
|
|
|
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) {
|
|
|
|
rpc.setActivity({
|
|
|
|
...idleStatus,
|
|
|
|
...{
|
|
|
|
details: `Listening to ${mediaInfoModule.mediaInfo.title}`,
|
|
|
|
state: mediaInfoModule.mediaInfo.artist,
|
|
|
|
buttons: [{ label: "Play on Tidal", url: mediaInfoModule.mediaInfo.url }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
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",
|
|
|
|
largeImageText: `Tidal HiFi ${electron.app.getVersion()}`,
|
|
|
|
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;
|