fixed a bug where sometimes removing a member from front would not register
This commit is contained in:
parent
99affbb674
commit
8abbe93584
5 changed files with 54 additions and 51 deletions
|
|
@ -5,4 +5,5 @@ pk_url="https://api.pluralkit.me/v2"
|
||||||
token="AAAAAAAAAAAAAAAAAAAA"
|
token="AAAAAAAAAAAAAAAAAAAA"
|
||||||
userId="AAAAAAAAAAAAAAAAAAA"
|
userId="AAAAAAAAAAAAAAAAAAA"
|
||||||
pk_token= "AAAAAAAAAAAAAAAA"
|
pk_token= "AAAAAAAAAAAAAAAA"
|
||||||
pk_system="AAAAAAAAAAAAAAAA"
|
pk_system="AAAAAAAAAAAAAAAA"
|
||||||
|
heartbeat=4500000
|
||||||
27
README.md
27
README.md
|
|
@ -1,17 +1,18 @@
|
||||||
# SPPK
|
# SPPK
|
||||||
### SimplyPlural -> PluralKit Connectivity.
|
### SimplyPlural -> PluralKit Connectivity.
|
||||||
|
|
||||||
#### .env
|
### 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.
|
||||||
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.
|
|
||||||
|
|
||||||
`userId`: Your SimplyPlural account/system ID. You can find it in account info near the bottom.
|
#### Environment Variables
|
||||||
|
These can be set either in the .env file, in terminal, or in the config vars section of Heroku.
|
||||||
`pk_token`: Your PluralKit token. Get it by using pk;token
|
| Setting | Default | Description |
|
||||||
|
| ---------| ------- | ------------------ |
|
||||||
`pk_system`: Your Pluralkit system ID. This can be either your Discord account ID or your 5 letter ID shown by using pk;system.
|
| 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. |
|
||||||
58
index.js
58
index.js
|
|
@ -80,7 +80,10 @@ generateResponse = async (target, data) => {
|
||||||
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({"members": fronters}), {
|
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({"members": fronters}), {
|
||||||
headers: pkHeader
|
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.'
|
response += '' + member.name + ' was added to the front.'
|
||||||
return
|
return
|
||||||
|
|
@ -112,7 +115,10 @@ generateResponse = async (target, data) => {
|
||||||
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
|
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
|
||||||
headers: pkHeader
|
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.'
|
response += '' + member.name + ' was removed from the front.'
|
||||||
break;
|
break;
|
||||||
|
|
@ -132,7 +138,10 @@ generateResponse = async (target, data) => {
|
||||||
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
|
axios.post(`${pkUrl}/systems/${config.pk_system}/switches`, JSON.stringify({ "members": fronters }), {
|
||||||
headers: pkHeader
|
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.'
|
response += '' + member.name + ' is now the primary fronter.'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -156,6 +165,10 @@ generateResponse = async (target, data) => {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unknownError400 = () => {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
unknownTarget = (target) => {
|
unknownTarget = (target) => {
|
||||||
console.log('::SimplyWS:: Unknown update target: ' + target + '\n::SimplyWS:: Full message: ' + e)
|
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
|
// 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
|
// 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 (diff.length == 1) {
|
||||||
// if there's an endTime, it was a removal event
|
if (diff[0].content.customStatus) {
|
||||||
if (diff[0].content.endTime) {
|
|
||||||
action = 'remove'
|
|
||||||
}
|
|
||||||
else if (diff[0].content.customStatus) {
|
|
||||||
// check if customStatus value is in cache
|
// check if customStatus value is in cache
|
||||||
let foundInCache = Object.keys(cache.frontHistory).filter((key) => {
|
let foundInCache = Object.keys(cache.frontHistory).filter((key) => {
|
||||||
return cache.frontHistory[key] === diff[0].content.customStatus
|
return cache.frontHistory[key] === diff[0].content.customStatus
|
||||||
|
|
@ -241,6 +250,12 @@ determineAction = async (eventData, frontData = []) => {
|
||||||
console.error('::SimplyWS:: Unrecognized diff: ' + JSON.stringify(diff))
|
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
|
return action
|
||||||
}
|
}
|
||||||
|
|
@ -256,17 +271,20 @@ const transform = require('lodash.transform')
|
||||||
const isEqual = require('lodash.isequal')
|
const isEqual = require('lodash.isequal')
|
||||||
const isArray = require('lodash.isarray')
|
const isArray = require('lodash.isarray')
|
||||||
const isObject = require('lodash.isobject')
|
const isObject = require('lodash.isobject')
|
||||||
calculateDiff = (origObj, newObj) => {
|
const { PassThrough } = require('stream')
|
||||||
changes = (newObj, origObj) => {
|
calculateDiff = async (origObj, newObj) => {
|
||||||
let arrayIndexCounter = 0
|
return new Promise(function (resolve) {
|
||||||
return transform(newObj, function (result, value, key) {
|
changes = (newObj, origObj) => {
|
||||||
if (!isEqual(value, origObj[key])) {
|
let arrayIndexCounter = 0
|
||||||
let resultKey = isArray(origObj) ? arrayIndexCounter++ : key
|
return transform(newObj, function (result, value, key) {
|
||||||
result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value
|
if (!isEqual(value, origObj[key])) {
|
||||||
}
|
let resultKey = isArray(origObj) ? arrayIndexCounter++ : key
|
||||||
})
|
result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value
|
||||||
}
|
}
|
||||||
return changes(newObj, origObj)
|
})
|
||||||
|
}
|
||||||
|
resolve(changes(newObj, origObj))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
@ -10,14 +10,12 @@
|
||||||
"author": "padlocks",
|
"author": "padlocks",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ajv": "^8.10.0",
|
|
||||||
"axios": "^0.26.0",
|
"axios": "^0.26.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"lodash.isarray": "^4.0.0",
|
"lodash.isarray": "^4.0.0",
|
||||||
"lodash.isequal": "^4.5.0",
|
"lodash.isequal": "^4.5.0",
|
||||||
"lodash.isobject": "^3.0.2",
|
"lodash.isobject": "^3.0.2",
|
||||||
"lodash.transform": "^4.6.0",
|
"lodash.transform": "^4.6.0",
|
||||||
"pkapi.js": "^3.1.0",
|
|
||||||
"ws": "^8.5.0"
|
"ws": "^8.5.0"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
|
|
|
||||||
15
vercel.json
15
vercel.json
|
|
@ -1,15 +0,0 @@
|
||||||
{
|
|
||||||
"version": 2,
|
|
||||||
"builds": [
|
|
||||||
{
|
|
||||||
"src": "./index.js",
|
|
||||||
"use": "@vercel/node"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"routes": [
|
|
||||||
{
|
|
||||||
"src": "/(.*)",
|
|
||||||
"dest": "/"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue