-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
253 lines (229 loc) · 7.34 KB
/
api.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = 3000;
const DISCORD_BOT_TOKEN =
process.env.DISCORD_BOT_TOKEN ||
'paste_your_token_here'; // Discord Bot Token
app.use(express.static('public'));
app.use(cors()); // Enable CORS
app.get('/api/user/:id', async (req, res) => {
const userId = req.params.id;
try {
const userData = await fetchDiscordUserData(userId);
if (!userData) {
// User not found
return res.status(404).json({ error: 'User not found' });
}
const extendedUserData = processUserData(userData);
res.json(extendedUserData);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
async function fetchDiscordUserData(userId) {
try {
const response = await fetch(`https://discord.com/api/v10/users/${userId}`, {
headers: {
Authorization: `Bot ${DISCORD_BOT_TOKEN}`,
},
});
if (!response.ok) {
if (response.status === 404) {
// User not found
return null;
} else {
throw new Error('Failed to fetch user information');
}
}
return response.json();
} catch (error) {
throw new Error('Failed to fetch user information');
}
}
// Process Discord user data
function processUserData(userData) {
const isBot = userData.bot || false;
const isSystem = userData.system || false;
const userFlags = resolveUserFlags(userData.flags);
const nitroType = resolveNitroType(userData.premium_type);
const avatarUrl = userData.avatar
? getImageUrl('avatar', userData.id, userData.avatar, 'png', 4096)
: 'https://cdn.discordapp.com/embed/avatars/index.png';
const bannerUrl = userData.banner
? getImageUrl('banner', userData.id, userData.banner, 'png', 4096)
: null;
const userAvatarDecorationUrl = userData.avatar_decoration
? `https://cdn.discordapp.com/avatar-decorations/${userData.id}/${userData.avatar_decoration}.png`
: userData.avatar_decoration_data
? `https://cdn.discordapp.com/avatar-decoration-presets/${userData.avatar_decoration_data.asset}.png?4096`
: null;
const creationDate = getCreationDate(userData.id);
const creationDateFormatted = new Date(creationDate).toLocaleString();
return {
id: userData.id,
global_name: userData.global_name,
username: userData.username,
discriminator: userData.discriminator,
avatar: avatarUrl,
avatarDecoration: userAvatarDecorationUrl,
banner: bannerUrl,
accentColor: userData.accent_color,
bannerColor: userData.banner_color,
isBot,
isSystem,
flags: userFlags,
nitroType,
creationDate: creationDateFormatted,
};
}
// Get image URL
function getImageUrl(type, id, hash, format = 'png', size = 128) {
const baseUrl =
type === 'avatar'
? 'https://cdn.discordapp.com/avatars'
: 'https://cdn.discordapp.com/banners';
const isGif = hash.startsWith('a_');
const actualFormat = isGif ? 'gif' : format;
const path = `${id}/${hash}.${actualFormat}`;
return `${baseUrl}/${path}`;
}
// Resolve user flags
function resolveUserFlags(flags) {
const userFlags = [];
const defaultFlagsConfig = getDefaultFlagsConfig();
for (const [flagValue, flagData] of Object.entries(defaultFlagsConfig)) {
const intValue = parseInt(flagValue);
if (flags & intValue) {
userFlags.push({ value: intValue, ...flagData });
}
}
return userFlags;
}
// Get default flags configuration
function getDefaultFlagsConfig() {
return {
1: {
name: "STAFF",
description: "Discord Employee",
icon: "https://cdn.discordapp.com/badge-icons/5e74e9b61934fc1f67c65515d1f7e60d.png",
},
2: {
name: "PARTNER",
description: "Partnered Server Owner",
icon: "https://cdn.discordapp.com/attachments/1186583830820290597/1190245049355743292/6714-discord-partner.png",
},
4: {
name: "HYPESQUAD",
description: "HypeSquad Events Member",
icon: "https://cdn.discordapp.com/badge-icons/bf01d1073931f921909045f3a39fd264.png",
},
8: {
name: "BUG_HUNTER_LEVEL_1",
description: "Bug Hunter Level 1",
icon: "https://cdn.discordapp.com/badge-icons/2717692c7dca7289b35297368a940dd0.png",
},
64: {
name: "HYPESQUAD_ONLINE_HOUSE_1",
description: "House Bravery Member",
icon: "https://cdn.discordapp.com/badge-icons/8a88d63823d8a71cd5e390baa45efa02.png",
},
128: {
name: "HYPESQUAD_ONLINE_HOUSE_2",
description: "House Brilliance Member",
icon: "https://cdn.discordapp.com/badge-icons/011940fd013da3f7fb926e4a1cd2e618.png",
},
256: {
name: "HYPESQUAD_ONLINE_HOUSE_3",
description: "House Balance Member",
icon: "https://cdn.discordapp.com/badge-icons/3aa41de486fa12454c3761e8e223442e.png",
},
512: {
name: "PREMIUM_EARLY_SUPPORTER",
description: "Early Nitro Supporter",
icon: "https://cdn.discordapp.com/badge-icons/7060786766c9c840eb3019e725d2b358.png",
},
1024: {
name: "TEAM_PSEUDO_USER",
description: "User is a team",
icon: "icon-url-for-team-pseudo-user",
},
16384: {
name: "BUG_HUNTER_LEVEL_2",
description: "Bug Hunter Level 2",
icon: "icon-url-for-bug-hunter-level-2",
},
65536: {
name: "VERIFIED_BOT",
description: "Verified Bot",
icon: "https://cdn.discordapp.com/attachments/1186583830820290597/1190245723300708393/verified-bot.png",
},
131072: {
name: "VERIFIED_DEVELOPER",
description: "Early Verified Bot Developer",
icon: "https://cdn.discordapp.com/badge-icons/6df5892e0f35b051f8b61eace34f4967.png",
},
262144: {
name: "CERTIFIED_MODERATOR",
description: "Moderator Programs Alumni",
icon: "https://cdn.discordapp.com/badge-icons/fee1624003e2fee35cb398e125dc479b.png",
},
524288: {
name: "BOT_HTTP_INTERACTIONS",
description:
"Bot uses only HTTP interactions and is shown in the online member list",
icon: "icon-url-for-bot-http-interactions",
},
4194304: {
name: "ACTIVE_DEVELOPER",
description: "User is an Active Developer",
icon: "https://cdn.discordapp.com/badge-icons/6bdc42827a38498929a4920da12695d9.png",
},
};
}
// Resolve Nitro type
function resolveNitroType(premiumType) {
const premiumTypeNumber = parseInt(premiumType);
const nitroTypes = {
0: {
value: 0,
name: "None",
description: "No Nitro",
icon: "",
},
1: {
value: 1,
name: "Nitro Classic",
description: "Nitro Classic",
icon: "https://cdn.discordapp.com/badge-icons/2ba85e8026a8614b640c2837bcdfe21b.png",
},
2: {
value: 2,
name: "Nitro",
description: "Nitro",
icon: "https://cdn.discordapp.com/badge-icons/2ba85e8026a8614b640c2837bcdfe21b.png",
},
3: {
value: 3,
name: "Nitro Basic",
description: "Nitro Basic",
icon: "https://cdn.discordapp.com/badge-icons/2ba85e8026a8614b640c2837bcdfe21b.png",
},
};
return (
nitroTypes[premiumTypeNumber] || {
value: 0,
name: "Unknown",
description: "Unknown Nitro Type",
icon: "icon-url-for-unknown",
}
);
}
// Calculate Discord user creation date
function getCreationDate(snowflake) {
return snowflake / 4194304 + 1420070400000;
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});