feat: enable/disable commands (#1)
This commit is contained in:
parent
4d72a15bd8
commit
428d22d703
3 changed files with 45 additions and 20 deletions
13
.vscode/launch.json
vendored
13
.vscode/launch.json
vendored
|
@ -1,6 +1,6 @@
|
|||
// A launch configuration that compiles the extension and then opens it inside a new window
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"version": "0.5.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Extension",
|
||||
|
@ -12,17 +12,6 @@
|
|||
"sourceMaps": true,
|
||||
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
|
||||
"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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
19
package.json
19
package.json
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "discord-vscode",
|
||||
"displayName": "Discord Presence",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.0",
|
||||
"description": "Update your discord status with the newly added rich presence.",
|
||||
"author": {
|
||||
"name": "iCrawl",
|
||||
|
@ -21,6 +21,18 @@
|
|||
"*"
|
||||
],
|
||||
"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": [
|
||||
{
|
||||
"title": "Visual Studio Code Discord Configuration",
|
||||
|
@ -30,6 +42,11 @@
|
|||
"type": "string",
|
||||
"default": "383226320970055681",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,20 +1,26 @@
|
|||
import { Client } from 'discord-rpc';
|
||||
import { basename, extname } from 'path';
|
||||
import { ExtensionContext, commands, window, workspace, Uri, TextDocumentChangeEvent, TextDocument } from 'vscode';
|
||||
import { setInterval, clearInterval } from 'timers';
|
||||
|
||||
let rpc: Client;
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
rpc = new Client({ transport: 'ipc' });
|
||||
const config = workspace.getConfiguration('discord');
|
||||
|
||||
rpc.once('ready', () => {
|
||||
setActivity();
|
||||
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => setActivity());
|
||||
if (config.get('enabled')) {
|
||||
initRPC(config.get('clientID'));
|
||||
}
|
||||
const enabler = commands.registerCommand('discord.enable', () => {
|
||||
config.update('enabled', true);
|
||||
initRPC(config.get('clientID'));
|
||||
});
|
||||
rpc.login(config.get('clientID')).catch(error =>
|
||||
window.showErrorMessage(`Could not connect to discord via rpc: ${error.message}`)
|
||||
);
|
||||
const disabler = commands.registerCommand('discord.disable', () => {
|
||||
config.update('enabled', false);
|
||||
rpc.setActivity({});
|
||||
});
|
||||
|
||||
context.subscriptions.push(enabler, disabler);
|
||||
}
|
||||
|
||||
export function deactivate(context: ExtensionContext) {
|
||||
|
@ -45,3 +51,16 @@ function setActivity(): void {
|
|||
};
|
||||
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}`)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue