Skip to content

Commit

Permalink
feat: Discord RPC Toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
NoCrypt committed Jul 28, 2024
1 parent 7bc56b4 commit 4bb003f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions common/modules/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions common/views/Player/MediaHandler.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions common/views/Settings/InterfaceSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

{#if SUPPORTS.discord}
<h4 class='mb-10 font-weight-bold'>Rich Pressence Settings</h4>
<SettingCard title='Enable Discord Rich Presence' description='Enables Discord Rich Presence'>
<div class='custom-switch'>
<input type='checkbox' id='rpc-enable' bind:checked={settings.enableRPC} />
<label for='rpc-enable'>{settings.enableRPC ? 'On' : 'Off'}</label>
</div>
</SettingCard>
<SettingCard title='Show Details in Discord Rich Presence' description='Shows currently played anime and episode in Discord rich presence.'>
<div class='custom-switch'>
<input type='checkbox' id='rpc-details' bind:checked={settings.showDetailsInRPC} />
Expand Down
2 changes: 2 additions & 0 deletions common/views/Settings/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</script>

<script>
Expand Down Expand Up @@ -102,6 +103,7 @@
IPC.off('player', playerListener)
})
$: IPC.emit('show-discord-status', $settings.showDetailsInRPC)
$: IPC.emit('toggle-rpc', $settings.enableRPC)
IPC.on('path', pathListener)
IPC.on('player', playerListener)
</script>
Expand Down
52 changes: 39 additions & 13 deletions electron/src/main/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }) => {
Expand All @@ -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()
}
}
}

0 comments on commit 4bb003f

Please sign in to comment.