Add an option to control discord RPC

This commit is contained in:
kyngs 2023-06-03 20:51:32 +02:00
parent d82bf27d41
commit e12a24570a
2 changed files with 78 additions and 4 deletions

View file

@ -250,7 +250,31 @@ let status = null
const discord = new Client({
transport: 'ipc'
})
function setDiscordRPC (event, data) {
function setDiscordRPC (data) {
if (!data) {
data = {
activity: {
timestamps: {
start: Date.now()
},
details: 'Stream anime torrents, real-time.',
state: 'Watching anime',
assets: {
small_image: 'logo',
small_text: 'https://github.com/ThaUnknown/miru'
},
buttons: [
{
label: 'Download app',
url: 'https://github.com/ThaUnknown/miru/releases/latest'
}
],
instance: true,
type: 3
}
}
}
status = data
if (discord?.user && status) {
status.pid = process.pid
@ -258,9 +282,39 @@ function setDiscordRPC (event, data) {
}
}
ipcMain.on('discord', setDiscordRPC)
let allowDiscordDetails = false
let requestedDiscordDetails = false
let rpcStarted = false
let cachedPresence = null
ipcMain.on('discord_status', (event, data) => {
requestedDiscordDetails = data;
if (!rpcStarted) {
handleRPC()
setInterval(handleRPC, 5000) //According to Discord documentation, clients can only update their presence 5 times per 20 seconds. We will add an extra second to be safe.
rpcStarted = true
}
})
function handleRPC() {
if (allowDiscordDetails === requestedDiscordDetails) return
allowDiscordDetails = requestedDiscordDetails
if (!allowDiscordDetails) {
setDiscordRPC(null)
} else if (cachedPresence) {
setDiscordRPC(cachedPresence)
}
}
ipcMain.on('discord', (event, data) => {
cachedPresence = data
if (allowDiscordDetails) {
setDiscordRPC(data)
}
})
discord.on('ready', async () => {
setDiscordRPC(null, status)
setDiscordRPC(status)
discord.subscribe('ACTIVITY_JOIN_REQUEST')
discord.subscribe('ACTIVITY_JOIN')
discord.subscribe('ACTIVITY_SPECTATE')

View file

@ -19,7 +19,8 @@
enableDoH: false,
doHURL: 'https://cloudflare-dns.com/dns-query',
disableSubtitleBlur: false,
catURL: decodeURIComponent(atob('aHR0cHMlM0ElMkYlMkZueWFhLnNp'))
catURL: decodeURIComponent(atob('aHR0cHMlM0ElMkYlMkZueWFhLnNp')),
showDetailsInRPC: true
}
localStorage.removeItem('relations') // TODO: remove
export const set = { ...defaults, ...(JSON.parse(localStorage.getItem('settings')) || {}) }
@ -90,6 +91,7 @@
const json = await res.json()
return json.map(({ body, tag_name: version }) => ({ body, version }))
})()
window.IPC.emit('discord_status', set.showDetailsInRPC)
</script>
<script>
@ -117,6 +119,11 @@
icon: 'hub',
desc: 'Torrent client settings, and preferences.'
},
discord: {
name: 'Discord',
icon: 'discord',
desc: 'Discord Rich Presence settings.'
},
changelog: {
name: 'Changelog',
icon: 'list',
@ -125,6 +132,7 @@
}
let settings = set
$: saveSettings(settings)
$: window.IPC.emit('discord_status', settings.showDetailsInRPC)
function saveSettings () {
localStorage.setItem('settings', JSON.stringify(settings))
}
@ -467,6 +475,18 @@
</div>
</div>
</Tab>
<Tab>
<div class='root p-20 m-20'>
<div
class='custom-switch mb-10 pl-10 font-size-16 w-300'
data-toggle='tooltip'
data-placement='bottom'
data-title='Enables showing currently played anime and episode in Discord Rich Presence.'>
<input type='checkbox' id="rpc-details" bind:checked={settings.showDetailsInRPC}/>
<label for='rpc-details'>Show details in Discord Rich Presence</label>
</div>
</div>
</Tab>
<Tab>
<div class='root m-20 px-20 pre-wrap'>
{#await changeLog}