Merge pull request #8 from padlocks/dev

fix customStatus events, option to silence spam messages
This commit is contained in:
bee! 2022-05-13 09:41:45 -07:00 committed by GitHub
commit 032f4fa753
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 13 deletions

View file

@ -8,3 +8,4 @@ userId="abcd1234"
pk_token= "PLURALKIT_TOKEN"
heartbeat=4500000
max_workers=1
silence_connections=true

View file

@ -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. |

View file

@ -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()
})

View file

@ -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 () {

View file

@ -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: