From 76a6b14565827ca6b541750f6ebfd6aacccfc55e Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Mon, 2 May 2022 12:40:54 +0200 Subject: [PATCH 1/8] add hide timestamp to rpc code --- src/main/plugins/discordrpc.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/plugins/discordrpc.ts b/src/main/plugins/discordrpc.ts index 92f613e3..460b57bc 100644 --- a/src/main/plugins/discordrpc.ts +++ b/src/main/plugins/discordrpc.ts @@ -223,10 +223,12 @@ export default class DiscordRPC { ] //To change attributes.url => preload/cider-preload.js } - // Add the timestamp if its playing - if (attributes.status) { - this._activity.startTimestamp = Date.now() - (attributes?.durationInMillis - attributes?.remainingTime) - this._activity.endTimestamp = attributes.endTime + // Add the timestamp if its playing and people want them + if (!this._utils.getStoreValue("general.discordrpc.hide_timestamp")) { + if (attributes.status) { + this._activity.startTimestamp = Date.now() - (attributes?.durationInMillis - attributes?.remainingTime) + this._activity.endTimestamp = attributes.endTime + } } // If the user wants to keep the activity when paused From b021f8d43a4c16e66c5849a591895cd01ccf780a Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Mon, 2 May 2022 12:42:16 +0200 Subject: [PATCH 2/8] add to store --- src/main/base/store.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/base/store.ts b/src/main/base/store.ts index 6741395c..5de4dc33 100644 --- a/src/main/base/store.ts +++ b/src/main/base/store.ts @@ -17,6 +17,7 @@ export class Store { "client": "Cider", "clear_on_pause": true, "hide_buttons": false, + "hide_timestamp": false, "state_format": "by {artist}", "details_format": "{title}", }, From f7f0db98bc12bbded5872e01195df1e2ed21709a Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Mon, 2 May 2022 12:47:12 +0200 Subject: [PATCH 3/8] add to en_us and source --- src/i18n/en_US.json | 1 + src/i18n/source/en_US.json | 1 + 2 files changed, 2 insertions(+) diff --git a/src/i18n/en_US.json b/src/i18n/en_US.json index 78413968..6236b726 100644 --- a/src/i18n/en_US.json +++ b/src/i18n/en_US.json @@ -427,6 +427,7 @@ "settings.option.connectivity.discordRPC.clientName": "Client Name", "settings.option.connectivity.discordRPC.clearOnPause": "Clear Discord Rich Presence on Pause", "settings.option.connectivity.discordRPC.hideButtons": "Hide buttons on Discord Rich Presence", + "settings.option.connectivity.discordRPC.hideTimestamp": "Hide timestamp on Discord Rich Presence", "settings.option.connectivity.discordRPC.detailsFormat": "Details Format", "settings.option.connectivity.discordRPC.stateFormat": "State Format", "settings.option.connectivity.lastfmScrobble": "Last.fm Scrobbling", diff --git a/src/i18n/source/en_US.json b/src/i18n/source/en_US.json index 71d7a3d8..6f0ce8f2 100644 --- a/src/i18n/source/en_US.json +++ b/src/i18n/source/en_US.json @@ -428,6 +428,7 @@ "settings.option.connectivity.discordRPC.clientName": "Client Name", "settings.option.connectivity.discordRPC.clearOnPause": "Clear Discord Rich Presence on Pause", "settings.option.connectivity.discordRPC.hideButtons": "Hide buttons on Discord Rich Presence", + "settings.option.connectivity.discordRPC.hideTimestamp": "Hide timestamp on Discord Rich Presence", "settings.option.connectivity.discordRPC.detailsFormat": "Details Format", "settings.option.connectivity.discordRPC.stateFormat": "State Format", "settings.option.connectivity.lastfmScrobble": "Last.fm Scrobbling", From c4acb794e64c735c08de136a85f9f74b11dd3883 Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Mon, 2 May 2022 12:48:54 +0200 Subject: [PATCH 4/8] add to hu_hu --- src/i18n/hu_HU.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/i18n/hu_HU.json b/src/i18n/hu_HU.json index 55706b2c..ec5a97de 100644 --- a/src/i18n/hu_HU.json +++ b/src/i18n/hu_HU.json @@ -414,6 +414,7 @@ "settings.option.connectivity.discordRPC.clientName": "Kliensnév", "settings.option.connectivity.discordRPC.clearOnPause": "Rich Presence törlése megállításnál", "settings.option.connectivity.discordRPC.hideButtons": "Rich Presence gombok elrejtése", + "settings.option.connectivity.discordRPC.hideTimestamp": "Rich Presence visszaszámláló elrejtése", "settings.option.connectivity.discordRPC.detailsFormat": "Részletek formátuma", "settings.option.connectivity.discordRPC.stateFormat": "Állapot formátuma", "settings.option.connectivity.lastfmScrobble": "Last.fm Scrobbling", From c2e768837fb91a4046e8bbe55bf1bee15852c2d9 Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Sat, 7 May 2022 20:39:39 +0200 Subject: [PATCH 5/8] Update discordrpc.ts --- src/main/plugins/discordrpc.ts | 316 ++++++++++++++------------------- 1 file changed, 132 insertions(+), 184 deletions(-) diff --git a/src/main/plugins/discordrpc.ts b/src/main/plugins/discordrpc.ts index 460b57bc..c943eb68 100644 --- a/src/main/plugins/discordrpc.ts +++ b/src/main/plugins/discordrpc.ts @@ -1,30 +1,29 @@ -import * as RPC from 'discord-rpc' +import {AutoClient} from 'discord-auto-rpc' import {ipcMain} from "electron"; import fetch from 'electron-fetch' export default class DiscordRPC { - /** - * Private variables for interaction in plugins - */ - private _utils: any; - private _app: any; - private _attributes: any; - private _connection: boolean = false; - /** * Base Plugin Details (Eventually implemented into a GUI in settings) */ public name: string = 'Discord Rich Presence'; public description: string = 'Discord RPC plugin for Cider'; - public version: string = '1.0.0'; + public version: string = '1.1.0'; public author: string = 'vapormusic/Core (Cider Collective)'; + /** + * Private variables for interaction in plugins + */ + private _utils: any; + private _attributes: any; + private ready: boolean = false; + /** * Plugin Initialization */ private _client: any = null; - private _activity: RPC.Presence = { + private _activityCache: any = { details: '', state: '', largeImageKey: '', @@ -34,15 +33,72 @@ export default class DiscordRPC { instance: false }; - private _activityCache: RPC.Presence = { - details: '', - state: '', - largeImageKey: '', - largeImageText: '', - smallImageKey: '', - smallImageText: '', - instance: false - }; + /******************************************************************************************* + * Public Methods + * ****************************************************************************************/ + + /** + * Runs on plugin load (Currently run on application start) + */ + constructor(utils: any) { + this._utils = utils; + console.debug(`[Plugin][${this.name}] Loading Complete.`); + } + + /** + * Runs on app ready + */ + onReady(_win: any): void { + const self = this + this.connect(); + console.debug(`[Plugin][${this.name}] Ready.`); + ipcMain.on('updateRPCImage', (_event, imageurl) => { + if (!this._utils.getStoreValue("general.privateEnabled")) { + fetch('https://api.cider.sh/v1/images', { + + method: 'POST', + body: JSON.stringify({url: imageurl}), + headers: { + 'Content-Type': 'application/json', + 'User-Agent': _win.webContents.getUserAgent() + }, + }) + .then(res => res.json()) + .then(function (json) { + self._attributes["artwork"]["url"] = json.url + self.setActivity(self._attributes) + }) + } + }) + } + + /** + * Runs on app stop + */ + onBeforeQuit(): void { + console.debug(`[Plugin][${this.name}] Stopped.`); + } + + /** + * Runs on playback State Change + * @param attributes Music Attributes (attributes.status = current state) + */ + onPlaybackStateDidChange(attributes: object): void { + this._attributes = attributes + this.setActivity(attributes) + + } + + /** + * Runs on song change + * @param attributes Music Attributes + */ + onNowPlayingItemDidChange(attributes: object): void { + this._attributes = attributes + this.setActivity(attributes) + + } + /******************************************************************************************* * Private Methods @@ -58,58 +114,86 @@ export default class DiscordRPC { } const clientId = this._utils.getStoreValue("general.discordrpc.client") === "Cider" ? '911790844204437504' : '886578863147192350'; - // Apparently needed for ask to join, join, spectate etc. - RPC.register(clientId) - // Create the client - this._client = new RPC.Client({transport: "ipc"}); + this._client = new AutoClient({transport: "ipc"}); // Runs on Ready - this._client.on('ready', () => { + this._client.once('ready', () => { console.info(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${this._client.user.id}.`); + + if (this._activityCache && this._activityCache.details && this._activityCache.state) { + console.info(`[DiscordRPC][connect] Restoring activity cache.`); + this._client.setActivity(this._activityCache) + } }) - // Handles Errors - this._client.on('error', (err: any) => { - console.error(`[DiscordRPC] ${err}`); - this.disconnect() - }); - - // If Discord is closed, allow reconnecting - this._client.transport.once('close', () => { - console.info(`[DiscordRPC] Connection closed`); - this.disconnect() - }); - // Login to Discord - this._client.login({clientId}) + this._client.endlessLogin({clientId: clientId}) .then(() => { - this._connection = true; + this.ready = true }) .catch((e: any) => console.error(`[DiscordRPC][connect] ${e}`)); } /** - * Disconnects from Discord RPC + * Sets the activity + * @param attributes Music Attributes */ - private disconnect() { + private setActivity(attributes: any) { if (!this._client) { return } - this._client.destroy().then(() => { - this._connection = false; - console.log('[DiscordRPC][disconnect] Disconnected from discord.') - }).catch((e: any) => console.error(`[DiscordRPC][disconnect] ${e}`)); + // Check if show buttons is (true) or (false) + let activity: Object = { + details: this._utils.getStoreValue("general.discordrpc.details_format"), + state: this._utils.getStoreValue("general.discordrpc.state_format"), + largeImageKey: attributes?.artwork?.url?.replace('{w}', '1024').replace('{h}', '1024'), + largeImageText: attributes.albumName, + instance: false // Whether the activity is in a game session + } - // Clean up, allow creating a new connection - this._client = null; + // Filter the activity + activity = this.filterActivity(activity, attributes) + + if (!this.ready) { + this._activityCache = activity + return + } + + // Set the activity + if (!attributes.status && this._utils.getStoreValue("general.discordrpc.clear_on_pause")) { + this._client.clearActivity() + } else if (activity && this._activityCache !== activity) { + this._client.setActivity(activity) + } + this._activityCache = activity; } /** * Filter the Discord activity object */ - private static filterActivity(activity: any, attributes: any): Object { + private filterActivity(activity: any, attributes: any): Object { + + // Add the buttons if people want them + if (!this._utils.getStoreValue("general.discordrpc.hide_buttons")) { + activity.buttons = [ + {label: 'Listen on Cider', url: attributes.url.cider}, + {label: 'View on Apple Music', url: attributes.url.appleMusic} + ] //To change attributes.url => preload/cider-preload.js + } + + // Add the timestamp if its playing + if (attributes.status) { + activity.startTimestamp = Date.now() - (attributes?.durationInMillis - attributes?.remainingTime) + activity.endTimestamp = attributes.endTime + } + + // If the user wants to keep the activity when paused + if (!this._utils.getStoreValue("general.discordrpc.clear_on_pause")) { + activity.smallImageKey = attributes.status ? 'play' : 'pause'; + activity.smallImageText = attributes.status ? 'Playing' : 'Paused'; + } /** * Works with: @@ -173,140 +257,4 @@ export default class DiscordRPC { } return activity } - - /** - * Sets the activity - * @param {activity} activity - */ - private setActivity(activity: any) { - if (!this._connection || !this._client || !activity) { - return - } - - // Filter the activity - activity = DiscordRPC.filterActivity(activity, this._attributes) - - // Set the activity - if (!this._attributes.status && this._utils.getStoreValue("general.discordrpc.clear_on_pause")) { - this._client.clearActivity() - } else if (this._activity && this._activityCache !== this._activity && this._activity.details) { - this._client.setActivity(activity) - this._activityCache = this._activity; - } - } - - /** - * Sets the activity of the client - * @param {object} attributes - */ - private updateActivity(attributes: any) { - if (!this._utils.getStoreValue("general.discordrpc.enabled") || this._utils.getStoreValue("general.privateEnabled")) { - return - } else if (!this._client || !this._connection) { - this.connect() - } - - // Check if show buttons is (true) or (false) - this._activity = { - details: this._utils.getStoreValue("general.discordrpc.details_format"), - state: this._utils.getStoreValue("general.discordrpc.state_format"), - largeImageKey: attributes?.artwork?.url?.replace('{w}', '1024').replace('{h}', '1024'), - largeImageText: attributes.albumName, - instance: false // Whether the activity is in a game session - } - - // Add the buttons if people want them - if (!this._utils.getStoreValue("general.discordrpc.hide_buttons")) { - this._activity.buttons = [ - {label: 'Listen on Cider', url: attributes.url.cider}, - {label: 'View on Apple Music', url: attributes.url.appleMusic} - ] //To change attributes.url => preload/cider-preload.js - } - - // Add the timestamp if its playing and people want them - if (!this._utils.getStoreValue("general.discordrpc.hide_timestamp")) { - if (attributes.status) { - this._activity.startTimestamp = Date.now() - (attributes?.durationInMillis - attributes?.remainingTime) - this._activity.endTimestamp = attributes.endTime - } - } - - // If the user wants to keep the activity when paused - if (!this._utils.getStoreValue("general.discordrpc.clear_on_pause")) { - this._activity.smallImageKey = attributes.status ? 'play' : 'pause'; - this._activity.smallImageText = attributes.status ? 'Playing' : 'Paused'; - } - - this.setActivity(this._activity) - } - - /******************************************************************************************* - * Public Methods - * ****************************************************************************************/ - - /** - * Runs on plugin load (Currently run on application start) - */ - constructor(utils: { getStore: () => any; getApp: () => any; }) { - this._utils = utils; - console.debug(`[Plugin][${this.name}] Loading Complete.`); - this._app = utils.getApp(); - } - - /** - * Runs on app ready - */ - onReady(_win: any): void { - let self = this - this.connect(); - console.debug(`[Plugin][${this.name}] Ready.`); - ipcMain.on('updateRPCImage', (_event, imageurl) => { - if (!this._utils.getStoreValue("general.privateEnabled")) { - fetch('https://api.cider.sh/v1/images', { - - method: 'POST', - body: JSON.stringify({url: imageurl}), - headers: { - 'Content-Type': 'application/json', - 'User-Agent': _win.webContents.getUserAgent() - }, - }) - .then(res => res.json()) - .then(function (json) { - self._attributes["artwork"]["url"] = json.url - self.updateActivity(self._attributes) - }) - } - }) - } - - /** - * Runs on app stop - */ - onBeforeQuit(): void { - if (this._client) { - this.disconnect() - } - console.debug(`[Plugin][${this.name}] Stopped.`); - } - - /** - * Runs on playback State Change - * @param attributes Music Attributes (attributes.status = current state) - */ - onPlaybackStateDidChange(attributes: object): void { - this._attributes = attributes - this.updateActivity(attributes) - - } - - /** - * Runs on song change - * @param attributes Music Attributes - */ - onNowPlayingItemDidChange(attributes: object): void { - this._attributes = attributes - this.updateActivity(attributes) - - } } From ab9d209eba14a4a3d3039d0f73df9a153218060d Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Sat, 7 May 2022 20:45:03 +0200 Subject: [PATCH 6/8] Add check to discordrpc.ts --- src/main/plugins/discordrpc.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/plugins/discordrpc.ts b/src/main/plugins/discordrpc.ts index c943eb68..fe933903 100644 --- a/src/main/plugins/discordrpc.ts +++ b/src/main/plugins/discordrpc.ts @@ -183,8 +183,8 @@ export default class DiscordRPC { ] //To change attributes.url => preload/cider-preload.js } - // Add the timestamp if its playing - if (attributes.status) { + // Add the timestamp if its playing and people want them + if (!this._utils.getStoreValue("general.discordrpc.hide_timestamp") && attributes.status) { activity.startTimestamp = Date.now() - (attributes?.durationInMillis - attributes?.remainingTime) activity.endTimestamp = attributes.endTime } From 050cd3e0b649bf1a9ad76f0d052d6d4973f5b9eb Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Sat, 7 May 2022 20:51:48 +0200 Subject: [PATCH 7/8] Add new setting --- src/renderer/views/pages/settings.ejs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/renderer/views/pages/settings.ejs b/src/renderer/views/pages/settings.ejs index 8e9bc841..71ea0a58 100644 --- a/src/renderer/views/pages/settings.ejs +++ b/src/renderer/views/pages/settings.ejs @@ -1067,6 +1067,17 @@ +
+
+ {{$root.getLz('settings.option.connectivity.discordRPC.hideTimestamp')}} +
+
+ +
+
+
{{$root.getLz('settings.option.connectivity.discordRPC.detailsFormat')}}
@@ -1639,4 +1650,4 @@ }, } }) - \ No newline at end of file + From 30bf34aa22a0793bc86cafc878a0c97927309f74 Mon Sep 17 00:00:00 2001 From: Amaru8 <52407090+Amaru8@users.noreply.github.com> Date: Mon, 9 May 2022 20:14:06 +0200 Subject: [PATCH 8/8] update settings.ejs --- src/renderer/views/pages/settings.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/views/pages/settings.ejs b/src/renderer/views/pages/settings.ejs index 71ea0a58..963cd6f0 100644 --- a/src/renderer/views/pages/settings.ejs +++ b/src/renderer/views/pages/settings.ejs @@ -1650,4 +1650,4 @@ }, } }) - + \ No newline at end of file