HeliosLauncher/app/assets/js/discordwrapper.js
Daniel Scalzi ee55446cd6
Temporary fix to DiscordRPC not shutting down properly.
Something must have changed on discord's end, as our previous solution is no longer working. The latest changes to the module include a clearActivity() method, however it is not available on npm. Until it is, we are using the implementation directly.
2018-05-29 23:42:27 -04:00

52 lines
1.5 KiB
JavaScript

// Work in progress
const {Client} = require('discord-rpc')
const ConfigManager = require('./configmanager.js')
let rpc
let activity
exports.initRPC = function(genSettings, servSettings, initialDetails = 'Waiting for Client..'){
rpc = new Client({ transport: 'ipc' })
rpc.on('ready', () => {
activity = {
details: initialDetails,
state: 'Server: ' + servSettings.shortId,
largeImageKey: servSettings.largeImageKey,
largeImageText: servSettings.largeImageText,
smallImageKey: genSettings.smallImageKey,
smallImageText: genSettings.smallImageText,
startTimestamp: new Date().getTime() / 1000,
instance: false
}
rpc.setActivity(activity)
})
rpc.login(genSettings.clientID).catch(error => {
if(error.message.includes('ENOENT')) {
console.log('Unable to initialize Discord Rich Presence, no client detected.')
} else {
console.log('Unable to initialize Discord Rich Presence: ' + error.message, error)
}
})
return rpc
}
exports.updateDetails = function(details){
if(activity == null){
console.error('Discord RPC is not initialized and therefore cannot be updated.')
}
activity.details = details
rpc.setActivity(activity)
}
exports.shutdownRPC = function(){
if(!rpc) return
// Workaround until discord rpc releases clearActivity()
rpc.request('SET_ACTIVITY', {pid: process.pid})
rpc.destroy()
rpc = null
activity = null
}