const express = require('express'), path = require('path'), fs = require('fs'), WebSocket = require('ws') var app = express() const PORT = process.env.PORT || 8080 const staticpath = path.join(__dirname, 'static') const resourcePath = path.join(__dirname, "resources") const mainpage = resourcePath + '/mainPage.html' var lanyardData = undefined var discData = null app.use(express.static(staticpath)) app.listen(PORT, () => { console.log("Violet's Purgatory is now listening on port: " + PORT) }) function pageUpdate() { console.log("Updating...") 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 html = fs.readFileSync(mainpage).toString() var addedHTML = "" if (lanyardData) { var statusData = statuses[lanyardData.discord_status] addedHTML += `

${statusData.text}

` addedHTML += `` } html = html.replace("{LANYARD_STATUS}", addedHTML) addedHTML = "" if (lanyardData && lanyardData.activities.length > 0) { if (lanyardData.activities[0].type == 4) { addedHTML += `

"${lanyardData.activities[0].state}" - ${lanyardData.discord_user.display_name} ${new Date(Date.now()).getFullYear()}

` } } html = html.replace("{LANYARD_QUOTE}", addedHTML) addedHTML = "" if (lanyardData) { for (let index = 0; index < lanyardData.activities.length; index++) { const activity = lanyardData.activities[index]; if (activity.type == 4) { addedHTML += `

"${lanyardData.activities[0].state}" - ${lanyardData.discord_user.display_name} ${new Date(Date.now()).getFullYear()}

` } } } html = html.replace("{LANYARD_SPOTIFY}", addedHTML) addedHTML = "" var debounce = false if (lanyardData && lanyardData.activities.length > 0) { for (let index = 0; index < lanyardData.activities.length; index++) { const activity = lanyardData.activities[index]; if (!debounce && activity.type != 4) { addedHTML += `


What I'm up to:

This section is pulled automatically from my discord. If Discord is down, or i'm offline, it won't be here.
Huge credit to Phineas' Lanyard for making this possible :D
(This is extremely experimental, so PLEASE report any issues on Codeberg!

` debounce = true } function get_img() { if ("assets" in activity) { var image = undefined if ("large_image" in activity.assets) { image = activity.assets.large_image } else if ("small_image" in activity.assets) { image = activity.assets.small_image } if (image) { if (image.includes("https/")) { return 'https://' + image.substr(image.indexOf('https/') + 6, image.length) } else if (image.includes("spotify")) { return 'https://i.scdn.co/image/' + image.substr(image.indexOf('spotify:') + 8, image.length) } } } } if (activity.type == 2) { if (get_img()) { addedHTML += `

Listening to ${activity.name}
Song: "${activity.details}"
Album: "${activity.assets.large_text}"
Artist: "${activity.state}"

` } else { addedHTML += `

Playing ${activity.name}
${activity.state}
${activity.details}

` } } else if (activity.type == 0) { if (get_img()) { addedHTML += `

Playing ${activity.name}
${activity.state || activity.assets.small_text}
${activity.details || activity.assets.large_text}

` } else { addedHTML += `

Playing ${activity.name}
${activity.state}
${activity.details}

` } } } } if (addedHTML) { addedHTML += "
" } html = html.replace("{LANYARD_FULL}", addedHTML) addedHTML = "" var socialsHTML = fs.readFileSync(path.join(__dirname, 'static/socials/index.html')).toString() addedHTML += socialsHTML.substr(socialsHTML.indexOf("

"), socialsHTML.indexOf("")) html = html.replace("{SOCIALS}", addedHTML) fs.writeFileSync(path.join(__dirname, 'static/index.html'), html) console.log("Updated!") } // 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 pageUpdate() } else { console.log(data.d) } })