2017-11-24 00:22:23 +00:00
|
|
|
// Import the required functions & object types from various packages.
|
2017-11-23 13:06:21 +00:00
|
|
|
import { Client } from 'discord-rpc';
|
|
|
|
import { basename, extname } from 'path';
|
2017-11-23 23:53:16 +00:00
|
|
|
import { setInterval, clearInterval } from 'timers';
|
2017-11-24 02:16:08 +00:00
|
|
|
import {
|
|
|
|
ExtensionContext,
|
|
|
|
commands,
|
|
|
|
window,
|
|
|
|
workspace,
|
|
|
|
TextDocumentChangeEvent,
|
|
|
|
Disposable
|
|
|
|
} from 'vscode';
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Define the RPC variable and its type.
|
2017-11-23 21:14:16 +00:00
|
|
|
let rpc: Client;
|
2017-11-24 02:16:08 +00:00
|
|
|
// Define the eventHandler variable and its type.
|
|
|
|
let eventHandler: Disposable;
|
|
|
|
// Define the config variable and its type.
|
|
|
|
let config;
|
|
|
|
// Define the reconnect timer and its type.
|
|
|
|
let reconnect: NodeJS.Timer;
|
2017-11-24 02:45:23 +00:00
|
|
|
// Define the reconnect counter and its type.
|
|
|
|
let reconnectCounter = 0;
|
2017-11-24 03:42:51 +00:00
|
|
|
// Define the last known file name and its type.
|
2017-11-24 03:31:18 +00:00
|
|
|
let lastKnownFileName: string;
|
2017-11-23 21:14:16 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// `Activate` is fired when the extension is enabled. This SHOULD only fire once.
|
2017-11-23 13:06:21 +00:00
|
|
|
export function activate(context: ExtensionContext) {
|
2017-11-24 00:22:23 +00:00
|
|
|
// Get the workspace's configuration for "discord".
|
2017-11-24 02:16:08 +00:00
|
|
|
config = workspace.getConfiguration('discord');
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Obtain whether or not the extension is activated.
|
2017-11-23 23:53:16 +00:00
|
|
|
if (config.get('enabled')) {
|
|
|
|
initRPC(config.get('clientID'));
|
|
|
|
}
|
2017-11-24 02:16:08 +00:00
|
|
|
|
2017-11-24 03:42:51 +00:00
|
|
|
// Register the `discord.enable` command, and set the `enabled` config option to true.
|
|
|
|
const enabler = commands.registerCommand('discord.enable', () => {
|
|
|
|
if (rpc) destroyRPC();
|
|
|
|
config.update('enabled', true);
|
|
|
|
initRPC(config.get('clientID'));
|
|
|
|
window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register the `discord.disable` command, and set the `enabled` config option to false.
|
|
|
|
const disabler = commands.registerCommand('discord.disable', () => {
|
|
|
|
if (!rpc) return;
|
|
|
|
config.update('enabled', false);
|
|
|
|
rpc.setActivity({});
|
|
|
|
destroyRPC();
|
|
|
|
window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
|
2017-11-23 23:53:16 +00:00
|
|
|
});
|
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Push the new commands into the subscriptions.
|
2017-11-24 03:42:51 +00:00
|
|
|
context.subscriptions.push(enabler, disabler);
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// `Deactivate` is fired whenever the extension is deactivated.
|
2017-11-23 21:14:16 +00:00
|
|
|
export function deactivate(context: ExtensionContext) {
|
2017-11-24 00:22:23 +00:00
|
|
|
// If there's an RPC Client initalized, destroy it.
|
2017-11-24 02:16:08 +00:00
|
|
|
destroyRPC();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initalize the RPC systems.
|
|
|
|
function initRPC(clientID: string): void {
|
|
|
|
// Update the RPC variable with a new RPC Client.
|
|
|
|
rpc = new Client({ transport: 'ipc' });
|
|
|
|
|
|
|
|
// Once the RPC Client is ready, set the activity.
|
|
|
|
rpc.once('ready', () => {
|
|
|
|
if (reconnect) {
|
|
|
|
// Clear the reconnect interval.
|
|
|
|
clearInterval(reconnect);
|
|
|
|
// Null reconnect variable.
|
|
|
|
reconnect = null;
|
|
|
|
}
|
2017-11-24 03:42:51 +00:00
|
|
|
// Reset the reconnect counter to 0 on a successful reconnect.
|
2017-11-24 02:45:23 +00:00
|
|
|
reconnectCounter = 0;
|
2017-11-24 02:16:08 +00:00
|
|
|
setActivity();
|
|
|
|
eventHandler = workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
|
|
|
|
// Make sure to listen to the close event and dispose and destroy everything accordingly.
|
|
|
|
rpc.transport.once('close', () => {
|
2017-11-24 03:17:12 +00:00
|
|
|
if (!config.get('enabled')) return;
|
2017-11-24 02:16:08 +00:00
|
|
|
destroyRPC();
|
|
|
|
// Set an interval for reconnecting.
|
2017-11-24 02:45:23 +00:00
|
|
|
reconnect = setInterval(() => {
|
|
|
|
reconnectCounter++;
|
|
|
|
initRPC(config.get('clientID'));
|
|
|
|
}, 5000);
|
2017-11-24 02:16:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Log in to the RPC Client, and check whether or not it errors.
|
2017-11-24 02:31:37 +00:00
|
|
|
rpc.login(clientID).catch(error => {
|
2017-11-24 02:45:23 +00:00
|
|
|
if (reconnect) {
|
2017-11-24 12:53:47 +00:00
|
|
|
// Destroy and dispose of everything after a default of 20 reconnect attempts
|
|
|
|
if (reconnectCounter >= config.get('reconnectThreshold')) destroyRPC();
|
2017-11-24 02:51:50 +00:00
|
|
|
else return;
|
2017-11-24 02:45:23 +00:00
|
|
|
}
|
2017-11-24 02:31:37 +00:00
|
|
|
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
|
2017-11-24 02:33:50 +00:00
|
|
|
else window.showErrorMessage(`Couldn't connect to discord via rpc: ${error.message}`);
|
2017-11-24 02:31:37 +00:00
|
|
|
});
|
2017-11-24 02:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanly destroy the RPC client (if it isn't already).
|
|
|
|
function destroyRPC(): void {
|
|
|
|
// Do not continue if RPC isn't initalized.
|
|
|
|
if (!rpc) return;
|
2017-11-24 02:51:50 +00:00
|
|
|
// Clear the reconnect interval.
|
|
|
|
if (reconnect) clearInterval(reconnect);
|
|
|
|
// Null reconnect variable.
|
|
|
|
reconnect = null;
|
|
|
|
// Dispose of the event handler.
|
|
|
|
eventHandler.dispose();
|
2017-11-24 02:16:08 +00:00
|
|
|
// If there's an RPC Client initalized, destroy it.
|
|
|
|
rpc.destroy();
|
|
|
|
// Null the RPC variable.
|
|
|
|
rpc = null;
|
2017-11-24 03:49:32 +00:00
|
|
|
// Null the last known file name
|
|
|
|
lastKnownFileName = null;
|
2017-11-23 21:14:16 +00:00
|
|
|
}
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// This function updates the activity (The Client's Rich Presence status).
|
2017-11-23 21:14:16 +00:00
|
|
|
function setActivity(): void {
|
2017-11-24 00:22:23 +00:00
|
|
|
// Do not continue if RPC isn't initalized.
|
2017-11-23 13:06:21 +00:00
|
|
|
if (!rpc) return;
|
2017-11-24 03:31:18 +00:00
|
|
|
if (window.activeTextEditor && window.activeTextEditor.document.fileName === lastKnownFileName) return;
|
2017-11-24 03:49:32 +00:00
|
|
|
lastKnownFileName = window.activeTextEditor ? window.activeTextEditor.document.fileName : null;
|
2017-11-24 20:00:52 +00:00
|
|
|
|
|
|
|
const details = window.activeTextEditor
|
|
|
|
? config.get('details').replace('{filename}', basename(window.activeTextEditor.document.fileName))
|
|
|
|
: config.get('detailsIdle');
|
|
|
|
const checkState = window.activeTextEditor
|
|
|
|
? Boolean(workspace.getWorkspaceFolder(window.activeTextEditor.document.uri))
|
|
|
|
: false;
|
|
|
|
const state = window.activeTextEditor
|
|
|
|
? checkState
|
|
|
|
? config.get('workspace').replace('{workspace}', workspace.getWorkspaceFolder(window.activeTextEditor.document.uri).name)
|
|
|
|
: config.get('workspaceNotFound')
|
|
|
|
: config.get('workspaceIdle');
|
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Create a JSON Object with the user's activity information.
|
2017-11-23 13:06:21 +00:00
|
|
|
const activity = {
|
2017-11-24 20:00:52 +00:00
|
|
|
details,
|
|
|
|
state,
|
2017-11-23 13:47:42 +00:00
|
|
|
startTimestamp: new Date().getTime() / 1000,
|
2017-11-23 21:17:11 +00:00
|
|
|
largeImageKey: window.activeTextEditor
|
|
|
|
? extname(basename(window.activeTextEditor.document.fileName)).substring(1)
|
|
|
|
|| basename(window.activeTextEditor.document.fileName).substring(1)
|
|
|
|
|| 'file'
|
|
|
|
: 'vscode-big',
|
|
|
|
largeImageText: window.activeTextEditor
|
2017-11-24 12:53:47 +00:00
|
|
|
? config.get('largeImage') || window.activeTextEditor.document.languageId
|
|
|
|
: config.get('largeImageIdle'),
|
2017-11-23 13:06:21 +00:00
|
|
|
smallImageKey: 'vscode',
|
2017-11-24 12:53:47 +00:00
|
|
|
smallImageText: config.get('smallImage'),
|
2017-11-23 13:06:21 +00:00
|
|
|
instance: false
|
|
|
|
};
|
2017-11-24 00:22:23 +00:00
|
|
|
|
|
|
|
// Update the user's activity to the `activity` variable.
|
2017-11-23 19:43:10 +00:00
|
|
|
rpc.setActivity(activity);
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|