diff --git a/src/client/RPCClient.ts b/src/client/RPCClient.ts index 62ad7cd..94799ab 100644 --- a/src/client/RPCClient.ts +++ b/src/client/RPCClient.ts @@ -44,35 +44,37 @@ export default class RPCClient implements Disposable { this._rpc.once('ready', () => { Logger.log('Successfully connected to Discord.'); this.statusBarIcon.text = '$(globe) Connected to Discord'; - this.statusBarIcon.tooltip = 'Connected to Discord'; - setTimeout(() => this.statusBarIcon.text = '$(globe)', 5000); + setTimeout(() => { + this.statusBarIcon.text = '$(globe)'; + this.statusBarIcon.tooltip = 'Connected to Discord'; + }, 10000); if (activityTimer) clearInterval(activityTimer); this.setActivity(); - this._rpc.transport.once('close', async () => { - if (!this.config.get<boolean>('enabled')) return; - await this.dispose(); - this.statusBarIcon.text = '$(plug) Reconnect to Discord'; - this.statusBarIcon.command = 'discord.reconnect'; - }); - activityTimer = setInterval(() => { this.config = workspace.getConfiguration('discord'); this.setActivity(this.config.get<boolean>('workspaceElapsedTime')); }, 10000); }); + + this._rpc.transport.once('close', async () => { + if (!this.config.get<boolean>('enabled')) return; + await this.dispose(); + this.statusBarIcon.text = '$(plug) Reconnect to Discord'; + this.statusBarIcon.command = 'discord.reconnect'; + }); await this._rpc.login({ clientId: this._clientId }); } public async dispose() { this._activity.dispose(); - if (this._rpc) { + try { await this._rpc.destroy(); - this._rpc = null; - } + } catch {} + this._rpc = null; + clearInterval(activityTimer); - this.statusBarIcon.hide(); } } diff --git a/src/extension.ts b/src/extension.ts index 5cd3e69..3642a2a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -25,6 +25,7 @@ export async function activate(context: ExtensionContext) { await rpc.login(); } catch (error) { Logger.log(`Encountered following error after trying to login:\n${error}`); + await rpc.dispose(); if (!config.get('silent')) { if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!'); else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.toString()}`); @@ -39,6 +40,7 @@ export async function activate(context: ExtensionContext) { config.update('enabled', true); rpc.config = workspace.getConfiguration('discord'); rpc.statusBarIcon.text = '$(pulse) Connecting to Discord...'; + rpc.statusBarIcon.show(); await rpc.login(); window.showInformationMessage('Enabled Discord Rich Presence for this workspace.'); }); @@ -47,6 +49,7 @@ export async function activate(context: ExtensionContext) { config.update('enabled', false); rpc.config = workspace.getConfiguration('discord'); await rpc.dispose(); + rpc.statusBarIcon.hide(); window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); });