From 183bad43f662ffc294be57548720eb09c9fa0cbb Mon Sep 17 00:00:00 2001 From: Constantin Piber Date: Sun, 15 Aug 2021 12:20:37 +0200 Subject: [PATCH] Fix discord clearActivity, menu The callback sends multiple events, in particular two pause when going to the next song, so the timeout wasn't properly cleared. Add menu buttons for the two options --- plugins/discord/back.js | 13 +++++++++---- plugins/discord/menu.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 plugins/discord/menu.js diff --git a/plugins/discord/back.js b/plugins/discord/back.js index c6faadd7..2f75c430 100644 --- a/plugins/discord/back.js +++ b/plugins/discord/back.js @@ -16,6 +16,10 @@ module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => { win.once("ready-to-show", () => { rpc.once("ready", () => { // Register the callback + // + // We get multiple events + // Next song: PAUSE(n), PAUSE(n+1), PLAY(n+1) + // Skip time: PAUSE(N), PLAY(N) registerCallback((songInfo) => { if (songInfo.title.length === 0 && songInfo.artist.length === 0) { return; @@ -31,16 +35,17 @@ module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => { ].join(' || '), }; + // stop the clear activity timout + clearTimeout(clearActivity); + if (songInfo.isPaused) { // Add an idle icon to show that the song is paused activityInfo.smallImageKey = "idle"; activityInfo.smallImageText = "idle/paused"; // Set start the timer so the activity gets cleared after a while if enabled if (activityTimoutEnabled) - clearActivity = setTimeout(()=>rpc.clearActivity(), activityTimoutTime||10000); + clearActivity = setTimeout(() => rpc.clearActivity().catch(console.error), activityTimoutTime || 10000); } else { - // stop the clear activity timout - clearTimeout(clearActivity); // Add the start and end time of the song const songStartTime = Date.now() - songInfo.elapsedSeconds * 1000; activityInfo.startTimestamp = songStartTime; @@ -48,7 +53,7 @@ module.exports = (win, {activityTimoutEnabled, activityTimoutTime}) => { songStartTime + songInfo.songDuration * 1000; } - rpc.setActivity(activityInfo); + rpc.setActivity(activityInfo).catch(console.error); }); }); diff --git a/plugins/discord/menu.js b/plugins/discord/menu.js new file mode 100644 index 00000000..d0af48e8 --- /dev/null +++ b/plugins/discord/menu.js @@ -0,0 +1,21 @@ +const { setOptions } = require("../../config/plugins"); +const { edit } = require("../../config"); + +module.exports = (win, options) => [ + { + label: "Clear activity after timeout", + type: "checkbox", + checked: options.activityTimoutEnabled, + click: (item) => { + options.activityTimoutEnabled = item.checked; + setOptions('discord', options); + }, + }, + { + label: "Set timeout time in config", + click: () => { + // open config.json + edit(); + }, + }, +];