diff --git a/.env_example b/.env_example new file mode 100644 index 0000000..41236d3 --- /dev/null +++ b/.env_example @@ -0,0 +1,8 @@ +# SPPK +url="https://devapi.apparyllis.com" +socket="wss://devapi.apparyllis.com/v1/socket" +pk_url="https://api.pluralkit.me/v2" +token="AAAAAAAAAAAAAAAAAAAA" +userId="AAAAAAAAAAAAAAAAAAA" +pk_token= "AAAAAAAAAAAAAAAA" +pk_system="AAAAAAAAAAAAAAAA" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6769e2b..4597a55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /node_modules /.vscode config.json -package-lock.json \ No newline at end of file +package-lock.json +.env \ No newline at end of file diff --git a/README.md b/README.md index 1a4ca9b..9122d94 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # SPPK ### SimplyPlural -> PluralKit Connectivity. -#### config.json +#### .env ``` -"token": "token_here", -"userId": "userid_here", -"pk_token": "pluralkit_token_here", -"pk_system": "pluralkit_system_id" +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. diff --git a/WebsocketClient.js b/WebsocketClient.js index c68e7b9..949ab19 100644 --- a/WebsocketClient.js +++ b/WebsocketClient.js @@ -1,77 +1,77 @@ -const WebSocket = require('ws'); -const timestamp = () => new Date().toISOString().replace('T', ' ').substr(0, 19); +const WebSocket = require('ws') +const timestamp = () => new Date().toISOString().replace('T', ' ').substr(0, 19) function WebSocketClient(url) { - let client; - let timeout; - let connecting = false; - let backoff = 250; + let client + let timeout + let connecting = false + let backoff = 250 const init = () => { - console.error(`::SimplyWS:: [${timestamp()}] connecting`); - connecting = false; + console.error(`::SimplyWS:: [${timestamp()}] connecting`) + connecting = false if (client !== undefined) { - client.removeAllListeners(); + client.removeAllListeners() } - client = new WebSocket(url); + client = new WebSocket(url) const heartbeat = () => { if (timeout !== undefined) { - clearTimeout(timeout); - timeout = undefined; + clearTimeout(timeout) + timeout = undefined } - timeout = setTimeout(() => client.terminate(), 350000); - }; + timeout = setTimeout(() => client.terminate(), 350000) + } client.on('ping', () => { - console.log(`::SimplyWS:: [${timestamp()}] pinged`); - heartbeat(); - }); + console.log(`::SimplyWS:: [${timestamp()}] pinged`) + heartbeat() + }) client.on('open', (e) => { if (typeof this.onOpen === 'function') { - this.onOpen(); + this.onOpen() } else { - console.log(`::SimplyWS:: [${timestamp()}] opened`); - console.log(e); + console.log(`::SimplyWS:: [${timestamp()}] opened`) + console.log(e) } - heartbeat(); - }); + heartbeat() + }) client.on('message', (e) => { if (typeof this.onMessage === 'function') { - this.onMessage(e); + this.onMessage(e) } else { - console.log(`::SimplyWS:: [${timestamp()}] messaged`); + console.log(`::SimplyWS:: [${timestamp()}] messaged`) } - heartbeat(); - }); + heartbeat() + }) client.on('close', (e) => { if (e.code !== 1000) { if (connecting === false) { // abnormal closure - backoff = backoff === 8000 ? 250 : backoff * 2; - setTimeout(() => init(), backoff); - connecting = true; + backoff = backoff === 8000 ? 250 : backoff * 2 + setTimeout(() => init(), backoff) + connecting = true } } else if (typeof this.onClose === 'function') { - this.onClose(); + this.onClose() } else { - console.error(`::SimplyWS:: [${timestamp()}] closed`); - console.error(e); + console.error(`::SimplyWS:: [${timestamp()}] closed`) + console.error(e) } - }); + }) client.on('error', (e) => { if (e.code === 'ECONREFUSED') { if (connecting === false) { // abnormal closure - backoff = backoff === 8000 ? 250 : backoff * 2; - setTimeout(() => init(), backoff); - connecting = true; + backoff = backoff === 8000 ? 250 : backoff * 2 + setTimeout(() => init(), backoff) + connecting = true } } else if (typeof this.onError === 'function') { - this.onError(e); + this.onError(e) } else { - console.error(`::SimplyWS:: [${timestamp()}] errored`); - console.error(e); + console.error(`::SimplyWS:: [${timestamp()}] errored`) + console.error(e) } - }); - this.send = client.send.bind(client); - }; - init(); + }) + this.send = client.send.bind(client) + } + init() } -module.exports = WebSocketClient; \ No newline at end of file +module.exports = WebSocketClient \ No newline at end of file diff --git a/index.js b/index.js index 162a7cd..541e807 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,12 @@ +const dotenv = require('dotenv') +dotenv.config() +const config = process.env + const axios = require('axios') -const config = require('./config.json') const SAPI = require('./SimplyAPI') const SimplyAPI = new SAPI(config) -const pkUrl = 'https://api.pluralkit.me/v2' +const pkUrl = config.pk_url const pkHeader = { 'Content-Type': 'application/json', 'Authorization': config.pk_token diff --git a/package.json b/package.json index 00b73b3..cfbb48a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "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",