From df887b8628d8c8f2b647a7bcdf27c300dff1fa6c Mon Sep 17 00:00:00 2001 From: Marie <8841466+Mar0xy@users.noreply.github.com> Date: Tue, 20 Apr 2021 21:56:45 +0200 Subject: [PATCH] add time left to discord rpc & duration, current to mediainfo --- src/scripts/discord.js | 18 +++++++++++++++--- src/scripts/mediaInfo.js | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/scripts/discord.js b/src/scripts/discord.js index 628d821..1753baa 100644 --- a/src/scripts/discord.js +++ b/src/scripts/discord.js @@ -1,21 +1,33 @@ const discordrpc = require("discord-rpc"); -const { ipcMain } = require("electron"); -const electron = require("electron"); +const { app, ipcMain } = require("electron"); const globalEvents = require("../constants/globalEvents"); const clientId = "833617820704440341"; const mediaInfoModule = require("./mediaInfo"); const discordModule = []; +function timeToSeconds(timeArray) { + let minutes = (timeArray[0] * 1); + let seconds = (minutes * 60) + (timeArray[1] * 1); + return seconds; +} + let rpc; const observer = (event, arg) => { if (mediaInfoModule.mediaInfo.status == "paused" && rpc) { rpc.setActivity(idleStatus); } else if (rpc) { + 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)); 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 }], }, }); @@ -25,7 +37,7 @@ const observer = (event, arg) => { const idleStatus = { details: `Browsing Tidal`, largeImageKey: "tidal-hifi-icon", - largeImageText: `Tidal HiFi ${electron.app.getVersion()}`, + largeImageText: `Tidal HiFi ${app.getVersion()}`, instance: false, }; diff --git a/src/scripts/mediaInfo.js b/src/scripts/mediaInfo.js index e7d7f62..0964aa5 100644 --- a/src/scripts/mediaInfo.js +++ b/src/scripts/mediaInfo.js @@ -6,6 +6,8 @@ const mediaInfo = { icon: "", status: statuses.paused, url: "", + current: "", + duration: "" }; const mediaInfoModule = { mediaInfo, @@ -20,6 +22,8 @@ mediaInfoModule.update = function (arg) { mediaInfo.icon = propOrDefault(arg.icon); mediaInfo.url = propOrDefault(arg.url); mediaInfo.status = propOrDefault(arg.status); + mediaInfo.current = propOrDefault(arg.current); + mediaInfo.duration = propOrDefault(arg.duration); }; /**