2017-11-23 13:06:21 +00:00
|
|
|
import { Client } from 'discord-rpc';
|
|
|
|
import { basename, extname } from 'path';
|
2017-11-23 15:21:46 +00:00
|
|
|
import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode';
|
2017-11-23 13:06:21 +00:00
|
|
|
|
|
|
|
export function activate(context: ExtensionContext) {
|
|
|
|
const rpc = new Client({ transport: 'ipc' });
|
2017-11-23 15:21:46 +00:00
|
|
|
const config = workspace.getConfiguration('discord');
|
2017-11-23 13:06:21 +00:00
|
|
|
|
|
|
|
rpc.once('ready', () => {
|
|
|
|
setActivity(rpc);
|
2017-11-23 15:21:46 +00:00
|
|
|
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity(rpc));
|
2017-11-23 13:06:21 +00:00
|
|
|
});
|
2017-11-23 15:21:46 +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) {}
|
|
|
|
|
|
|
|
function setActivity(rpc: Client): void {
|
|
|
|
if (!rpc) return;
|
|
|
|
const activity = {
|
2017-11-23 13:47:42 +00:00
|
|
|
details: window.activeTextEditor ? `Editing ${basename(window.activeTextEditor.document.fileName)}` : 'Idle.',
|
2017-11-23 19:43:10 +00:00
|
|
|
state: window.activeTextEditor ? `Workspace: ${workspace.getWorkspaceFolder(window.activeTextEditor.document.uri).name}` : 'Idling.',
|
2017-11-23 13:47:42 +00:00
|
|
|
startTimestamp: new Date().getTime() / 1000,
|
2017-11-23 20:14:56 +00:00
|
|
|
largeImageKey: window.activeTextEditor ? extname(basename(window.activeTextEditor.document.fileName)).substring(1) || basename(window.activeTextEditor.document.fileName).substring(1) || 'file' : 'vscode-big',
|
2017-11-23 13:47:42 +00:00
|
|
|
largeImageText: window.activeTextEditor ? window.activeTextEditor.document.languageId : 'Idling.',
|
2017-11-23 13:06:21 +00:00
|
|
|
smallImageKey: 'vscode',
|
2017-11-23 13:47:42 +00:00
|
|
|
smallImageText: 'Visual Studio Code',
|
2017-11-23 13:06:21 +00:00
|
|
|
instance: false
|
|
|
|
};
|
2017-11-23 19:43:10 +00:00
|
|
|
rpc.setActivity(activity);
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|