From 4bb003fc963fdf7f63e65480c4f518701a121ada Mon Sep 17 00:00:00 2001 From: NoCrypt <57245077+NoCrypt@users.noreply.github.com> Date: Sun, 28 Jul 2024 17:48:10 +0700 Subject: [PATCH] feat: Discord RPC Toggle --- common/modules/util.js | 1 + common/views/Player/MediaHandler.svelte | 6 +-- .../views/Settings/InterfaceSettings.svelte | 6 +++ common/views/Settings/Settings.svelte | 2 + electron/src/main/discord.js | 52 ++++++++++++++----- 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/common/modules/util.js b/common/modules/util.js index 8882ddd7..76d8cd26 100644 --- a/common/modules/util.js +++ b/common/modules/util.js @@ -149,6 +149,7 @@ export const defaults = { enableDoH: false, doHURL: 'https://cloudflare-dns.com/dns-query', disableSubtitleBlur: SUPPORTS.isAndroid, + enableRPC: false, showDetailsInRPC: true, smoothScroll: false, cards: 'small', diff --git a/common/views/Player/MediaHandler.svelte b/common/views/Player/MediaHandler.svelte index b3cf5f63..1033bf90 100644 --- a/common/views/Player/MediaHandler.svelte +++ b/common/views/Player/MediaHandler.svelte @@ -217,8 +217,8 @@ assets: { large_text: np.title, large_image: np.thumbnail, - small_image: 'logo', - small_text: 'https://github.com/ThaUnknown/miru' + small_image: 'https://raw.githubusercontent.com/NoCrypt/migu/main/common/public/logo_filled.png', + small_text: 'https://github.com/NoCrypt/migu' }, instance: true, type: 3 @@ -234,7 +234,7 @@ activity.buttons = [ { label: 'Download app', - url: 'https://github.com/ThaUnknown/miru/releases/latest' + url: 'https://github.com/NoCrypt/migu/releases/latest' }, { label: 'Watch on Migu', diff --git a/common/views/Settings/InterfaceSettings.svelte b/common/views/Settings/InterfaceSettings.svelte index 1ca8253a..bebc4d3f 100644 --- a/common/views/Settings/InterfaceSettings.svelte +++ b/common/views/Settings/InterfaceSettings.svelte @@ -13,6 +13,12 @@ {#if SUPPORTS.discord}

Rich Pressence Settings

+ +
+ + +
+
diff --git a/common/views/Settings/Settings.svelte b/common/views/Settings/Settings.svelte index 09cad3c7..ca2adf79 100644 --- a/common/views/Settings/Settings.svelte +++ b/common/views/Settings/Settings.svelte @@ -41,6 +41,7 @@ return json.map(({ body, tag_name: version, published_at: date, assets }) => ({ body, version, date, assets })) })() IPC.emit('show-discord-status', settings.value.showDetailsInRPC) + IPC.emit('toggle-rpc', settings.value.enableRPC) diff --git a/electron/src/main/discord.js b/electron/src/main/discord.js index c42857db..4efc2d3c 100644 --- a/electron/src/main/discord.js +++ b/electron/src/main/discord.js @@ -9,13 +9,13 @@ export default class Discord { details: 'Stream anime torrents, real-time.', state: 'Watching anime', assets: { - small_image: 'logo', - small_text: 'https://github.com/ThaUnknown/miru' + small_image: 'https://raw.githubusercontent.com/NoCrypt/migu/main/common/public/logo_filled.png', + small_text: 'https://github.com/NoCrypt/migu' }, buttons: [ { label: 'Download app', - url: 'https://github.com/ThaUnknown/miru/releases/latest' + url: 'https://github.com/NoCrypt/migu/releases/latest' } ], instance: true, @@ -30,23 +30,31 @@ export default class Discord { /** @type {Discord['defaultStatus'] | undefined} */ cachedPresence + rpcEnabled = true // Property to track RPC state + /** @param {import('electron').BrowserWindow} window */ constructor (window) { ipcMain.on('show-discord-status', (event, data) => { this.allowDiscordDetails = data - this.debouncedDiscordRPC(this.allowDiscordDetails ? this.cachedPresence : undefined) + this.debouncedDiscordRPC(this.allowDiscordDetails && this.rpcEnabled ? this.cachedPresence : undefined) }) ipcMain.on('discord', (event, data) => { this.cachedPresence = data - this.debouncedDiscordRPC(this.allowDiscordDetails ? this.cachedPresence : undefined) + this.debouncedDiscordRPC(this.allowDiscordDetails && this.rpcEnabled ? this.cachedPresence : undefined) + }) + + ipcMain.on('toggle-rpc', (event, data) => { + this.toggleRPC(data) }) this.discord.on('ready', async () => { - this.setDiscordRPC(this.cachedPresence || this.defaultStatus) - this.discord.subscribe('ACTIVITY_JOIN_REQUEST') - this.discord.subscribe('ACTIVITY_JOIN') - this.discord.subscribe('ACTIVITY_SPECTATE') + if (this.rpcEnabled) { + this.setDiscordRPC(this.cachedPresence || this.defaultStatus) + this.discord.subscribe('ACTIVITY_JOIN_REQUEST') + this.discord.subscribe('ACTIVITY_JOIN') + this.discord.subscribe('ACTIVITY_SPECTATE') + } }) this.discord.on('ACTIVITY_JOIN', ({ secret }) => { @@ -59,15 +67,33 @@ export default class Discord { } loginRPC () { - this.discord.login({ clientId: '954855428355915797' }).catch(() => { - setTimeout(() => this.loginRPC(), 5000).unref() - }) + if (this.rpcEnabled) { + this.discord.login({ clientId: '954855428355915797' }).catch(() => { + setTimeout(() => this.loginRPC(), 5000).unref() + }) + } } setDiscordRPC (data = this.defaultStatus) { - if (this.discord.user && data) { + if (this.discord.user && data && this.rpcEnabled) { data.pid = process.pid this.discord.request('SET_ACTIVITY', data) } } + + clearDiscordRPC () { + if (this.discord.user) { + this.discord.request('SET_ACTIVITY', { pid: process.pid }) + } + } + + toggleRPC (enabled) { + this.rpcEnabled = enabled + if (this.rpcEnabled) { + this.loginRPC() + this.setDiscordRPC(this.cachedPresence || this.defaultStatus) + } else { + this.clearDiscordRPC() + } + } }