fixed a bug where sometimes removing a member from front would not register

This commit is contained in:
bee 2022-03-02 04:19:52 -08:00
parent 99affbb674
commit 8abbe93584
No known key found for this signature in database
GPG key ID: 70EECBF29DA75D8B
5 changed files with 54 additions and 51 deletions

View file

@ -6,3 +6,4 @@ token="AAAAAAAAAAAAAAAAAAAA"
userId="AAAAAAAAAAAAAAAAAAA"
pk_token= "AAAAAAAAAAAAAAAA"
pk_system="AAAAAAAAAAAAAAAA"
heartbeat=4500000

View file

@ -1,17 +1,18 @@
# SPPK
### SimplyPlural -> PluralKit Connectivity.
#### .env
```
token = "token_here",
userId = "userid_here",
pk_token = "pluralkit_token_here",
pk_system = "pluralkit_system_id"
```
`token`: Your SimplyPlural account token. As of now, the only permission necessary is the Read permission.
### Fork this repository and simply deploy to Heroku!
This project already has a Procfile set up, so it's super easy to get started. Once you have forked / cloned this repo, you can connect it to Heroku and fill in your credentials as config variables. More info below.
`userId`: Your SimplyPlural account/system ID. You can find it in account info near the bottom.
`pk_token`: Your PluralKit token. Get it by using pk;token
`pk_system`: Your Pluralkit system ID. This can be either your Discord account ID or your 5 letter ID shown by using pk;system.
#### Environment Variables
These can be set either in the .env file, in terminal, or in the config vars section of Heroku.
| Setting | Default | Description |
| ---------| ------- | ------------------ |
| url | https://devapi.apparyllis.com | The base URL for all SimplyPlural API requests. Unless you are running your own fork of Simply Plural, you shouldn't change this. |
| socket | wss://devapi.apparyllis.com/v1/socket | The socket URL for SimplyPlural. Unless you are running your own fork of Simply Plural, you shouldn't change this. |
| pk_url | https://api.pluralkit.me/v2 | The base URL for all PluralKit API requests. Unless you are running your own fork of PluralKit, you shouldn't change this. |
| token | token_here | Your SimplyPlural account token. As of now, the only permission necessary is the Read permission. |
| userId | user_id | Your SimplyPlural account/system ID. You can find it in account info near the bottom. |
| pk_token | pluralkit_token_here | Your PluralKit token. Get it by using `pk;token`. |
| pk_system | pluralkit_system_id | Your Pluralkit system ID. This can be either your Discord account ID or your 5 letter ID shown by using pk;system. |
| heartbeat | 4500000 | The time in miliseconds before the websocket client reconnects to the websocket server. |

View file

@ -80,7 +80,10 @@ generateResponse = async (target, data) => {
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({"members": fronters}), {
headers: pkHeader
})
.catch(err => console.error(err.toJSON().message))
.catch(err => {
if (err.toJSON().status == 400) unknownError400()
else console.error(err.message)
})
response += '' + member.name + ' was added to the front.'
return
@ -112,7 +115,10 @@ generateResponse = async (target, data) => {
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
headers: pkHeader
})
.catch(err => console.error(err.message))
.catch(err => {
if (err.toJSON().status == 400) unknownError400()
else console.error(err.message)
})
response += '' + member.name + ' was removed from the front.'
break;
@ -132,7 +138,10 @@ generateResponse = async (target, data) => {
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
headers: pkHeader
})
.catch(err => console.error(err.message))
.catch(err => {
if (err.toJSON().status == 400) unknownError400()
else console.error(err.message)
})
response += '' + member.name + ' is now the primary fronter.'
}
}
@ -156,6 +165,10 @@ generateResponse = async (target, data) => {
return response
}
unknownError400 = () => {
return
}
unknownTarget = (target) => {
console.log('::SimplyWS:: Unknown update target: ' + target + '\n::SimplyWS:: Full message: ' + e)
}
@ -219,14 +232,10 @@ determineAction = async (eventData, frontData = []) => {
}
// get the difference between cached history and current front
let diff = calculateDiff(cache.frontHistory, frontData)
let diff = await calculateDiff(cache.frontHistory, frontData)
// we handle one thing at a time, although this should be expanded since you can modify multiple custom statuses at once
if (diff.length == 1) {
// if there's an endTime, it was a removal event
if (diff[0].content.endTime) {
action = 'remove'
}
else if (diff[0].content.customStatus) {
if (diff[0].content.customStatus) {
// check if customStatus value is in cache
let foundInCache = Object.keys(cache.frontHistory).filter((key) => {
return cache.frontHistory[key] === diff[0].content.customStatus
@ -241,6 +250,12 @@ determineAction = async (eventData, frontData = []) => {
console.error('::SimplyWS:: Unrecognized diff: ' + JSON.stringify(diff))
}
}
else {
// if there's an endTime, it was a removal event
if (eventData.content.endTime && !eventData.content.live) {
action = 'remove'
}
}
return action
}
@ -256,7 +271,9 @@ const transform = require('lodash.transform')
const isEqual = require('lodash.isequal')
const isArray = require('lodash.isarray')
const isObject = require('lodash.isobject')
calculateDiff = (origObj, newObj) => {
const { PassThrough } = require('stream')
calculateDiff = async (origObj, newObj) => {
return new Promise(function (resolve) {
changes = (newObj, origObj) => {
let arrayIndexCounter = 0
return transform(newObj, function (result, value, key) {
@ -266,7 +283,8 @@ calculateDiff = (origObj, newObj) => {
}
})
}
return changes(newObj, origObj)
resolve(changes(newObj, origObj))
})
}
main()

View file

@ -10,14 +10,12 @@
"author": "padlocks",
"license": "ISC",
"dependencies": {
"ajv": "^8.10.0",
"axios": "^0.26.0",
"dotenv": "^16.0.0",
"lodash.isarray": "^4.0.0",
"lodash.isequal": "^4.5.0",
"lodash.isobject": "^3.0.2",
"lodash.transform": "^4.6.0",
"pkapi.js": "^3.1.0",
"ws": "^8.5.0"
},
"optionalDependencies": {

View file

@ -1,15 +0,0 @@
{
"version": 2,
"builds": [
{
"src": "./index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}