diff --git a/src/main/plugins/discordrpc.ts b/src/main/plugins/discordrpc.ts index a55e89b5..9cc7baf1 100644 --- a/src/main/plugins/discordrpc.ts +++ b/src/main/plugins/discordrpc.ts @@ -70,13 +70,21 @@ export default class DiscordRPC { }) } }) - ipcMain.handle("reloadRPC", (e) => { + ipcMain.on("reloadRPC", () => { console.log(`[DiscordRPC][reload] Reloading DiscordRPC.`); - if (this._client) { - this._client.destroy() - this._client = null - } - this.connect(true) + this._client.destroy() + + this._client.endlessLogin({clientId: this._utils.getStoreValue("general.discordrpc.client") === "Cider" ? '911790844204437504' : '886578863147192350'}) + .then(() => { + this.ready = true + this._utils.getWindow().webContents.send("rpcReloaded", this._client.user) + if (this._activityCache && this._activityCache.details && this._activityCache.state) { + console.info(`[DiscordRPC][reload] Restoring activity cache.`); + this._client.setActivity(this._activityCache) + } + }) + .catch((e: any) => console.error(`[DiscordRPC][reload] ${e}`)); + // this.connect(true) }) } @@ -116,11 +124,10 @@ export default class DiscordRPC { * Connect to Discord RPC * @private */ - private connect(reload = false) { + private connect() { if (!this._utils.getStoreValue("general.discordrpc.enabled")) { return; } - const clientId = this._utils.getStoreValue("general.discordrpc.client") === "Cider" ? '911790844204437504' : '886578863147192350'; // Create the client this._client = new AutoClient({transport: "ipc"}); @@ -133,14 +140,10 @@ export default class DiscordRPC { console.info(`[DiscordRPC][connect] Restoring activity cache.`); this._client.setActivity(this._activityCache) } - - if (reload) { - this._utils.getWindow().webContents.send("rpcReloaded", this._client.user) - } }) // Login to Discord - this._client.endlessLogin({clientId: clientId}) + this._client.endlessLogin({clientId: this._utils.getStoreValue("general.discordrpc.client") === "Cider" ? '911790844204437504' : '886578863147192350'}) .then(() => { this.ready = true })