DiscordEmbedder/index.js

135 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 13:01:53 -06:00
const express = require("express"),
path = require("path"),
fs = require("fs"),
fetch = require("node-fetch")
var app = express()
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)"
}
}
const PORT = process.env.PORT || 8080
app.use(express.static(path.join(__dirname, 'static')))
app.listen(PORT, () => {
console.log("Now listening on PORT: " + PORT)
})
app.get('/embedCard', async (req, res) => {
var discID = req.query.discordid
2024-01-07 02:23:18 -06:00
var server = 'https://' + (req.query.server || 'api.lanyard.rest')
2023-12-18 13:01:53 -06:00
res.header("Content-Type", "text/html")
var html = fs.readFileSync(path.join(__dirname, 'resources/embedCard.html')).toString()
2024-01-07 02:23:18 -06:00
var result = (await fetch(`${server}/v1/users/${discID}`).then(response => response.json())).data
2023-12-18 13:01:53 -06:00
var user = result.discord_user
var replacers = {
"DISPLAY_NAME": user.display_name || user.global_name,
"USERNAME": user.username,
"DISCORD_ID": user.id,
"STATUS": statuses[result.discord_status].text,
"STATUS_COLOR": statuses[result.discord_status].color,
"CUSTOM_STATUS": ""
}
if (result && result.activities.length > 0) {
if (result.activities[0].type == 4) {
replacers.CUSTOM_STATUS = `Custom Status: "${result.activities[0].state}"`
}
}
if (user.discriminator != 0) {
replacers.USERNAME += user.discriminator
}
for (let index = 0; index < Object.keys(replacers).length; index++) {
const key = Object.keys(replacers)[index]
const val = replacers[key];
while (html.includes("{" + key + "}")) {
html = html.replace("{" + key + "}", val)
}
}
res.send(html)
})
app.get('/getUser', async (req, res) => {
var discID = req.query.discordid
2024-01-07 02:23:18 -06:00
var server = 'https://' + (req.query.server || 'api.lanyard.rest')
2023-12-18 13:01:53 -06:00
res.header("Content-Type", "text/html")
var html = fs.readFileSync(path.join(__dirname, 'resources/userPage.html')).toString()
2024-01-07 02:23:18 -06:00
try {
var data = (await fetch(`${server}/v1/users/${discID}`).then(response => response.json()))
} catch (error) {
res.send(`
<link rel="stylesheet" href="./style.css">
<h1>Error!</h1>
<p>Code: ${error}<br>
Try clearing cache and trying again.
</p>
`)
return
}
2023-12-18 15:38:09 -06:00
var result = data.data
if (data.error) {
res.send(`
<link rel="stylesheet" href="./style.css">
<h1>Error!</h1>
<p>Code: ${data.error.code}<br>
Message: ${data.error.message}<br>
Double check the main page to ensure you've set things up right!
</p>
`)
return
}
2023-12-18 13:01:53 -06:00
var user = result.discord_user
var replacers = {
"DISPLAY_NAME": user.display_name || user.global_name,
"USERNAME": user.username,
"DISCORD_ID": user.id,
"STATUS": user.discord_status,
2024-01-07 02:23:18 -06:00
"BASE_LINK": "localhost:8080/"
2023-12-18 13:01:53 -06:00
}
if (user.discriminator != 0) {
replacers.USERNAME += "#" + user.discriminator
}
for (let index = 0; index < Object.keys(replacers).length; index++) {
const key = Object.keys(replacers)[index]
const val = replacers[key];
while (html.includes("{" + key + "}")) {
html = html.replace("{" + key + "}", val)
}
}
res.send(html)
})