-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (77 loc) · 2.75 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
const Discord = require("discord.js")
const dotenv = require("dotenv")
const express = require("express")
const { GenerateBadge, GenerateBadgeWithoutStatus, GenerateJSON } = require("./badge")
const app = express()
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildPresences, Discord.GatewayIntentBits.GuildMembers] })
dotenv.config()
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
client.login(process.env.TOKEN)
async function GetUser(id, callback) {
const user = await (await fetch("https://discord.com/api/v10/users/" + id, { headers: { "Authorization": "Bot " + process.env.TOKEN } })).json()
var itemsProcessed = 0;
await client.guilds.cache.get(process.env.GUILD_ID).presences.cache.forEach(element => {
itemsProcessed++;
if (element.userId == id) {
callback({ username: user.global_name || element.user.tag, status: element.status })
itemsProcessed = 0
}
if (itemsProcessed === client.guilds.cache.get(process.env.GUILD_ID).memberCount) {
callback(null);
}
})
}
app.get("/badge/status/:id", async (req, res) => {
GetUser(req.params.id, async (data) => {
if (!data) {
res.status(404).send("Sorry... We couldn't find this user. Please verify that you have joined the following Discord server: https://discord.gg/FM8MxRra9P. If the problem persists, please open an issue at https://github.com/Av32000/Discord-Badge/issues")
return
}
let sint = 5
switch (data.status) {
case "online":
sint = 0
break;
case "idle":
sint = 1
break;
case "dnd":
sint = 2
break
case "offline":
sint = 3
break
}
let canva = null
if (sint == 5) {
canva = await GenerateBadgeWithoutStatus(data.username)
} else {
if (req.query.json == "true") {
return res.send(GenerateJSON(data.username, sint))
}
canva = await GenerateBadge(data.username, sint)
}
canva.createPNGStream().pipe(res)
})
})
app.get("/badge/:id", async (req, res) => {
GetUser(req.params.id, async (data) => {
if (!data) {
res.send("Sorry... We couldn't find this user. Please verify that you have joined the following Discord server: https://discord.gg/FM8MxRra9P. If the problem persists, please open an issue at https://github.com/Av32000/Discord-Badge/issues")
return
}
let canva = await GenerateBadgeWithoutStatus(data.username)
canva.createPNGStream().pipe(res)
})
})
app.get("/", (req, res) => {
res.redirect("https://github.com/Av32000/Discord-Badge")
})
app.all('*', function (req, res) {
res.sendStatus(404);
});
app.listen(8080, () => {
console.log("Github Discord Badge listening on port 8080...")
})