Skip to content

Commit 76614c4

Browse files
committed
feat(discord): better embeds
1 parent 5e251b3 commit 76614c4

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

app.js

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ app.use(express.json());
1616
app.use(express.urlencoded({ extended: false }));
1717
app.use(express.static(path.join(__dirname, 'public')));
1818

19+
app.use(function(req, res, next) {
20+
req.getUrl = function() {
21+
return req.protocol + "://" + req.get('host') + req.originalUrl;
22+
}
23+
return next();
24+
});
25+
1926
app.use('/', indexRouter);
2027

2128
// catch 404 and forward to error handler

routes/index.js

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ router.get('/', function(req, res, next) {
1010
router.get('/record/:ownerId/:recordId', (req,res, next) => handle("world", req, res, next));
1111
router.get('/session/:sessionId', (req,res, next) => handle("session", req, res, next));
1212

13+
router.get('/record/:ownerId/:recordId/json', (req,res, next) => handleJson("world", req, res, next));
14+
router.get('/session/:sessionId/json', (req,res, next) => handleJson("session", req, res, next));
15+
1316
function getUrl(type, req) {
1417
switch (type) {
1518
case "world":
@@ -45,15 +48,43 @@ async function handle(type, req, res, next) {
4548
}
4649

4750
json = preProcess(json);
51+
json.urlPath = req.getUrl();
4852

4953
res.status(200).render(type, json);
5054
}
5155

56+
async function handleJson(type, req, res, next) {
57+
var apiResponse = await fetch(getUrl(type, req));
58+
if (!apiResponse.ok) {
59+
res.status(apiResponse.status);
60+
return next();
61+
}
62+
63+
var json = await apiResponse.json();
64+
65+
if (type ==="world" && json.recordType !== "world") {
66+
res.status(400);
67+
return next();
68+
}
69+
70+
json = preProcess(json);
71+
72+
res.json({
73+
title: json.name,
74+
author_name: json.name,
75+
author_url: req.getUrl().replace("/json",""),
76+
provider_name: "Resonite",
77+
provider_url: "https://resonite.com"
78+
});
79+
80+
}
81+
5282
function preProcessName(name) {
5383
const start = /<color="?(.+?)"?>/gi;
5484
const end = /<\/color>/gi
5585
return name.replace(start, "<span style=\"color: $1 ;\">").replace(end, "</span>");
5686
}
87+
5788
function preProcess(json) {
5889
json.name = preProcessName(json.name);
5990

views/layout.pug

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ html(lang="en")
44
title #{title}
55
meta(charset='utf-8')
66
meta(name='viewport', content='width=device-width, initial-scale=1')
7+
meta(property="og:site_name" content="Resonite")
78
link(rel='stylesheet' href='https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css')
89
link(rel='stylesheet' href='/stylesheets/style.css')
910
link(rel="preconnect" href="https://api.resonite.com")

views/session.pug

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ block head
44
meta(property="og:title" content=name)
55
meta(property="og:type" content="resonite.session")
66
meta(property="og:image" content=thumbnailUrl)
7+
meta(name="twitter:card" content="summary_large_image")
8+
link(type="application/json+oembed" href=urlPath + "/json")
9+
710

811
block content
912
h1 !{name}

views/world.pug

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ block head
44
meta(property="og:title" content=name)
55
meta(property="og:type" content="resonite.world")
66
meta(property="og:image" content=thumbnailUri)
7+
meta(name="twitter:card" content="summary_large_image")
8+
link(type="application/json+oembed" href=urlPath + "/json")
79

810
block content
911
h1 !{name}

0 commit comments

Comments
 (0)