api.violets-purgatory.dev/index.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-10-20 21:53:49 -05:00
const express = require('express'),
2023-11-16 15:16:22 -06:00
path = require('path'),
fs = require('fs'),
WebSocket = require('ws')
2023-10-20 21:53:49 -05:00
var app = express()
const PORT = process.env.PORT || 8080
2023-11-16 15:16:22 -06:00
const staticpath = path.join(__dirname, 'static')
const resourcePath = path.join(__dirname, "resources")
const mainpage = resourcePath + '/mainPage.html'
var lanyardData = undefined
var discData = null
2023-10-20 21:53:49 -05:00
app.use(express.static(staticpath))
2023-11-16 15:16:22 -06:00
app.listen(PORT, () => {
console.log("Violet's Purgatory is now listening on port: " + PORT)
2023-11-03 13:01:13 -05:00
})
2023-11-16 15:16:22 -06:00
app.get("/", (req, res) => {
var html = fs.readFileSync(mainpage).toString()
var addedHTML = "<p>"
if (lanyardData && (lanyardData.spotify != null || lanyardData.activities[0] != undefined)) {
addedHTML += `Discord Status: ${lanyardData.spotify || '"' + lanyardData.activities[0].state + '"' || ""}`
}
var statuses = {
"online": {
"text": "Online",
"color": "rgb(100, 255, 100)"
},
"dnd": {
"text": "DND",
"color": "rgb(255, 100, 100)"
},
"idle": {
"text": "Idle",
"color": "rgb(255, 255, 75)"
},
"offline": {
"text": "Offline",
"color": "rgb(125, 125, 125)"
}
}
var statusData = statuses[lanyardData.discord_status]
addedHTML += `<br><span style="color: ${statusData.color}">${statusData.text}</span>`
addedHTML += `<style>.pfp { border-color: ${statusData.color} !important }</style>`
html = html.replace("{LANYARD}", addedHTML + "</p>")
res.send(html)
})
// Lanyard Stuffs
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", (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.d
} else {
console.log(data.d)
}
2023-11-07 13:07:11 -06:00
})