-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (44 loc) · 1.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require('dotenv').config()
const express = require('express')
const fetch = require('node-fetch')
const app = express()
const cacheTime = 5 * 60 * 60 * 1000
const cache = {}
app.get('/', (req, res) => {
res.contentType('text/plain')
res.send(`
Davatar
-------
Davatar is a service to find Discord avatars.
Cache time is ${cacheTime}ms
GET /:id -> a PNG image
`.trim())
})
app.get('/:id', async (req, res) => {
res.type('png')
if (cache[req.params.id]) {
const cached = cache[req.params.id]
if (Date.now() - cached.time < cacheTime) {
res.end(cached.buffer)
return
}
}
const ures = await fetch(`https://discordapp.com/api/users/${req.params.id}`, {
headers: {
'Authorization': `Bot ${process.env.BOT_TOKEN}`
}
})
if (!ures.ok) {
res.sendStatus(ures.status)
return
}
const json = await ures.json()
const ares = await fetch(`https://cdn.discordapp.com/avatars/${req.params.id}/${json.avatar}.png`)
const buffer = await ares.buffer()
cache[req.params.id] = {
time: Date.now(),
buffer
}
res.end(buffer)
})
app.listen(process.env.PORT ? parseInt(process.env.PORT) : 3123, () => console.log('> Listening on http://localhost:3000/'))