2019-11-01 17:10:59 +00:00
|
|
|
import { commands, ExtensionContext, StatusBarAlignment, StatusBarItem, window, workspace, extensions } from 'vscode';
|
2018-11-10 16:39:01 +00:00
|
|
|
import RPCClient from './client/RPCClient';
|
|
|
|
import Logger from './structures/Logger';
|
2019-11-01 16:53:57 +00:00
|
|
|
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;
|
2019-08-03 12:16:17 +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
|
|
|
|
2020-07-14 09:37:01 +00:00
|
|
|
const clientId = '383226320970055681';
|
2018-11-07 02:53:18 +00:00
|
|
|
const config = workspace.getConfiguration('discord');
|
2020-07-14 09:37:01 +00:00
|
|
|
register(clientId);
|
|
|
|
const rpc = new RPCClient(clientId, statusBarIcon);
|
2018-11-07 02:53:18 +00:00
|
|
|
|
2019-09-13 11:18:12 +00:00
|
|
|
export async function activate(context: ExtensionContext) {
|
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');
|
2019-11-01 17:10:59 +00:00
|
|
|
if (excludePatterns?.length) {
|
2019-04-19 11:00:44 +00:00
|
|
|
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))) {
|
2019-04-19 11:00:44 +00:00
|
|
|
isWorkspaceExcluded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:25:05 +00:00
|
|
|
const enabler = commands.registerCommand('discord.enable', () => {
|
|
|
|
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');
|
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();
|
2020-07-16 16:25:05 +00:00
|
|
|
void rpc.login();
|
2020-05-23 03:34:15 +00:00
|
|
|
void window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
|
2017-11-24 03:42:51 +00:00
|
|
|
});
|
2020-07-16 16:25:05 +00:00
|
|
|
const disabler = commands.registerCommand('discord.disable', () => {
|
2020-05-23 03:34:15 +00:00
|
|
|
void config.update('enabled', false);
|
2018-11-10 16:39:01 +00:00
|
|
|
rpc.config = workspace.getConfiguration('discord');
|
2020-07-16 16:25:05 +00:00
|
|
|
rpc.dispose();
|
2018-11-20 20:32:26 +00:00
|
|
|
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
|
|
|
});
|
2020-07-16 16:25:05 +00:00
|
|
|
const reconnecter = commands.registerCommand('discord.reconnect', () => {
|
2019-08-03 12:16:17 +00:00
|
|
|
if (loginTimeout) clearTimeout(loginTimeout);
|
2020-07-16 16:25:05 +00:00
|
|
|
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...');
|
2019-08-03 12:16:17 +00:00
|
|
|
rpc.statusBarIcon.text = '$(pulse) Reconnecting to Discord...';
|
|
|
|
rpc.statusBarIcon.command = 'discord.reconnect';
|
|
|
|
}, 1000);
|
|
|
|
});
|
2020-07-16 16:25:05 +00:00
|
|
|
const disconnect = commands.registerCommand('discord.disconnect', () => {
|
|
|
|
rpc.dispose();
|
2019-08-03 12:16:17 +00:00
|
|
|
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
|
|
|
rpc.statusBarIcon.command = 'discord.reconnect';
|
2018-01-05 02:17:18 +00:00
|
|
|
});
|
2020-07-16 16:25:05 +00:00
|
|
|
const allowSpectate = commands.registerCommand('discord.allowSpectate', () => rpc.allowSpectate());
|
|
|
|
const disableSpectate = commands.registerCommand('discord.disableSpectate', () => rpc.disableSpectate());
|
|
|
|
const allowJoinRequests = commands.registerCommand('discord.allowJoinRequests', () => rpc.allowJoinRequests());
|
|
|
|
const disableJoinRequests = commands.registerCommand('discord.disableJoinRequests', () => rpc.disableJoinRequests());
|
2018-12-01 04:18:35 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
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}`);
|
2020-07-16 16:25:05 +00:00
|
|
|
rpc.dispose();
|
2019-11-02 16:55:43 +00:00
|
|
|
if (!config.get('silent')) {
|
2020-07-02 21:22:44 +00:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
2020-07-16 16:25:05 +00:00
|
|
|
|
|
|
|
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
|
|
|
|
if (gitExtension) {
|
|
|
|
if (gitExtension.isActive) {
|
|
|
|
rpc.git = gitExtension.exports.getAPI(1);
|
|
|
|
} else {
|
|
|
|
const extension = await gitExtension.activate();
|
|
|
|
rpc.git = extension.getAPI(1);
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:25:05 +00:00
|
|
|
export function deactivate() {
|
|
|
|
rpc.dispose();
|
2018-04-30 09:28:20 +00:00
|
|
|
}
|
2018-11-02 14:33:14 +00:00
|
|
|
|
2020-03-21 20:49:43 +00:00
|
|
|
process.on('unhandledRejection', (err) => Logger.log(err as string));
|