2017-11-29 22:40:56 -08:00
|
|
|
// Work in progress
|
2022-12-24 19:17:30 -08:00
|
|
|
const { LoggerUtil } = require('helios-core')
|
2018-08-22 11:21:49 -07:00
|
|
|
|
2022-12-24 19:17:30 -08:00
|
|
|
const logger = LoggerUtil.getLogger('DiscordWrapper')
|
|
|
|
|
|
|
|
const { Client } = require('discord-rpc-patch')
|
2017-11-29 22:40:56 -08:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
let client
|
2018-01-18 20:45:50 -08:00
|
|
|
let activity
|
2017-11-29 22:40:56 -08:00
|
|
|
|
2018-01-18 20:45:50 -08:00
|
|
|
exports.initRPC = function(genSettings, servSettings, initialDetails = 'Waiting for Client..'){
|
2018-07-22 08:40:15 -07:00
|
|
|
client = new Client({ transport: 'ipc' })
|
2017-11-29 22:40:56 -08:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
activity = {
|
|
|
|
details: initialDetails,
|
|
|
|
state: 'Server: ' + servSettings.shortId,
|
|
|
|
largeImageKey: servSettings.largeImageKey,
|
|
|
|
largeImageText: servSettings.largeImageText,
|
|
|
|
smallImageKey: genSettings.smallImageKey,
|
|
|
|
smallImageText: genSettings.smallImageText,
|
2018-10-30 22:25:38 -07:00
|
|
|
startTimestamp: new Date().getTime(),
|
2018-07-22 08:40:15 -07:00
|
|
|
instance: false
|
|
|
|
}
|
2017-12-02 23:13:42 -08:00
|
|
|
|
2018-07-22 08:40:15 -07:00
|
|
|
client.on('ready', () => {
|
2022-12-24 19:17:30 -08:00
|
|
|
logger.info('Discord RPC Connected')
|
2018-07-22 08:40:15 -07:00
|
|
|
client.setActivity(activity)
|
|
|
|
})
|
|
|
|
|
|
|
|
client.login({clientId: genSettings.clientId}).catch(error => {
|
2017-11-29 22:40:56 -08:00
|
|
|
if(error.message.includes('ENOENT')) {
|
2022-12-24 19:17:30 -08:00
|
|
|
logger.info('Unable to initialize Discord Rich Presence, no client detected.')
|
2017-11-29 22:40:56 -08:00
|
|
|
} else {
|
2022-12-24 19:17:30 -08:00
|
|
|
logger.info('Unable to initialize Discord Rich Presence: ' + error.message, error)
|
2017-11-29 22:40:56 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:45:50 -08:00
|
|
|
exports.updateDetails = function(details){
|
|
|
|
activity.details = details
|
2018-07-22 08:40:15 -07:00
|
|
|
client.setActivity(activity)
|
2018-01-18 20:45:50 -08:00
|
|
|
}
|
|
|
|
|
2017-12-02 23:13:42 -08:00
|
|
|
exports.shutdownRPC = function(){
|
2018-07-22 08:40:15 -07:00
|
|
|
if(!client) return
|
|
|
|
client.clearActivity()
|
|
|
|
client.destroy()
|
|
|
|
client = null
|
2018-01-18 20:45:50 -08:00
|
|
|
activity = null
|
2017-11-29 22:40:56 -08:00
|
|
|
}
|