2017-11-24 02:16:08 +00:00
|
|
|
import {
|
2017-11-27 01:14:12 +00:00
|
|
|
commands,
|
2017-11-24 02:16:08 +00:00
|
|
|
ExtensionContext,
|
2018-01-05 02:17:18 +00:00
|
|
|
StatusBarAlignment,
|
2018-11-10 16:39:01 +00:00
|
|
|
StatusBarItem,
|
2017-11-27 01:14:12 +00:00
|
|
|
window,
|
2018-11-07 02:53:18 +00:00
|
|
|
workspace
|
2019-04-21 18:30:00 +00:00
|
|
|
} from 'vscode';
|
2018-11-10 16:39:01 +00:00
|
|
|
import RPCClient from './client/RPCClient';
|
|
|
|
import Logger from './structures/Logger';
|
2019-04-21 18:30:00 +00:00
|
|
|
const { register } = require('discord-rpc'); // eslint-disable-line
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2018-11-10 16:39:01 +00:00
|
|
|
const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
|
2018-11-20 04:26:48 +00:00
|
|
|
statusBarIcon.text = '$(pulse) Connecting to Discord...';
|
2017-11-23 21:14:16 +00:00
|
|
|
|
2018-11-07 02:53:18 +00:00
|
|
|
const config = workspace.getConfiguration('discord');
|
2018-12-01 04:18:35 +00:00
|
|
|
register(config.get<string>('clientID'));
|
2018-11-10 16:39:01 +00:00
|
|
|
const rpc = new RPCClient(config.get<string>('clientID')!, statusBarIcon);
|
2018-11-07 02:53:18 +00:00
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
export async function activate(context: ExtensionContext): Promise<void> {
|
2018-11-07 02:53:18 +00:00
|
|
|
Logger.log('Discord Presence activated!');
|
|
|
|
|
2019-04-19 11:00:44 +00:00
|
|
|
let isWorkspaceExcluded = false;
|
|
|
|
const excludePatterns = config.get<string[]>('workspaceExcludePatterns');
|
|
|
|
if (excludePatterns && excludePatterns.length > 0) {
|
|
|
|
for (const pattern of excludePatterns) {
|
|
|
|
const regex = new RegExp(pattern);
|
|
|
|
const folders = workspace.workspaceFolders;
|
|
|
|
if (!folders) break;
|
2019-04-21 18:30:00 +00:00
|
|
|
if (folders.some((folder): boolean => regex.test(folder.uri.fsPath))) {
|
2019-04-19 11:00:44 +00:00
|
|
|
isWorkspaceExcluded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isWorkspaceExcluded && config.get<boolean>('enabled')) {
|
2018-11-10 16:39:01 +00:00
|
|
|
statusBarIcon.show();
|
2018-11-07 02:53:18 +00:00
|
|
|
try {
|
|
|
|
await rpc.login();
|
|
|
|
} catch (error) {
|
|
|
|
Logger.log(`Encountered following error after trying to login:\n${error}`);
|
2018-11-20 20:32:26 +00:00
|
|
|
await rpc.dispose();
|
2018-11-07 02:53:18 +00:00
|
|
|
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()}`);
|
2018-11-07 03:02:31 +00:00
|
|
|
}
|
2018-11-20 04:26:48 +00:00
|
|
|
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
2018-11-10 16:39:01 +00:00
|
|
|
rpc.statusBarIcon.command = 'discord.reconnect';
|
2018-11-07 02:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-24 02:16:08 +00:00
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const enabler = commands.registerCommand('discord.enable', async (): Promise<void> => {
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
2018-11-13 02:40:24 +00:00
|
|
|
config.update('enabled', true);
|
2018-11-10 16:42:22 +00:00
|
|
|
rpc.config = workspace.getConfiguration('discord');
|
2018-11-20 04:26:48 +00:00
|
|
|
rpc.statusBarIcon.text = '$(pulse) Connecting to Discord...';
|
2018-11-20 20:32:26 +00:00
|
|
|
rpc.statusBarIcon.show();
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.login();
|
2017-11-24 03:42:51 +00:00
|
|
|
window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
|
|
|
|
});
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const disabler = commands.registerCommand('discord.disable', async (): Promise<void> => {
|
2018-11-13 02:40:24 +00:00
|
|
|
config.update('enabled', false);
|
2018-11-10 16:39:01 +00:00
|
|
|
rpc.config = workspace.getConfiguration('discord');
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
2018-11-20 20:32:26 +00:00
|
|
|
rpc.statusBarIcon.hide();
|
2017-11-24 03:42:51 +00:00
|
|
|
window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
|
2017-11-23 23:53:16 +00:00
|
|
|
});
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const reconnecter = commands.registerCommand('discord.reconnect', async (): Promise<void> => {
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
|
|
|
await rpc.login();
|
|
|
|
if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...');
|
2018-11-20 04:26:48 +00:00
|
|
|
rpc.statusBarIcon.text = '$(pulse) Reconnecting to Discord...';
|
2018-11-10 16:39:01 +00:00
|
|
|
rpc.statusBarIcon.command = undefined;
|
2018-01-05 02:17:18 +00:00
|
|
|
});
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const allowSpectate = commands.registerCommand('discord.allowSpectate', async (): Promise<void> => {
|
2018-12-01 04:18:35 +00:00
|
|
|
await rpc.allowSpectate();
|
|
|
|
});
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const disableSpectate = commands.registerCommand('discord.disableSpectate', async (): Promise<void> => {
|
2018-12-01 04:18:35 +00:00
|
|
|
await rpc.disableSpectate();
|
|
|
|
});
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const allowJoinRequests = commands.registerCommand('discord.allowJoinRequests', async (): Promise<void> => {
|
2018-12-01 04:18:35 +00:00
|
|
|
await rpc.allowJoinRequests();
|
|
|
|
});
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
const disableJoinRequests = commands.registerCommand('discord.disableJoinRequests', async (): Promise<void> => {
|
2018-12-01 04:18:35 +00:00
|
|
|
await rpc.disableJoinRequests();
|
|
|
|
});
|
|
|
|
|
|
|
|
context.subscriptions.push(enabler, disabler, reconnecter, allowSpectate, disableSpectate, allowJoinRequests, disableJoinRequests);
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
export async function deactivate(): Promise<void> {
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
2018-04-30 09:28:20 +00:00
|
|
|
}
|
2018-11-02 14:33:14 +00:00
|
|
|
|
2019-04-21 18:30:00 +00:00
|
|
|
process.on('unhandledRejection', (err): void => Logger.log(err as string));
|