feat: enable/disable commands (#1)

This commit is contained in:
FOG_Yamato 2017-11-24 01:53:16 +02:00 committed by Crawl
parent 4d72a15bd8
commit 428d22d703
3 changed files with 45 additions and 20 deletions

13
.vscode/launch.json vendored
View file

@ -1,6 +1,6 @@
// A launch configuration that compiles the extension and then opens it inside a new window // A launch configuration that compiles the extension and then opens it inside a new window
{ {
"version": "0.1.0", "version": "0.5.0",
"configurations": [ "configurations": [
{ {
"name": "Launch Extension", "name": "Launch Extension",
@ -12,17 +12,6 @@
"sourceMaps": true, "sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
"preLaunchTask": "npm" "preLaunchTask": "npm"
},
{
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/test/**/*.js" ],
"preLaunchTask": "npm"
} }
] ]
} }

View file

@ -1,7 +1,7 @@
{ {
"name": "discord-vscode", "name": "discord-vscode",
"displayName": "Discord Presence", "displayName": "Discord Presence",
"version": "0.4.0", "version": "0.5.0",
"description": "Update your discord status with the newly added rich presence.", "description": "Update your discord status with the newly added rich presence.",
"author": { "author": {
"name": "iCrawl", "name": "iCrawl",
@ -21,6 +21,18 @@
"*" "*"
], ],
"contributes": { "contributes": {
"commands": [
{
"command": "discord.enable",
"title": "Enable Visual Studio Code Discord in the current workspace",
"category": "VS-Discord"
},
{
"command": "discord.disable",
"title": "Disable Visual Studio Code Discord in the current workspace",
"category": "VS-Discord"
}
],
"configuration": [ "configuration": [
{ {
"title": "Visual Studio Code Discord Configuration", "title": "Visual Studio Code Discord Configuration",
@ -30,6 +42,11 @@
"type": "string", "type": "string",
"default": "383226320970055681", "default": "383226320970055681",
"description": "Only modify this if you know what you are doing (most of you don't)." "description": "Only modify this if you know what you are doing (most of you don't)."
},
"discord.enabled": {
"type": "boolean",
"default": true,
"description": "Controls if Visual Studio Code Discord is active"
} }
} }
} }

View file

@ -1,20 +1,26 @@
import { Client } from 'discord-rpc'; import { Client } from 'discord-rpc';
import { basename, extname } from 'path'; import { basename, extname } from 'path';
import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode'; import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode';
import { setInterval, clearInterval } from 'timers';
let rpc: Client; let rpc: Client;
export function activate(context: ExtensionContext) { export function activate(context: ExtensionContext) {
rpc = new Client({ transport: 'ipc' });
const config = workspace.getConfiguration('discord'); const config = workspace.getConfiguration('discord');
rpc.once('ready', () => { if (config.get('enabled')) {
setActivity(); initRPC(config.get('clientID'));
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity()); }
const enabler = commands.registerCommand('discord.enable', () => {
config.update('enabled', true);
initRPC(config.get('clientID'));
}); });
rpc.login(config.get('clientID')).catch(error => const disabler = commands.registerCommand('discord.disable', () => {
window.showErrorMessage(`Could not connect to discord via rpc: ${error.message}`) config.update('enabled', false);
); rpc.setActivity({});
});
context.subscriptions.push(enabler, disabler);
} }
export function deactivate(context: ExtensionContext) { export function deactivate(context: ExtensionContext) {
@ -45,3 +51,16 @@ function setActivity(): void {
}; };
rpc.setActivity(activity); rpc.setActivity(activity);
} }
function initRPC(clientID: string): void {
rpc = new Client({ transport: 'ipc' });
rpc.once('ready', () => {
setActivity();
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
});
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}`)
);
}