discord-vscode/src/extension.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

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 13:06:21 +00:00
let rpc: Client;
2017-11-23 13:06:21 +00:00
export function activate(context: ExtensionContext) {
rpc = new Client({ transport: 'ipc' });
const config = workspace.getConfiguration('discord');
2017-11-23 13:06:21 +00:00
rpc.once('ready', () => {
setActivity();
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
2017-11-23 13:06:21 +00:00
});
rpc.login(config.get('clientID')).catch(error =>
2017-11-23 13:06:21 +00:00
window.showErrorMessage(`Could not connect to discord via rpc: ${error.message}`)
);
}
export function deactivate(context: ExtensionContext) {
if (rpc) rpc.destroy();
}
2017-11-23 13:06:21 +00:00
function setActivity(): void {
2017-11-23 13:06:21 +00:00
if (!rpc) return;
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
};
rpc.setActivity(activity);
2017-11-23 13:06:21 +00:00
}