From 9d54038ec715b9262a6a5d06de9f76239cfd2895 Mon Sep 17 00:00:00 2001 From: bee! Date: Fri, 13 May 2022 09:39:48 -0700 Subject: [PATCH] fix customStatus events, option to silence spam messages --- .env_example | 3 ++- README.md | 1 + WebsocketClient.js | 8 ++++---- dataManager.js | 7 ++----- index.js | 8 +++++--- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/.env_example b/.env_example index c83e345..e8c8531 100644 --- a/.env_example +++ b/.env_example @@ -7,4 +7,5 @@ token="SIMPLYPLURAL_TOKEN" userId="abcd1234" pk_token= "PLURALKIT_TOKEN" heartbeat=4500000 -max_workers=1 \ No newline at end of file +max_workers=1 +silence_connections=true \ No newline at end of file diff --git a/README.md b/README.md index aed59b5..d52522d 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,4 @@ These can be set either in the .env file, in terminal, or in the config vars sec | pk_token | pluralkit_token_here | Your PluralKit token. Get it by using `pk;token`. | | heartbeat | 4500000 | The time in miliseconds before the websocket client reconnects to the websocket server. | | max_workers | 1 | Max number of workers for processing enqueued tasks. This probably shouldn't be changed. | +| silence_connections | true | Whether or not to silence the websocket connection and authentication messages. | diff --git a/WebsocketClient.js b/WebsocketClient.js index f10f9f5..bc40b9e 100644 --- a/WebsocketClient.js +++ b/WebsocketClient.js @@ -7,7 +7,7 @@ function WebSocketClient(url) { let connecting = false let backoff = 250 const init = () => { - console.error(`::SimplyWS:: [${timestamp()}] connecting`) + if (!process.env.silence_connections) console.error(`::SimplyWS:: [${timestamp()}] connecting`) connecting = false if (client !== undefined) { client.removeAllListeners() @@ -21,14 +21,14 @@ function WebSocketClient(url) { timeout = setTimeout(() => client.terminate(), process.env.heartbeat || 350000) } client.on('ping', () => { - console.log(`::SimplyWS:: [${timestamp()}] pinged`) + if (!process.env.silence_connections) console.log(`::SimplyWS:: [${timestamp()}] pinged`) heartbeat() }) client.on('open', (e) => { if (typeof this.onOpen === 'function') { this.onOpen() } else { - console.log(`::SimplyWS:: [${timestamp()}] opened`) + if (!process.env.silence_connections) console.log(`::SimplyWS:: [${timestamp()}] opened`) console.log(e) } heartbeat() @@ -37,7 +37,7 @@ function WebSocketClient(url) { if (typeof this.onMessage === 'function') { this.onMessage(e) } else { - console.log(`::SimplyWS:: [${timestamp()}] messaged`) + if (!process.env.silence_connections) console.log(`::SimplyWS:: [${timestamp()}] messaged`) } heartbeat() }) diff --git a/dataManager.js b/dataManager.js index 0b8ba3a..5e844b5 100644 --- a/dataManager.js +++ b/dataManager.js @@ -221,10 +221,9 @@ async function removeFront(member) { async function updateCustomStatus(member) { // find the "primary" fronter to move to the first element in the list - let system = new System(Config) let fronters = await getPKFronters() let primary = await findPrimary() - if (primary && fronters.length > 1) { + if (primary && fronters.length > 1 && (member.content.pkId == primary)) { if (fronters.indexOf(primary) >= 0) { fronters.splice(fronters.indexOf(primary), 1) fronters.unshift(primary) @@ -234,9 +233,7 @@ async function updateCustomStatus(member) { headers: pkHeader }) .catch(async err => { - if (err.toJSON().status == 400) - unknownError400() - else if (err.toJSON().status == 429) + if (err.toJSON().status == 429) //Too many requests console.warn("::SimplyWS:: Too many requests, waiting to try again.") setTimeout(function () { diff --git a/index.js b/index.js index 21887d2..ea23807 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,7 @@ initiateWorkerPool = () => { bc.onmessage = (event) => { //console.log('::SimplyWS:: received message from worker') queue.push(event.data, (error, task) => { + // task completed if (error.status) { console.log(`An error occurred while processing task ${error.message}`) } @@ -59,6 +60,7 @@ openWebSocket = () => { wss.onError = (e) => { console.log('SimplyWS/onError :: %s', e) } const bc = new BroadcastChannel('plural') + let first_auth = true wss.onMessage = (raw) => { e = raw let data = JSON.parse(e) @@ -66,15 +68,15 @@ openWebSocket = () => { switch (data.msg) { case "Successfully authenticated": - console.log('::SimplyWS:: authenticated') + if (!process.env.silence_connections || first_auth) console.log('::SimplyWS:: authenticated') + first_auth = false // cache current front initializeCache() break case "Authentication violation: Token is missing or invalid. Goodbye :)": - console.log('::SimplyWS:: invalid token, exiting..') + console.error('::SimplyWS:: invalid token, exiting..') process.exit(1) case "update": - initializeCache() bc.postMessage({data: data}) break default: