discord-vscode/src/extension.ts

127 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-11-01 17:10:59 +00:00
import { commands, ExtensionContext, StatusBarAlignment, StatusBarItem, window, workspace, extensions } from 'vscode';
import RPCClient from './client/RPCClient';
import Logger from './structures/Logger';
import { GitExtension } from './git';
2019-11-02 16:55:43 +00:00
const { register } = require('discord-rpc'); // eslint-disable-line
2017-11-23 13:06:21 +00:00
2020-05-23 03:34:15 +00:00
let loginTimeout: NodeJS.Timer | undefined;
const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
statusBarIcon.text = '$(pulse) Connecting to Discord...';
const clientId = '383226320970055681';
const config = workspace.getConfiguration('discord');
register(clientId);
const rpc = new RPCClient(clientId, statusBarIcon);
2019-09-13 11:18:12 +00:00
export async function activate(context: ExtensionContext) {
Logger.log('Discord Presence activated!');
let isWorkspaceExcluded = false;
const excludePatterns = config.get<string[]>('workspaceExcludePatterns');
2019-11-01 17:10:59 +00:00
if (excludePatterns?.length) {
for (const pattern of excludePatterns) {
const regex = new RegExp(pattern);
const folders = workspace.workspaceFolders;
if (!folders) break;
2020-03-21 20:49:43 +00:00
if (folders.some((folder) => regex.test(folder.uri.fsPath))) {
isWorkspaceExcluded = true;
break;
}
}
}
const enabler = commands.registerCommand('discord.enable', async () => {
await rpc.dispose();
2020-05-23 03:34:15 +00:00
void config.update('enabled', true);
2018-11-10 16:42:22 +00:00
rpc.config = workspace.getConfiguration('discord');
rpc.statusBarIcon.text = '$(pulse) Connecting to Discord...';
rpc.statusBarIcon.show();
await rpc.login();
2020-05-23 03:34:15 +00:00
void window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
});
const disabler = commands.registerCommand('discord.disable', async () => {
2020-05-23 03:34:15 +00:00
void config.update('enabled', false);
rpc.config = workspace.getConfiguration('discord');
await rpc.dispose();
rpc.statusBarIcon.hide();
2020-05-23 03:34:15 +00:00
void window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
2017-11-23 23:53:16 +00:00
});
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
if (loginTimeout) clearTimeout(loginTimeout);
await rpc.dispose();
2020-05-23 03:34:15 +00:00
loginTimeout = setTimeout(() => {
void rpc.login();
if (!config.get('silent')) void window.showInformationMessage('Reconnecting to Discord RPC...');
rpc.statusBarIcon.text = '$(pulse) Reconnecting to Discord...';
rpc.statusBarIcon.command = 'discord.reconnect';
}, 1000);
});
const disconnect = commands.registerCommand('discord.disconnect', async () => {
await rpc.dispose();
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
rpc.statusBarIcon.command = 'discord.reconnect';
});
const allowSpectate = commands.registerCommand('discord.allowSpectate', async () => {
await rpc.allowSpectate();
});
const disableSpectate = commands.registerCommand('discord.disableSpectate', async () => {
await rpc.disableSpectate();
});
const allowJoinRequests = commands.registerCommand('discord.allowJoinRequests', async () => {
await rpc.allowJoinRequests();
});
const disableJoinRequests = commands.registerCommand('discord.disableJoinRequests', async () => {
await rpc.disableJoinRequests();
});
2019-09-22 16:56:19 +00:00
context.subscriptions.push(
enabler,
disabler,
reconnecter,
disconnect,
allowSpectate,
disableSpectate,
allowJoinRequests,
disableJoinRequests,
);
2019-11-02 16:55:43 +00:00
2019-11-03 03:40:53 +00:00
setTimeout(() => {
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
if (gitExtension) {
if (gitExtension.isActive) {
rpc.git = gitExtension.exports.getAPI(1);
}
2019-11-03 03:17:58 +00:00
}
2019-11-03 03:40:53 +00:00
}, 5000);
2019-11-03 03:17:58 +00:00
2019-11-02 16:55:43 +00:00
if (!isWorkspaceExcluded && config.get<boolean>('enabled')) {
statusBarIcon.show();
try {
await rpc.login();
} catch (error) {
2020-05-23 03:34:15 +00:00
Logger.log(`Encountered following error after trying to login:\n${error as string}`);
2019-11-02 16:55:43 +00:00
await rpc.dispose();
if (!config.get('silent')) {
if (error?.message?.includes('ENOENT')) void window.showErrorMessage('No Discord Client detected!');
2020-05-23 03:34:15 +00:00
else void window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error as string}`);
2019-11-02 16:55:43 +00:00
}
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
rpc.statusBarIcon.command = 'discord.reconnect';
}
}
2017-11-23 13:06:21 +00:00
}
2019-09-13 11:18:12 +00:00
export async function deactivate() {
await rpc.dispose();
}
2020-03-21 20:49:43 +00:00
process.on('unhandledRejection', (err) => Logger.log(err as string));