SkirdaElectronLauncher/app/assets/js/discordwrapper.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

// Work in progress
2022-12-25 03:17:30 +00:00
const { LoggerUtil } = require('helios-core')
2022-12-25 03:17:30 +00:00
const logger = LoggerUtil.getLogger('DiscordWrapper')
const { Client } = require('discord-rpc-patch')
let client
let activity
exports.initRPC = function(genSettings, servSettings, initialDetails = 'Waiting for Client..'){
client = new Client({ transport: 'ipc' })
activity = {
details: initialDetails,
state: 'Server: ' + servSettings.shortId,
largeImageKey: servSettings.largeImageKey,
largeImageText: servSettings.largeImageText,
smallImageKey: genSettings.smallImageKey,
smallImageText: genSettings.smallImageText,
startTimestamp: new Date().getTime(),
instance: false
}
client.on('ready', () => {
2022-12-25 03:17:30 +00:00
logger.info('Discord RPC Connected')
client.setActivity(activity)
})
client.login({clientId: genSettings.clientId}).catch(error => {
if(error.message.includes('ENOENT')) {
2022-12-25 03:17:30 +00:00
logger.info('Unable to initialize Discord Rich Presence, no client detected.')
} else {
2022-12-25 03:17:30 +00:00
logger.info('Unable to initialize Discord Rich Presence: ' + error.message, error)
}
})
}
exports.updateDetails = function(details){
activity.details = details
client.setActivity(activity)
}
exports.shutdownRPC = function(){
if(!client) return
client.clearActivity()
client.destroy()
client = null
activity = null
}