const express = require('express'), path = require("path"), fs = require("fs"), WebSocket = require("ws") const PORT = process.env.PORT || 8080 var app = express() var lanyardData = "Nothing Fetched yet!" var sockets = [] var pfpSeason = "default" var violetSpins = 0 function randomFileInDir(directory) { var dir = fs.readdirSync(directory) return path.join(directory, dir[Math.floor(Math.random() * dir.length)]) } app.get("/", (req, res) => { var docs = fs.readFileSync(path.join(__dirname, "basicDocs.json")) docs = JSON.parse(docs) res.send(docs) }) app.get("/v1/pfp", (req, res) => { var time = new Date(Date.now()) if ([11, 12, 1].includes(time.getMonth() + 1)) { pfpSeason = "christmas" } var pfpConfig = JSON.parse(fs.readFileSync(path.join(__dirname, "PFPs.json")))[pfpSeason] var characterNumb = (time.getDay() - 1) % pfpConfig.length var char = pfpConfig[characterNumb] var pfps = char.pfps try { var pfpPool = pfps var rand = Math.floor(Math.random() * pfpPool.length) var pfp = path.join(__dirname, char.pfpRoot, pfpPool[rand]) res.sendFile(pfp) } catch (error) { var pfp = path.join(__dirname, char.fallback) res.sendFile(pfp) } }) app.get("/v1/lanyard", (req, res) => { res.send(lanyardData) }) app.get("/v1/spins", (req, res) => { res.send(violetSpins.toString()) }) // app.listen(PORT, () => { // console.log("API is now listening on port 8080!") // }) function socketeer() { var lanyard = new WebSocket('wss://api.lanyard.rest/socket') function beat(dur) { lanyard.send(JSON.stringify({ op: 3 })) setTimeout(() => { beat(dur) }, dur); } lanyard.addEventListener("message", async (res) => { var data = JSON.parse(res.data) if (data.op == 1) { beat(data.d.heartbeat_interval) lanyard.send(JSON.stringify({ op: 2, d: { subscribe_to_id: "534132311781015564" } })) } else if (data.op == 0) { lanyardData = data for (let index = 0; index < sockets.length; index++) { var socketData = sockets[index] var socket = socketData.socket; socket.send(JSON.stringify(lanyardData)) if ((Date.now() - socketData.lastPing) > 120000) { sockets.splice(index, 1) } } } }) lanyard.on('close', () => { console.log("Disconnected") setTimeout(() => { socketeer() }, 60000) }) } socketeer() // wsServer = new WebSocket.WebSocketServer({ port: 8090 }) wsServer = WebSocket.Server; let server = require('http').createServer() wsServer = new wsServer({ server: server, perMessageDeflate: false }) server.on('request', app) wsServer.on("connection", function connection(socket) { socket.on('message', function message(data) { data = JSON.parse(data) if (data.op == 3) { for (let index = 0; index < sockets.length; index++) { const socketData = sockets[index]; if (socketData.socket == socket) { sockets[index].lastPing = Date.now() } } socket.send(`{"op": 3}`) } else if (data.op == 4) { violetSpins += 1 for (var i = 0; i < sockets.length; i++) { var soc = sockets[i].socket soc.send(`{"op": 4, "spins": ${violetSpins}}`) } } else { console.log(data) } }) socket.send(JSON.stringify(lanyardData)) socket.send(`{ "op": 1 }`) socket.send(`{"op": 4, "spins": ${violetSpins}}`) sockets.push({ socket, lastPing: Date.now() }) }) server.listen(PORT, function () { console.log("API & Websocket now on port: " + PORT) })