Skip to content

Commit

Permalink
✨ Improve DiscordRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed Aug 7, 2020
1 parent a4ed2ea commit b6b3e31
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ function createWindow() {
}

mainWindow.on('closed', function () {
view = null
mainWindow = null
})

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<body>
<div class="center-flex">
<div id="center-loading" class="preloader-wrapper big active">
<div class="spinner-layer spinner-blue-only">
<div class="spinner-layer spinner-red-only">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
Expand Down
32 changes: 22 additions & 10 deletions src/providers/discordRpcProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +23,6 @@ function start() {
client.login({ clientId }).catch(() => {
if (!isStarted()) {
setTimeout(function () {
// console.log('trying to connect')
start()
}, 10000)
}
Expand All @@ -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')
Expand All @@ -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) {
Expand All @@ -73,6 +82,9 @@ function setActivity(info) {
console.log(err)
})
}

_lastTrackId = info.track.id
_lastState = info.player.isPaused
}
}

Expand Down

0 comments on commit b6b3e31

Please sign in to comment.