2021-02-10 03:24:42 +00:00
|
|
|
const { Client } = require('discord-rpc'); // eslint-disable-line
|
|
|
|
import {
|
|
|
|
commands,
|
|
|
|
ExtensionContext,
|
|
|
|
StatusBarAlignment,
|
|
|
|
StatusBarItem,
|
|
|
|
window,
|
|
|
|
workspace,
|
|
|
|
extensions,
|
|
|
|
debug,
|
|
|
|
} from 'vscode';
|
|
|
|
import { activity } from './activity';
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2021-02-10 03:24:42 +00:00
|
|
|
import { CLIENT_ID, CONFIG_KEYS } from './constants';
|
|
|
|
import { GitExtension } from './git';
|
|
|
|
import { log, LogLevel } from './logger';
|
|
|
|
import { getConfig } from './util';
|
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
|
|
|
|
2021-02-10 03:24:42 +00:00
|
|
|
const rpc = new Client({ transport: 'ipc' });
|
|
|
|
const config = getConfig();
|
|
|
|
|
|
|
|
async function sendActivity() {
|
|
|
|
rpc.setActivity(await activity());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function login(context: ExtensionContext) {
|
|
|
|
rpc.once('ready', () => {
|
|
|
|
log(LogLevel.Info, 'Successfully connected to Discord');
|
|
|
|
|
|
|
|
statusBarIcon.text = '$(globe) Connected to Discord';
|
|
|
|
statusBarIcon.tooltip = 'Connected to Discord';
|
|
|
|
|
|
|
|
void sendActivity();
|
|
|
|
const onChangeActiveTextEditor = window.onDidChangeActiveTextEditor(() => sendActivity());
|
|
|
|
const onChangeTextDocument = workspace.onDidChangeTextDocument(() => sendActivity());
|
|
|
|
const onStartDebugSession = debug.onDidStartDebugSession(() => sendActivity());
|
|
|
|
const onTerminateDebugSession = debug.onDidTerminateDebugSession(() => sendActivity());
|
|
|
|
|
|
|
|
context.subscriptions.push(
|
|
|
|
onChangeActiveTextEditor,
|
|
|
|
onChangeTextDocument,
|
|
|
|
onStartDebugSession,
|
|
|
|
onTerminateDebugSession,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await rpc.login({ clientId: CLIENT_ID });
|
|
|
|
} catch (error) {
|
|
|
|
log(LogLevel.Error, `Encountered following error while trying to login:\n${error as string}`);
|
|
|
|
rpc.dispose();
|
|
|
|
if (!config[CONFIG_KEYS.SuppressNotifications]) {
|
|
|
|
if (error?.message?.includes('ENOENT')) void window.showErrorMessage('No Discord client detected');
|
|
|
|
else void window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error as string}`);
|
|
|
|
}
|
|
|
|
rpc.statusBarIcon.text = '$(pulse) Reconnect to Discord';
|
|
|
|
rpc.statusBarIcon.command = 'discord.reconnect';
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 02:53:18 +00:00
|
|
|
|
2021-02-10 03:24:42 +00:00
|
|
|
export function activate(context: ExtensionContext) {
|
|
|
|
log(LogLevel.Info, 'Discord Presence activated');
|
2018-11-07 02:53:18 +00:00
|
|
|
|
2019-04-19 11:00:44 +00:00
|
|
|
let isWorkspaceExcluded = false;
|
2021-02-10 03:24:42 +00:00
|
|
|
for (const pattern of config[CONFIG_KEYS.WorkspaceExcludePatterns]) {
|
|
|
|
const regex = new RegExp(pattern);
|
|
|
|
const folders = workspace.workspaceFolders;
|
|
|
|
if (!folders) break;
|
|
|
|
if (folders.some((folder) => regex.test(folder.uri.fsPath))) {
|
|
|
|
isWorkspaceExcluded = true;
|
|
|
|
break;
|
2019-04-19 11:00:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:25:05 +00:00
|
|
|
const enabler = commands.registerCommand('discord.enable', () => {
|
2021-02-10 03:24:42 +00:00
|
|
|
rpc.destroy();
|
2020-05-23 03:34:15 +00:00
|
|
|
void config.update('enabled', true);
|
2021-02-10 03:24:42 +00:00
|
|
|
statusBarIcon.text = '$(pulse) Connecting to Discord...';
|
|
|
|
statusBarIcon.show();
|
|
|
|
void login(context);
|
|
|
|
void window.showInformationMessage('Enabled Discord Presence for this workspace');
|
2017-11-24 03:42:51 +00:00
|
|
|
});
|
2021-02-10 03:24:42 +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);
|
2021-02-10 03:24:42 +00:00
|
|
|
rpc.destroy();
|
2018-11-20 20:32:26 +00:00
|
|
|
rpc.statusBarIcon.hide();
|
2021-02-10 03:24:42 +00:00
|
|
|
void window.showInformationMessage('Disabled Discord Presence for this workspace');
|
2017-11-23 23:53:16 +00:00
|
|
|
});
|
2021-02-10 03:24:42 +00:00
|
|
|
|
2020-07-16 16:25:05 +00:00
|
|
|
const reconnecter = commands.registerCommand('discord.reconnect', () => {
|
2021-02-10 03:24:42 +00:00
|
|
|
deactivate();
|
|
|
|
void activate(context);
|
2019-08-03 12:16:17 +00:00
|
|
|
});
|
2021-02-10 03:24:42 +00:00
|
|
|
|
2020-07-16 16:25:05 +00:00
|
|
|
const disconnect = commands.registerCommand('discord.disconnect', () => {
|
2021-02-10 03:24:42 +00:00
|
|
|
rpc.destroy();
|
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
|
|
|
});
|
2021-02-10 03:24:42 +00:00
|
|
|
|
|
|
|
context.subscriptions.push(enabler, disabler, reconnecter, disconnect);
|
|
|
|
|
|
|
|
if (!isWorkspaceExcluded && config[CONFIG_KEYS.Enabled]) {
|
2019-11-02 16:55:43 +00:00
|
|
|
statusBarIcon.show();
|
2021-02-10 03:24:42 +00:00
|
|
|
void login(context);
|
2019-11-02 16:55:43 +00:00
|
|
|
}
|
2020-07-16 16:25:05 +00:00
|
|
|
|
|
|
|
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
|
2021-02-10 03:24:42 +00:00
|
|
|
void gitExtension?.activate();
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:25:05 +00:00
|
|
|
export function deactivate() {
|
2021-02-10 03:24:42 +00:00
|
|
|
rpc.destroy();
|
2018-04-30 09:28:20 +00:00
|
|
|
}
|