diff --git a/index.js b/index.js index a425ee8..9b44799 100644 --- a/index.js +++ b/index.js @@ -491,6 +491,28 @@ app.get("/api/v1/directory", (req, res) => { }); }); +// Other public endpoints that need special handling +discordInviteCache = {time: 0, url: ""}; + +app.get("/discord", (req, res) => { + // fetch from process.env.WIDGET_URL, get json body, redirect to body.instant_invite. Cache url for 5 minutes + if (Date.now() - discordInviteCache.time < 300000) { + res.redirect(discordInviteCache.url); + return; + } + fetch(process.env.WIDGET_URL) + .then(response => response.json()) + .then(data => { + discordInviteCache.time = Date.now(); + discordInviteCache.url = data.instant_invite; + res.redirect(data.instant_invite); + }) + .catch(error => { + console.error('Error fetching discord invite:', error); + res.status(500).send('Internal server error'); + }); +}); + // Query to get a route app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => { const apiKey = req.params.apiKey;