HeliosLauncher/app/assets/js/discordwrapper.js
2018-05-30 11:41:04 -04:00

50 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)
}
})
}
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
}