Prevent multiple Discord clients

This commit is contained in:
Samuel Elliott 2022-09-26 16:42:24 +01:00
parent d9fb2657e6
commit 45c315978d
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
2 changed files with 20 additions and 2 deletions

View File

@ -393,11 +393,13 @@ export class EmbeddedPresenceMonitor extends ZncDiscordPresence {
await this.loop();
}
if (this._running === 0) {
if (this._running === 0 && !this.onStop) {
// Run one more time after the loop ends
const result = await this.loopRun();
}
await this.onStop?.();
debug('Monitor for user %s finished', this.data.nsoAccount.user.name);
} finally {
this._running = 0;
@ -449,11 +451,13 @@ export class EmbeddedProxyPresenceMonitor extends ZncProxyDiscordPresence {
await this.loop();
}
if (this._running === 0) {
if (this._running === 0 && !this.onStop) {
// Run one more time after the loop ends
const result = await this.loopRun();
}
await this.onStop?.();
debug('Monitor for presence URL %s finished', this.presence_url);
} finally {
this._running = 0;

View File

@ -304,6 +304,12 @@ class ZncDiscordPresenceClient {
// @ts-expect-error
client.transport.on('close', reconnect);
if (this.rpc) {
// Another client connected first
client!.destroy();
return (this.rpc as {client: DiscordRpcClient}).client;
}
this.rpc = {client: client!, id: client_id};
this.onUpdateClient?.call(null, client!);
if (this.last_activity) this.rpc.client.setActivity(this.last_activity.activity);
@ -450,6 +456,10 @@ export class ZncDiscordPresence extends ZncNotifications {
if (user) await this.updatePresenceForSplatNet2Monitors([user]);
}
async onStop() {
await this.discord.setActivity(null);
}
saved_presence = new Map<string, number>();
async savePresenceForTitleUpdateAt(id: string, presence: Presence, title_since = Date.now()) {
@ -532,6 +542,10 @@ export class ZncProxyDiscordPresence extends Loop {
await this.updatePresenceForSplatNet2Monitor(presence, this.presence_url);
}
async onStop() {
await this.discord.setActivity(null);
}
async updatePresenceForSplatNet2Monitors(friends: (CurrentUser | Friend)[]) {
for (const friend of friends) {
await this.updatePresenceForSplatNet2Monitor(friend.presence, friend.nsaId, friend.name);