Create reconnect button/command (#29)

* Create reconnect button/command

* fixed formatting

* //
This commit is contained in:
Zachary Kohnen 2018-01-04 21:17:18 -05:00 committed by Crawl
parent 9a89ef3290
commit ec3008bd51

View file

@ -8,6 +8,8 @@ import {
Disposable, Disposable,
env, env,
ExtensionContext, ExtensionContext,
StatusBarItem,
StatusBarAlignment,
window, window,
workspace, workspace,
WorkspaceFolder WorkspaceFolder
@ -33,6 +35,8 @@ let lastKnownFileName: string;
let activity: object; let activity: object;
// Define the activity timer to not spam the API with requests. // Define the activity timer to not spam the API with requests.
let activityTimer: NodeJS.Timer; let activityTimer: NodeJS.Timer;
// Define the status bar icon
let statusBarIcon: StatusBarItem;
// `Activate` is fired when the extension is enabled. This SHOULD only fire once. // `Activate` is fired when the extension is enabled. This SHOULD only fire once.
export function activate(context: ExtensionContext) { export function activate(context: ExtensionContext) {
@ -58,8 +62,17 @@ export function activate(context: ExtensionContext) {
window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
}); });
// Register the `discord.reconnect` command
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
if (rpc) try { await destroyRPC(); } catch {}
initRPC(config.get('clientID'), true);
window.showInformationMessage('Reconnecting to Discord RPC');
if (statusBarIcon) statusBarIcon.text = '$(pulse) Reconnecting';
});
// Push the new commands into the subscriptions. // Push the new commands into the subscriptions.
context.subscriptions.push(enabler, disabler); context.subscriptions.push(enabler, disabler, reconnecter);
} }
// `Deactivate` is fired whenever the extension is deactivated. // `Deactivate` is fired whenever the extension is deactivated.
@ -69,12 +82,17 @@ export async function deactivate() {
} }
// Initalize the RPC systems. // Initalize the RPC systems.
function initRPC(clientID: string): void { function initRPC(clientID: string, loud?: boolean): void {
// Update the RPC variable with a new RPC Client. // Update the RPC variable with a new RPC Client.
rpc = new Client({ transport: 'ipc' }); rpc = new Client({ transport: 'ipc' });
// Once the RPC Client is ready, set the activity. // Once the RPC Client is ready, set the activity.
rpc.once('ready', () => { rpc.once('ready', () => {
if (loud) window.showInformationMessage('Successfully reconnected to Discord RPC');
// Remove icon if connected
if (statusBarIcon) statusBarIcon.dispose();
// This is purely for safety measures. // This is purely for safety measures.
if (reconnectTimer) { if (reconnectTimer) {
// Clear the reconnect interval. // Clear the reconnect interval.
@ -117,17 +135,34 @@ function initRPC(clientID: string): void {
rpc.login(clientID).catch(async error => { rpc.login(clientID).catch(async error => {
if (reconnectTimer) { if (reconnectTimer) {
// Destroy and dispose of everything after a default of 20 reconnect attempts // Destroy and dispose of everything after a default of 20 reconnect attempts
if (reconnectCounter >= config.get('reconnectThreshold')) await destroyRPC(); if (reconnectCounter >= config.get('reconnectThreshold')) {
else return; createButon();
await destroyRPC();
} else {
return;
}
} }
if (!config.get('silent')) { if (!config.get('silent')) {
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!'); if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.message}`); else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.message}`);
createButon();
} }
}); });
} }
// Cleanly destroy the RPC client (if it isn't already). // Create reconnect button
function createButon(): void {
if (!statusBarIcon) {
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
statusBarIcon.text = '$(plug) Reconnect to Discord';
statusBarIcon.command = 'discord.reconnect';
statusBarIcon.show();
} else {
statusBarIcon.text = '$(plug) Reconnect to Discord';
}
}
// Cleanly destroy the RPC client (if it isn't already). && add icon to reconnect
async function destroyRPC(): Promise<void> { async function destroyRPC(): Promise<void> {
// Do not continue if RPC isn't initalized. // Do not continue if RPC isn't initalized.
if (!rpc) return; if (!rpc) return;