From 0fb3204df1a3da58ff13532c95eb1111a4b02f41 Mon Sep 17 00:00:00 2001 From: Bingus_Violet Date: Mon, 12 Feb 2024 08:46:49 -0600 Subject: [PATCH] Disc Status --- config.json | 19 +++++- index.js | 108 +----------------------------- pageUpdater.js | 166 ++++++++++++++++++++++++++++++++++------------ static/index.html | 69 ++++++++++--------- 4 files changed, 177 insertions(+), 185 deletions(-) diff --git a/config.json b/config.json index 78ac5f0..f718e16 100644 --- a/config.json +++ b/config.json @@ -2,7 +2,24 @@ "activityImages": { "ULTRAKILL": "https://fs.violets-purgatory.dev/ULTRAKILL/etc/DiscordIcon.webp" }, - + "discStatuses": { + "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": "", + "color": "rgb(175, 175, 200)" + } + }, "quotes": [ "Remember the 14th commandment: Thou shalt always clean thy plate and not waste anything, whether thy stomach is full, or not.", "Remember son, dying is gay!", diff --git a/index.js b/index.js index d4c9e6c..645bdef 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,6 @@ const express = require('express'), path = require('path'), fs = require('fs'), - WebSocket = require('ws'), - minify = require('minify-html'), pageUpdater = require('./pageUpdater.js') var app = express() @@ -10,23 +8,13 @@ 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 config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'))) var thumborInstances = config.thumborInstances -var activityImages = config.activityImages - -var highlight = config.highlightedWords - -var uptime = Date.now() -var lastLanyardUpdate = Date.now() -var lastPong = Date.now() - var thumbCount = 0 function getThumbor() { @@ -38,8 +26,6 @@ app.listen(PORT, () => { console.log("Violet's Purgatory is now listening on port: " + PORT) }) -var cachedImages = {} - if (!fs.existsSync(path.join(staticpath, 'cached'))) { fs.mkdirSync(path.join(staticpath, 'cached')) } @@ -48,31 +34,6 @@ var randomQuotes = config.quotes var commitCount = "300+" -function get_img_url(activity, size = "large_image") { - - if ("assets" in activity) { - var image = activity.assets[size] - - if (image) { - if (image.includes("https/")) { - return decodeURIComponent('https://' + image.substr(image.indexOf('https/') + 6, image.length)) - } else if (image.includes("spotify")) { - return decodeURIComponent('https://i.scdn.co/image/' + image.substr(image.indexOf('spotify:') + 8, image.length)) - } else { - return decodeURIComponent(`https://cdn.discordapp.com/app-assets/${activity.application_id}/${image}.png`) - } - } - } - - if (!image) { - if (activity.name in activityImages) { - return decodeURIComponent(activityImages[activity.name]) - } else { - return null - } - } -} - function timeFormatter(seconds) { seconds = Math.ceil(seconds) var minutes = Math.floor(seconds / 60) @@ -99,71 +60,4 @@ function gameTimeFormatter(seconds) { } -app.use(pageUpdater.middleWare) - -// Lanyard Stuffs - -function socketeer() { - var lanyard = new WebSocket('https://api.violets-purgatory.dev') - function ping(dur) { - lanyard.send(JSON.stringify({ - op: 3 - })) - setTimeout(() => { - ping(dur) - if (Date.now() - lastPong > 120000) { - console.log("FUCK!") - lanyard.close() - socketeer() - } - }, dur); - } - - lanyard.addEventListener("message", async (res) => { - var data = JSON.parse(res.data) - if (data.op == 1) { - ping(30000) - lastPong = Date.now() - } else if (data.op == 3) { - lastPong = Date.now() - } else if (data.op == 0) { - lanyardData = data.d - lastLanyardUpdate = Date.now() - - for (let index = 0; index < lanyardData.activities.length; index++) { - const activity = lanyardData.activities[index]; - - if (get_img_url(activity)) { - var url = get_img_url(activity) - var fn = Math.ceil(Math.random() * 100_000_000_000).toString() - var fp = path.join(__dirname, 'static/cached', fn) - - if (!cachedImages[url]) { - const response = await (await fetch(url)).arrayBuffer() - - fs.writeFileSync(fp, Buffer.from(response)) - - cachedImages[url] = fn - } - } - - if (get_img_url(activity, "small_image")) { - var url = get_img_url(activity, "small_image") - var fn = Math.ceil(Math.random() * 100_000_000_000).toString() - var fp = path.join(__dirname, 'static/cached', fn) - - if (!cachedImages[url]) { - const response = await (await fetch(url)).arrayBuffer() - - fs.writeFileSync(fp, Buffer.from(response)) - - cachedImages[url] = fn - } - } - } - - } - }) -} - -socketeer() \ No newline at end of file +app.use(pageUpdater.middleWare) \ No newline at end of file diff --git a/pageUpdater.js b/pageUpdater.js index 2918eef..6afd500 100644 --- a/pageUpdater.js +++ b/pageUpdater.js @@ -1,5 +1,7 @@ const path = require('path'), - fs = require('fs') + fs = require('fs'), + WebSocket = require('ws'), + minify = require('minify-html') var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'))) @@ -9,51 +11,32 @@ var titles = config.titles var commitCount = "300+" -function makeStars() { - var html = "" +var lanyardData = undefined - for (let index = 0; index < 35; index++) { - html += `
` - } - html += "" - - return html -} +var uptime = Date.now() function converter(html) { - html = html.replaceAll("{BG_EFFECT}", makeStars()) + if (lanyardData) { + var statusData = config.discStatuses[lanyardData.discord_status] + } else { + var statusData = config.discStatuses.offline + } + var replacers = { + "COMMIT_COUNT": commitCount, + "RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)], + "QUOTE_COUNT": quotes.length, + "RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)], + "DISCORD_STATUS": + `${statusData.text}` + + `` + } - html = html.replaceAll("{COMMIT_COUNT}", commitCount) + var rpTable = Object.keys(replacers) - html = html.replaceAll("{RANDOM_QUOTE}", quotes[Math.floor(Math.random() * quotes.length)]) - - html = html.replaceAll("{QUOTE_COUNT}", quotes.length) - - html = html.replaceAll("{RANDOM_TITLE}", titles[Math.floor(Math.random() * titles.length)]) + for (let index = 0; index < rpTable.length; index++) { + const text = rpTable[index]; + html = html.replaceAll(`{${text}}`, replacers[text]) + } var highTable = Object.keys(highlightedWords) for (let index = 0; index < highTable.length; index++) { @@ -99,4 +82,103 @@ async function updateCommits() { commitCount = commits } -updateCommits() \ No newline at end of file +updateCommits() + +// Lanyard Stuffs + +var lastLanyardUpdate = Date.now() +var lastPong = Date.now() + +var activityImages = config.activityImages +var cachedImages = {} + +function get_img_url(activity, size = "large_image") { + + if ("assets" in activity) { + var image = activity.assets[size] + + if (image) { + if (image.includes("https/")) { + return decodeURIComponent('https://' + image.substr(image.indexOf('https/') + 6, image.length)) + } else if (image.includes("spotify")) { + return decodeURIComponent('https://i.scdn.co/image/' + image.substr(image.indexOf('spotify:') + 8, image.length)) + } else { + return decodeURIComponent(`https://cdn.discordapp.com/app-assets/${activity.application_id}/${image}.png`) + } + } + } + + if (!image) { + if (activity.name in activityImages) { + return decodeURIComponent(activityImages[activity.name]) + } else { + return null + } + } +} + + +function socketeer() { + var lanyard = new WebSocket('https://api.violets-purgatory.dev') + function ping(dur) { + lanyard.send(JSON.stringify({ + op: 3 + })) + setTimeout(() => { + ping(dur) + if (Date.now() - lastPong > 120000) { + console.log("FUCK!") + lanyard.close() + socketeer() + } + }, dur); + } + + lanyard.addEventListener("message", async (res) => { + var data = JSON.parse(res.data) + if (data.op == 1) { + ping(30000) + lastPong = Date.now() + } else if (data.op == 3) { + lastPong = Date.now() + } else if (data.op == 0) { + lanyardData = data.d + lastLanyardUpdate = Date.now() + + for (let index = 0; index < lanyardData.activities.length; index++) { + const activity = lanyardData.activities[index]; + + if (get_img_url(activity)) { + var url = get_img_url(activity) + var fn = Math.ceil(Math.random() * 100_000_000_000).toString() + var fp = path.join(__dirname, 'static/cached', fn) + + if (!cachedImages[url]) { + const response = await (await fetch(url)).arrayBuffer() + + fs.writeFileSync(fp, Buffer.from(response)) + + cachedImages[url] = fn + } + } + + if (get_img_url(activity, "small_image")) { + var url = get_img_url(activity, "small_image") + var fn = Math.ceil(Math.random() * 100_000_000_000).toString() + var fp = path.join(__dirname, 'static/cached', fn) + + if (!cachedImages[url]) { + const response = await (await fetch(url)).arrayBuffer() + + fs.writeFileSync(fp, Buffer.from(response)) + + cachedImages[url] = fn + } + } + } + + } + }) +} + +socketeer() \ No newline at end of file diff --git a/static/index.html b/static/index.html index 43f4812..97bf06d 100644 --- a/static/index.html +++ b/static/index.html @@ -2,7 +2,7 @@ - + @@ -26,45 +26,44 @@ {COMMIT_COUNT}

{RANDOM_QUOTE}

-
-

Violet

-
- -
-

They/Them

-

Developer

-

{DISCORD_STATUS}

-
-
+
+

Violet

+
+
-
- - -

FileShare

-

Socials

-

Projects

-

Donate -

- - -
- -

Blog

-

FAQ

-

Dev Branch

- -
-

Hi! I'm Violet, a 15 year old web and game developer.
- I aspire to make fast and javascript free websites! I'm currently learning the Godot Engine, and have been - becoming quite fluent in NodeJS.

+

They/Them

+

Developer

+

{DISCORD_STATUS}

-

Although my site is pretty barebones at the moment due to a recent rewrite, I hope to continue with this +

+
+ + +

Donate

+ +

FileShare

+ + + + + + + +

Blog

+ +

Dev Branch

+ +
+

Hi! I'm Violet, a 15 year old web and game developer.
+ I aspire to make fast and javascript free websites! I'm currently learning the Godot Engine, and + have been + becoming quite fluent in NodeJS.

+
+
+

Although my site is pretty barebones at the moment due to a recent rewrite, I hope to continue with this passion and update this website along with it.

-

As is probably very obvious, I'm not great at graphic design at the moment, but I hope to improve upon that - as time goes on.
I'm sure you can tell I have a pretty low self esteem too... -