2018-11-07 02:53:18 +00:00
|
|
|
import RPCClient from './client/RPCClient';
|
|
|
|
import Logger from './structures/Logger';
|
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
|
|
|
StatusBarItem,
|
|
|
|
StatusBarAlignment,
|
2017-11-27 01:14:12 +00:00
|
|
|
window,
|
2018-11-07 02:53:18 +00:00
|
|
|
workspace
|
2017-11-24 02:16:08 +00:00
|
|
|
} from 'vscode';
|
2018-11-07 02:53:18 +00:00
|
|
|
import { setInterval, clearInterval } from 'timers';
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-26 15:30:32 +00:00
|
|
|
let activityTimer: NodeJS.Timer;
|
2018-01-05 02:17:18 +00:00
|
|
|
let statusBarIcon: StatusBarItem;
|
2017-11-23 21:14:16 +00:00
|
|
|
|
2018-11-07 02:53:18 +00:00
|
|
|
const config = workspace.getConfiguration('discord');
|
|
|
|
const rpc = new RPCClient(config.get<string>('clientID')!);
|
|
|
|
|
|
|
|
export async function activate(context: ExtensionContext) {
|
|
|
|
Logger.log('Discord Presence activated!');
|
|
|
|
|
|
|
|
rpc.client.once('ready', () => {
|
|
|
|
Logger.log('Successfully connected to Discord.');
|
|
|
|
if (!config.get<boolean>('silent')) window.showInformationMessage('Successfully reconnected to Discord RPC');
|
|
|
|
|
|
|
|
if (statusBarIcon) statusBarIcon.dispose();
|
|
|
|
if (activityTimer) clearInterval(activityTimer);
|
|
|
|
rpc.setActivity();
|
|
|
|
|
|
|
|
rpc.client.transport.once('close', async () => {
|
|
|
|
if (!config.get<boolean>('enabled')) return;
|
|
|
|
await rpc.dispose();
|
|
|
|
await rpc.login();
|
|
|
|
if (!statusBarIcon) {
|
|
|
|
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
|
|
|
|
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
|
|
|
statusBarIcon.command = 'discord.reconnect';
|
|
|
|
statusBarIcon.show();
|
|
|
|
}
|
|
|
|
});
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2018-11-07 02:53:18 +00:00
|
|
|
activityTimer = setInterval(() => {
|
|
|
|
rpc.setActivity(config.get<boolean>('workspaceElapsedTime'));
|
|
|
|
}, 10000);
|
|
|
|
})
|
|
|
|
|
|
|
|
if (config.get<boolean>('enabled')) {
|
|
|
|
try {
|
|
|
|
await rpc.login();
|
|
|
|
} catch (error) {
|
|
|
|
Logger.log(`Encountered following error after trying to login:\n${error}`);
|
|
|
|
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
|
|
|
}
|
|
|
|
if (!statusBarIcon) {
|
|
|
|
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
|
|
|
|
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
|
|
|
statusBarIcon.command = 'discord.reconnect';
|
|
|
|
statusBarIcon.show();
|
2018-11-07 02:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-24 02:16:08 +00:00
|
|
|
|
2017-12-20 05:25:37 +00:00
|
|
|
const enabler = commands.registerCommand('discord.enable', async () => {
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
2018-02-04 16:13:19 +00:00
|
|
|
await config.update('enabled', true);
|
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.');
|
|
|
|
});
|
|
|
|
|
2017-12-20 05:25:37 +00:00
|
|
|
const disabler = commands.registerCommand('discord.disable', async () => {
|
2018-02-04 16:13:19 +00:00
|
|
|
await config.update('enabled', false);
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
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
|
|
|
});
|
|
|
|
|
2018-01-05 02:17:18 +00:00
|
|
|
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
|
2018-11-07 02:53:18 +00:00
|
|
|
await rpc.dispose();
|
|
|
|
await rpc.login();
|
|
|
|
if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...');
|
|
|
|
if (statusBarIcon) statusBarIcon.text = '$(pulse) reconnecting...';
|
2018-01-05 02:17:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
context.subscriptions.push(enabler, disabler, reconnecter);
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 05:25:37 +00:00
|
|
|
export async function deactivate() {
|
2018-11-07 02:53:18 +00:00
|
|
|
clearInterval(activityTimer);
|
|
|
|
await rpc.dispose();
|
2018-04-30 09:28:20 +00:00
|
|
|
}
|
2018-11-02 14:33:14 +00:00
|
|
|
|
2018-11-07 03:02:31 +00:00
|
|
|
process.on('unhandledRejection', err => Logger.log(err));
|