2022-07-14 12:25:24 -07:00
|
|
|
/**
|
|
|
|
* @author Daniel D. Scalzi
|
|
|
|
* @license CC-BY-NC 4.0 - https://creativecommons.org/licenses/by-nc/4.0/
|
|
|
|
*/
|
|
|
|
|
2017-11-29 22:40:56 -08:00
|
|
|
// Work in progress
|
2018-08-22 11:21:49 -07:00
|
|
|
const logger = require('./loggerutil')('%c[DiscordWrapper]', 'color: #7289da; font-weight: bold')
|
|
|
|
|
2022-01-23 15:37:21 -08:00
|
|
|
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', () => {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('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')) {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('Unable to initialize Discord Rich Presence, no client detected.')
|
2017-11-29 22:40:56 -08:00
|
|
|
} else {
|
2018-08-22 11:21:49 -07:00
|
|
|
logger.log('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
|
|
|
}
|