-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (84 loc) · 3.43 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require("dotenv").config()
const express = require("express")
const app = express()
const { QuickDB, SqliteDriver } = require("quick.db");
const sqliteDriver = new SqliteDriver("./data.db")
const db = new QuickDB({ driver: sqliteDriver });
const path = require("path");
const { verifyKeyMiddleware, InteractionType, InteractionResponseType } = require("discord-interactions");
async function getAvatar(userId) {
let u = await db.get(userId)
if (u && u.lastFetched && (u.lastFetched + 1000 * 60 * 60 * 24) > Date.now() && u.avatar) return u.avatar
u = { lastFetched: Date.now(), avatar: null }
const res = await fetch(`https://discord.com/api/users/${userId}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bot ${process.env.TOKEN}`
}
})
if (res.status !== 200) return null
const userJSON = await res.json()
if (!userJSON?.avatar) {
db.set(userId, u)
return null
}
u.avatar = userJSON.avatar
db.set(userId, u)
return userJSON.avatar
}
app.get("/", async (req, res) => {
res.sendFile(path.resolve(__dirname, "home.html"))
})
app.get("/bot", async (req, res) => {
res.redirect("https://discord.com/oauth2/authorize?client_id=1206787663584165898")
})
app.post("/bot", verifyKeyMiddleware(process.env.PUBLIC_KEY), async (req, res) => {
const interaction = req.body;
if (interaction.type != InteractionType.APPLICATION_COMMAND) return
if (interaction.data.name == "ava") {
const userId = interaction.data.options.find(o => o.name == "user").value
const format = interaction.data.options.find(o => o.name == "format")?.value
const size = interaction.data.options.find(o => o.name == "size")?.value
let url = `https://ava.viadev.xyz/${userId}`
if (format) url += `.${format}`
if (size) url += `?size=${size}`
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `${url} .`,
},
})
}
if (interaction.data.name == "emoji") {
const parts = interaction.data.options.find(o => o.name == "emoji").value.split(":")
const id = parts[2]
if (id == undefined || id.length < 9) return
return res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `https://cdn.discordapp.com/emojis/${id.slice(0, id.length - 1)}${parts[0] == "<a" ? ".gif" : ""} .`,
},
})
}
})
app.get("/:id", async (req, res) => {
let [userId, format] = req.params.id.split(".")
if (!format) format = null
if (req.query.debug === "true") {
let u = await db.get(userId)
if (!u) return res.send("null")
return res.send(`${u.avatar}<br>${new Date(u.lastFetched).toString()}`)
}
const size = req.query.size || 256
const $f = (url) => {
if (format) url += `.${format}`
if (size) url += `?size=${size}`
return url
}
if (!RegExp("[0-9]{18,19}").test(userId.trim())) return res.redirect(`https://cdn.discordapp.com/embed/avatars/5.${format || "png"}?size=${size}`)
const avatar = await getAvatar(userId)
if (!avatar) return res.redirect(`https://cdn.discordapp.com/embed/avatars/5.${format || "png"}?size=${size}`)
else res.redirect($f(`https://cdn.discordapp.com/avatars/${userId}/${avatar}`))
})
app.listen(3002)