From b6b3e31d0c40b9d55ddd2fa58452cf045aebda6e Mon Sep 17 00:00:00 2001 From: Adler Luiz Date: Fri, 7 Aug 2020 00:02:03 -0300 Subject: [PATCH] :sparkles: Improve DiscordRPC --- main.js | 1 + package.json | 10 ++++----- src/pages/home/home.html | 2 +- src/providers/discordRpcProvider.js | 32 ++++++++++++++++++++--------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/main.js b/main.js index f581f588f..a941dd54c 100644 --- a/main.js +++ b/main.js @@ -270,6 +270,7 @@ function createWindow() { } mainWindow.on('closed', function () { + view = null mainWindow = null }) diff --git a/package.json b/package.json index f3a042d34..ac8740a6e 100644 --- a/package.json +++ b/package.json @@ -4,15 +4,15 @@ "description": "YouTube Music Desktop App", "main": "main.js", "scripts": { - "start": "npx electron .", + "start": "electron .", "rebuild": "electron-rebuild -f", "postinstall": "npx electron-builder install-app-deps", + "deploy:win": "npx electron-builder --win -p always --config electron-builder-deploy64.yml", + "deploy:mac": "npx electron-builder --mac -p always --config electron-builder-deploy64.yml", + "deploy:lin": "npx electron-builder --linux -p always --config electron-builder-deploy64.yml", "publish:win": "npx electron-builder --win -p always --config electron-builder64.yml", "publish:mac": "npx electron-builder --mac -p always --config electron-builder64.yml", - "publish:lin": "npx electron-builder --linux -p always --config electron-builder64.yml", - "publish:win:deploy": "npx electron-builder --win -p always --config electron-builder-deploy64.yml", - "publish:mac:deploy": "npx electron-builder --mac -p always --config electron-builder-deploy64.yml", - "publish:lin:deploy": "npx electron-builder --linux -p always --config electron-builder-deploy64.yml" + "publish:lin": "npx electron-builder --linux -p always --config electron-builder64.yml" }, "repository": "https://github.com/ytmdesktop/ytmdesktop", "keywords": [ diff --git a/src/pages/home/home.html b/src/pages/home/home.html index fac532ce6..1233d809a 100644 --- a/src/pages/home/home.html +++ b/src/pages/home/home.html @@ -24,7 +24,7 @@
-
+
diff --git a/src/providers/discordRpcProvider.js b/src/providers/discordRpcProvider.js index 9f1468a1b..813eaa6c8 100644 --- a/src/providers/discordRpcProvider.js +++ b/src/providers/discordRpcProvider.js @@ -3,8 +3,7 @@ const RPC = require('discord-rpc') const settingsProvider = require('./settingsProvider') var client - -var _isStarted +var _isStarted, _lastTrackId, _lastState function isStarted() { return _isStarted @@ -24,7 +23,6 @@ function start() { client.login({ clientId }).catch(() => { if (!isStarted()) { setTimeout(function () { - // console.log('trying to connect') start() }, 10000) } @@ -42,7 +40,11 @@ function stop() { } function setActivity(info) { - if (isStarted() && info.track.title) { + if ( + isStarted() && + info.track.title && + (_lastTrackId != info.track.id || _lastState != info.player.isPaused) + ) { var now = Date.now() var activity = {} var discordSettings = settingsProvider.get('discord-presence-settings') @@ -52,18 +54,25 @@ function setActivity(info) { if (discordSettings.state) activity.state = info.track.author if (discordSettings.time) { - activity.startTimestamp = - now + info.player.seekbarCurrentPosition * 1000 - activity.endTimestamp = - now + - (info.track.duration - info.player.seekbarCurrentPosition) * - 1000 + if (info.player.isPaused) { + delete activity.startTimestamp + delete activity.endTimestamp + } else { + activity.startTimestamp = + now + info.player.seekbarCurrentPosition * 1000 + activity.endTimestamp = + now + + (info.track.duration - info.player.seekbarCurrentPosition) * + 1000 + } } activity.largeImageKey = 'ytm_logo_512' activity.smallImageKey = info.player.isPaused ? 'discordrpc-pause' : 'discordrpc-play' + activity.largeImageText = 'YouTube Music' + activity.smallImageText = info.player.isPaused ? 'Paused' : 'Playing' activity.instance = false if (!discordSettings.hideIdle && info.player.isPaused) { @@ -73,6 +82,9 @@ function setActivity(info) { console.log(err) }) } + + _lastTrackId = info.track.id + _lastState = info.player.isPaused } }