discord-vscode/src/extension.ts

95 lines
3.2 KiB
TypeScript
Raw Normal View History

// 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';
import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode';
2017-11-23 23:53:16 +00:00
import { setInterval, clearInterval } from 'timers';
2017-11-23 13:06:21 +00:00
// Define the RPC variable and its type.
let rpc: Client;
// `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) {
// Get the workspace's configuration for "discord".
const config = workspace.getConfiguration('discord');
2017-11-23 13:06:21 +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'));
}
// Register the `discord.enable` command, and set the `enabled` config option to true.
2017-11-23 23:53:16 +00:00
const enabler = commands.registerCommand('discord.enable', () => {
config.update('enabled', true);
initRPC(config.get('clientID'));
2017-11-23 13:06:21 +00:00
});
// Register the `discord.disable` command, and set the `enabled` config option to false.
2017-11-23 23:53:16 +00:00
const disabler = commands.registerCommand('discord.disable', () => {
config.update('enabled', false);
rpc.setActivity({});
});
// Push the new commands into the subscriptions.
2017-11-23 23:53:16 +00:00
context.subscriptions.push(enabler, disabler);
2017-11-23 13:06:21 +00:00
}
// `Deactivate` is fired whenever the extension is deactivated.
export function deactivate(context: ExtensionContext) {
// If there's an RPC Client initalized, destroy it.
if (rpc) rpc.destroy();
}
2017-11-23 13:06:21 +00:00
// This function updates the activity (The Client's Rich Presence status).
function setActivity(): void {
// Do not continue if RPC isn't initalized.
2017-11-23 13:06:21 +00:00
if (!rpc) return;
// Create a JSON Object with the user's activity information.
2017-11-23 13:06:21 +00:00
const activity = {
details: window.activeTextEditor
? `Editing ${basename(window.activeTextEditor.document.fileName)}`
: 'Idle.',
state: window.activeTextEditor
? `Workspace: ${workspace.getWorkspaceFolder(window.activeTextEditor.document.uri).name}`
: 'Idling.',
startTimestamp: new Date().getTime() / 1000,
largeImageKey: window.activeTextEditor
? extname(basename(window.activeTextEditor.document.fileName)).substring(1)
|| basename(window.activeTextEditor.document.fileName).substring(1)
|| 'file'
: 'vscode-big',
largeImageText: window.activeTextEditor
? window.activeTextEditor.document.languageId
: 'Idling',
2017-11-23 13:06:21 +00:00
smallImageKey: 'vscode',
smallImageText: 'Visual Studio Code',
2017-11-23 13:06:21 +00:00
instance: false
};
// Update the user's activity to the `activity` variable.
rpc.setActivity(activity);
2017-11-23 13:06:21 +00:00
}
2017-11-23 23:53:16 +00:00
/// Initalize the RPC systems.
2017-11-23 23:53:16 +00:00
function initRPC(clientID: string): void {
// Update the RPC variable with a new RPC Client.
2017-11-23 23:53:16 +00:00
rpc = new Client({ transport: 'ipc' });
// Once the RPC Client is ready, set the activity.
2017-11-23 23:53:16 +00:00
rpc.once('ready', () => {
setActivity();
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
});
// Log in to the RPC Client, and check whether or not it errors.
2017-11-23 23:53:16 +00:00
rpc.login(clientID).catch(error =>
error.message.includes('ENOENT')
? window.showErrorMessage('No Discord Client detected!')
: window.showErrorMessage(`Could not connect to discord via rpc: ${error.message}`)
);
}