discordrpc now reloads/clears activity with setting changes
This commit is contained in:
parent
f8e37029ac
commit
a3ca0b09f4
4 changed files with 42 additions and 27 deletions
|
@ -48,15 +48,21 @@ export default class DiscordRPC {
|
|||
* Runs on app ready
|
||||
*/
|
||||
onReady(_win: any): void {
|
||||
const self = this;
|
||||
this.connect();
|
||||
console.debug(`[Plugin][${this.name}] Ready.`);
|
||||
ipcMain.on("updateRPCImage", async (_event, imageurl) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up ipc listeners for the plugin
|
||||
*/
|
||||
onRendererReady() {
|
||||
const self = this;
|
||||
ipcMain.on("discordrpc:updateImage", async (_event, imageurl) => {
|
||||
if (!this._utils.getStoreValue("general.privateEnabled")) {
|
||||
let b64data = "";
|
||||
let postbody = "";
|
||||
if (imageurl.startsWith("/ciderlocalart")) {
|
||||
let port = await _win.webContents.executeJavaScript(`app.clientPort`);
|
||||
let port = await this._utils.getWindow().webContents.executeJavaScript(`app.clientPort`);
|
||||
console.log("http://localhost:" + port + imageurl);
|
||||
const response = await fetch("http://localhost:" + port + imageurl);
|
||||
b64data = (await response.buffer()).toString("base64");
|
||||
|
@ -66,7 +72,7 @@ export default class DiscordRPC {
|
|||
body: postbody,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": _win.webContents.getUserAgent(),
|
||||
"User-Agent": this._utils.getWindow().webContents.getUserAgent(),
|
||||
},
|
||||
})
|
||||
.then((res) => res.json())
|
||||
|
@ -81,7 +87,7 @@ export default class DiscordRPC {
|
|||
body: postbody,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": _win.webContents.getUserAgent(),
|
||||
"User-Agent": this._utils.getWindow().webContents.getUserAgent(),
|
||||
},
|
||||
})
|
||||
.then((res) => res.json())
|
||||
|
@ -92,17 +98,22 @@ export default class DiscordRPC {
|
|||
}
|
||||
}
|
||||
});
|
||||
ipcMain.on("reloadRPC", () => {
|
||||
ipcMain.on("discordrpc:reload", (_event, configUpdate = null) => {
|
||||
console.log(`[DiscordRPC][reload] Reloading DiscordRPC.`);
|
||||
this._client.destroy();
|
||||
|
||||
if (this._client) {
|
||||
this._client.destroy();
|
||||
}
|
||||
|
||||
if (!this._utils.getStoreValue("connectivity.discord_rpc.enabled")) return
|
||||
this._client
|
||||
.endlessLogin({
|
||||
clientId: this._utils.getStoreValue("connectivity.discord_rpc.client") === "Cider" ? "911790844204437504" : "886578863147192350",
|
||||
})
|
||||
.then(() => {
|
||||
console.log(`[DiscordRPC][reload] DiscordRPC Reloaded.`);
|
||||
this.ready = true;
|
||||
this._utils.getWindow().webContents.send("rpcReloaded", this._client.user);
|
||||
if (configUpdate == null) 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);
|
||||
|
@ -111,6 +122,13 @@ export default class DiscordRPC {
|
|||
.catch((e: any) => console.error(`[DiscordRPC][reload] ${e}`));
|
||||
// this.connect(true)
|
||||
});
|
||||
ipcMain.on("onPrivacyModeChange", (_event, enabled) => {
|
||||
if (enabled && this._client) {
|
||||
this._client.clearActivity();
|
||||
} else if (!enabled && this._activityCache && this._activityCache.details && this._activityCache.state) {
|
||||
this._client.setActivity(this._activityCache);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -126,11 +144,7 @@ export default class DiscordRPC {
|
|||
*/
|
||||
onPlaybackStateDidChange(attributes: object): void {
|
||||
this._attributes = attributes;
|
||||
if (this._utils.getStoreValue("general.privateEnabled")) {
|
||||
this._client.clearActivity();
|
||||
} else {
|
||||
this.setActivity(attributes);
|
||||
}
|
||||
this.setActivity(attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,11 +153,7 @@ export default class DiscordRPC {
|
|||
*/
|
||||
onNowPlayingItemDidChange(attributes: object): void {
|
||||
this._attributes = attributes;
|
||||
if (this._utils.getStoreValue("general.privateEnabled")) {
|
||||
this._client.clearActivity();
|
||||
} else {
|
||||
this.setActivity(attributes);
|
||||
}
|
||||
this.setActivity(attributes);
|
||||
}
|
||||
|
||||
/*******************************************************************************************
|
||||
|
@ -166,7 +176,7 @@ export default class DiscordRPC {
|
|||
this._client.once("ready", () => {
|
||||
console.info(`[DiscordRPC][connect] Successfully Connected to Discord. Authed for user: ${this._client.user.id}.`);
|
||||
|
||||
if (this._activityCache && this._activityCache.details && this._activityCache.state) {
|
||||
if (this._activityCache && this._activityCache.details && this._activityCache.state && !this._utils.getStoreValue("general.privateEnabled")) {
|
||||
console.info(`[DiscordRPC][connect] Restoring activity cache.`);
|
||||
this._client.setActivity(this._activityCache);
|
||||
}
|
||||
|
@ -209,12 +219,11 @@ export default class DiscordRPC {
|
|||
return;
|
||||
}
|
||||
|
||||
// Set the activity
|
||||
if (this._utils.getStoreValue("general.privateEnabled")) {
|
||||
this._client.clearActivity();
|
||||
} else if (!attributes.status && this._utils.getStoreValue("connectivity.discord_rpc.clear_on_pause")) {
|
||||
|
||||
if (!attributes.status && this._utils.getStoreValue("connectivity.discord_rpc.clear_on_pause")) {
|
||||
this._client.clearActivity();
|
||||
} else if (activity && this._activityCache !== activity) {
|
||||
if (this._utils.getStoreValue("general.privateEnabled")) return
|
||||
this._client.setActivity(activity);
|
||||
}
|
||||
this._activityCache = activity;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue