diff --git a/src/app/main/monitor.ts b/src/app/main/monitor.ts index dd2f517..f6b643e 100644 --- a/src/app/main/monitor.ts +++ b/src/app/main/monitor.ts @@ -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; diff --git a/src/common/presence.ts b/src/common/presence.ts index 6ed0468..f9fdf41 100644 --- a/src/common/presence.ts +++ b/src/common/presence.ts @@ -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(); 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);