2024-01-20 22:14:18 -06:00
|
|
|
const express = require('express'),
|
2024-02-07 21:49:28 -06:00
|
|
|
path = require("path"),
|
|
|
|
fs = require("fs"),
|
|
|
|
WebSocket = require("ws")
|
2024-01-19 18:39:24 -06:00
|
|
|
|
|
|
|
const PORT = process.env.PORT || 8080
|
|
|
|
|
|
|
|
var app = express()
|
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
var lanyardData = "Nothing Fetched yet!"
|
|
|
|
|
|
|
|
var sockets = []
|
|
|
|
|
2024-03-07 10:54:28 -06:00
|
|
|
var pfpSeason = "default"
|
|
|
|
|
2024-04-20 15:54:05 -05:00
|
|
|
var violetSpins = 0
|
|
|
|
|
2024-06-25 01:22:17 -05:00
|
|
|
var lastActivity
|
|
|
|
|
2024-06-19 12:55:18 -05:00
|
|
|
var configPath = path.join(__dirname, "config")
|
|
|
|
if (!fs.existsSync(configPath)) {
|
|
|
|
fs.mkdirSync(configPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
var configs = ["socials.json"]
|
|
|
|
|
|
|
|
for (var i = 0; i < configs.length; i++) {
|
|
|
|
var file = configs[i]
|
|
|
|
var filePath = path.join(configPath, file)
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
fs.writeFileSync(filePath, "{}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:14:18 -06:00
|
|
|
function randomFileInDir(directory) {
|
|
|
|
var dir = fs.readdirSync(directory)
|
|
|
|
return path.join(directory, dir[Math.floor(Math.random() * dir.length)])
|
|
|
|
}
|
|
|
|
|
2024-01-19 18:44:51 -06:00
|
|
|
app.get("/", (req, res) => {
|
2024-02-07 21:49:28 -06:00
|
|
|
var docs = fs.readFileSync(path.join(__dirname, "basicDocs.json"))
|
|
|
|
docs = JSON.parse(docs)
|
|
|
|
res.send(docs)
|
2024-01-19 18:44:51 -06:00
|
|
|
})
|
2024-01-19 18:39:24 -06:00
|
|
|
|
2024-01-20 22:14:18 -06:00
|
|
|
app.get("/v1/pfp", (req, res) => {
|
2024-03-07 10:54:28 -06:00
|
|
|
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]
|
2024-06-14 00:37:17 -05:00
|
|
|
var characterNumb = (time.getDay()) % pfpConfig.length
|
2024-03-07 10:54:28 -06:00
|
|
|
var char = pfpConfig[characterNumb]
|
|
|
|
var pfps = char.pfps
|
|
|
|
|
|
|
|
try {
|
2024-06-11 23:00:16 -05:00
|
|
|
var pfpPool = pfps
|
2024-03-07 10:54:28 -06:00
|
|
|
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)
|
|
|
|
}
|
2024-01-20 22:14:18 -06:00
|
|
|
})
|
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
app.get("/v1/lanyard", (req, res) => {
|
|
|
|
res.send(lanyardData)
|
|
|
|
})
|
|
|
|
|
2024-04-20 16:03:51 -05:00
|
|
|
app.get("/v1/spins", (req, res) => {
|
2024-04-21 13:12:50 -05:00
|
|
|
res.send(violetSpins.toString())
|
2024-04-20 16:03:51 -05:00
|
|
|
})
|
|
|
|
|
2024-06-19 12:55:18 -05:00
|
|
|
app.get("/v1/socials", (req, res) => {
|
|
|
|
res.sendFile(path.join(configPath, "socials.json"))
|
|
|
|
})
|
|
|
|
|
2024-02-07 21:58:04 -06:00
|
|
|
// app.listen(PORT, () => {
|
|
|
|
// console.log("API is now listening on port 8080!")
|
|
|
|
// })
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
function socketeer() {
|
|
|
|
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
function beat(dur) {
|
2024-02-07 21:49:28 -06:00
|
|
|
lanyard.send(JSON.stringify({
|
2024-02-19 10:57:50 -06:00
|
|
|
op: 3
|
2024-02-07 21:49:28 -06:00
|
|
|
}))
|
2024-02-19 10:57:50 -06:00
|
|
|
setTimeout(() => {
|
|
|
|
beat(dur)
|
|
|
|
}, dur);
|
|
|
|
}
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
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
|
2024-06-19 13:13:05 -05:00
|
|
|
lanyardData.d.socials = JSON.parse(fs.readFileSync(path.join(configPath, "socials.json")))
|
2024-06-26 23:56:47 -05:00
|
|
|
|
|
|
|
var newArr = []
|
|
|
|
for (var i = 0; i < lanyardData.d.activities.length; i++) {
|
|
|
|
var activity = lanyardData.d.activities[i]
|
|
|
|
|
2024-06-29 16:06:16 -05:00
|
|
|
if (activity.type == 2) {
|
|
|
|
// if (activity.name == "Spotify") {
|
2024-06-29 16:06:54 -05:00
|
|
|
if (activity.assets) {
|
2024-06-29 16:45:30 -05:00
|
|
|
activity.song = activity.details
|
|
|
|
activity.album = activity.assets.large_text
|
|
|
|
activity.artist = activity.state
|
2024-06-29 16:06:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:56:47 -05:00
|
|
|
if (activity.type != 4) {
|
|
|
|
newArr.push(activity)
|
|
|
|
} else if (activity.type == 4) {
|
|
|
|
lanyardData.d.custom_status = {
|
|
|
|
"text": activity.state
|
|
|
|
}
|
|
|
|
if (activity.emoji) {
|
|
|
|
lanyardData.d.custom_status.emoji = activity.emoji
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lanyardData.d.activities = newArr
|
|
|
|
|
2024-06-25 13:04:39 -05:00
|
|
|
if (lanyardData.d.discord_status == "offline") {
|
|
|
|
lanyardData.d.activityChanged = false
|
|
|
|
} else {
|
|
|
|
lanyardData.d.activityChanged = lastActivity != JSON.stringify(lanyardData.d.activities)
|
|
|
|
}
|
2024-06-25 01:22:17 -05:00
|
|
|
lastActivity = JSON.stringify(lanyardData.d.activities)
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
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)
|
|
|
|
}
|
2024-02-07 21:49:28 -06:00
|
|
|
}
|
|
|
|
}
|
2024-02-19 10:57:50 -06:00
|
|
|
})
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
lanyard.on('close', () => {
|
|
|
|
console.log("Disconnected")
|
|
|
|
setTimeout(() => {
|
|
|
|
socketeer()
|
|
|
|
}, 60000)
|
|
|
|
})
|
|
|
|
}
|
2024-02-18 06:34:43 -06:00
|
|
|
|
2024-02-19 11:01:40 -06:00
|
|
|
socketeer()
|
|
|
|
|
2024-02-07 21:58:04 -06:00
|
|
|
// wsServer = new WebSocket.WebSocketServer({ port: 8090 })
|
2024-02-19 10:57:50 -06:00
|
|
|
|
2024-02-07 21:58:04 -06:00
|
|
|
wsServer = WebSocket.Server;
|
|
|
|
let server = require('http').createServer()
|
|
|
|
wsServer = new wsServer({
|
|
|
|
server: server,
|
|
|
|
perMessageDeflate: false
|
|
|
|
})
|
|
|
|
server.on('request', app)
|
2024-02-07 21:49:28 -06:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 10:57:50 -06:00
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
socket.send(`{"op": 3}`)
|
2024-04-20 16:03:51 -05:00
|
|
|
} else if (data.op == 4) {
|
|
|
|
violetSpins += 1
|
2024-04-20 16:10:27 -05:00
|
|
|
for (var i = 0; i < sockets.length; i++) {
|
2024-04-20 16:14:23 -05:00
|
|
|
var soc = sockets[i].socket
|
|
|
|
soc.send(`{"op": 4, "spins": ${violetSpins}}`)
|
2024-04-20 16:10:27 -05:00
|
|
|
|
|
|
|
}
|
2024-04-20 15:54:05 -05:00
|
|
|
} else {
|
|
|
|
console.log(data)
|
2024-02-19 10:57:50 -06:00
|
|
|
}
|
2024-02-07 21:49:28 -06:00
|
|
|
})
|
2024-02-19 10:57:50 -06:00
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
socket.send(JSON.stringify(lanyardData))
|
|
|
|
socket.send(`{ "op": 1 }`)
|
2024-04-21 13:11:57 -05:00
|
|
|
socket.send(`{"op": 4, "spins": ${violetSpins}}`)
|
2024-06-25 01:22:17 -05:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
sockets.push({ socket, lastPing: Date.now() })
|
2024-02-07 21:58:04 -06:00
|
|
|
})
|
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
server.listen(PORT, function () {
|
2024-02-07 21:58:04 -06:00
|
|
|
console.log("API & Websocket now on port: " + PORT)
|
2024-06-20 23:14:26 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err, origin) => {
|
|
|
|
fs.writeSync(
|
|
|
|
process.stderr.fd,
|
|
|
|
`Caught exception: ${err}\n` +
|
|
|
|
`Exception origin: ${origin}`,
|
|
|
|
);
|
|
|
|
});
|