import { commands, ExtensionContext, StatusBarAlignment, StatusBarItem, window, workspace } from 'vscode'; import RPCClient from './client/RPCClient'; import Logger from './structures/Logger'; const { register } = require('discord-rpc'); // eslint-disable-line const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left); statusBarIcon.text = '$(pulse) Connecting to Discord...'; const config = workspace.getConfiguration('discord'); register(config.get('clientID')); const rpc = new RPCClient(config.get('clientID')!, statusBarIcon); export async function activate(context: ExtensionContext): Promise { Logger.log('Discord Presence activated!'); let isWorkspaceExcluded = false; const excludePatterns = config.get('workspaceExcludePatterns'); if (excludePatterns && excludePatterns.length > 0) { for (const pattern of excludePatterns) { const regex = new RegExp(pattern); const folders = workspace.workspaceFolders; if (!folders) break; if (folders.some((folder): boolean => regex.test(folder.uri.fsPath))) { isWorkspaceExcluded = true; break; } } } if (!isWorkspaceExcluded && config.get('enabled')) { statusBarIcon.show(); try { 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()}`); } rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord'; rpc.statusBarIcon.command = 'discord.reconnect'; } } const enabler = commands.registerCommand('discord.enable', async (): Promise => { await rpc.dispose(); 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.'); }); const disabler = commands.registerCommand('discord.disable', async (): Promise => { config.update('enabled', false); rpc.config = workspace.getConfiguration('discord'); await rpc.dispose(); rpc.statusBarIcon.hide(); window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); }); const reconnecter = commands.registerCommand('discord.reconnect', async (): Promise => { await rpc.dispose(); await rpc.login(); if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...'); rpc.statusBarIcon.text = '$(pulse) Reconnecting to Discord...'; rpc.statusBarIcon.command = undefined; }); const allowSpectate = commands.registerCommand('discord.allowSpectate', async (): Promise => { await rpc.allowSpectate(); }); const disableSpectate = commands.registerCommand('discord.disableSpectate', async (): Promise => { await rpc.disableSpectate(); }); const allowJoinRequests = commands.registerCommand('discord.allowJoinRequests', async (): Promise => { await rpc.allowJoinRequests(); }); const disableJoinRequests = commands.registerCommand('discord.disableJoinRequests', async (): Promise => { await rpc.disableJoinRequests(); }); context.subscriptions.push(enabler, disabler, reconnecter, allowSpectate, disableSpectate, allowJoinRequests, disableJoinRequests); } export async function deactivate(): Promise { await rpc.dispose(); } process.on('unhandledRejection', (err): void => Logger.log(err as string));