-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.js
115 lines (105 loc) · 3.51 KB
/
utils.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
import 'dotenv/config';
import { verifyKey } from 'discord-interactions';
import { getFakeUsername } from './game.js';
export function VerifyDiscordRequest(clientKey) {
return function (req, res, buf) {
const signature = req.get('X-Signature-Ed25519');
const timestamp = req.get('X-Signature-Timestamp');
console.log(signature, timestamp, clientKey);
const isValidRequest = verifyKey(buf, signature, timestamp, clientKey);
if (!isValidRequest) {
res.status(401).send('Bad request signature');
throw new Error('Bad request signature');
}
};
}
export async function DiscordRequest(endpoint, options) {
// append endpoint to root API URL
const url = 'https://discord.com/api/v10/' + endpoint;
// Stringify payloads
if (options.body) options.body = JSON.stringify(options.body);
const res = await fetch(url, {
headers: {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
'Content-Type': 'application/json; charset=UTF-8',
'User-Agent':
'DiscordBot (https://github.com/discord/discord-example-app, 1.0.0)',
},
...options,
});
// throw API errors
if (!res.ok) {
const data = await res.json();
console.log(res.status);
throw new Error(JSON.stringify(data));
}
// return original response
return res;
}
export async function InstallGlobalCommands(appId, commands) {
// API endpoint to overwrite global commands
const endpoint = `applications/${appId}/commands`;
try {
// This is calling the bulk overwrite endpoint: https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-global-application-commands
await DiscordRequest(endpoint, { method: 'PUT', body: commands });
} catch (err) {
console.error(err);
}
}
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export async function getServerLeaderboard(guildId) {
let members = await getServerMembers(guildId, 3);
members = members
.map((id, i) => `${i + 1}. <@${id}> (\`${getFakeUsername(i)}\`)`)
.join('\n');
return `## :trophy: Server Leaderboard\n*This is a very fake leaderboard that just pulls random server members. Pretend it's pulling real game data and it's much more fun* :zany_face:\n\n### This week\n${members}\n\n### All time\n${members}`;
}
async function getServerMembers(guildId, limit) {
const endpoint = `guilds/${guildId}/members?limit=${limit}`;
try {
const res = await DiscordRequest(endpoint, { method: 'GET' });
const parsedRes = await res.json();
return parsedRes.map((member) => member.user.id);
} catch (err) {
return console.error(err);
}
}
export function createPlayerEmbed(profile) {
return {
type: 'rich',
title: `${profile.username} Profile (lvl ${profile.stats.level})`,
color: 0x968b9f,
fields: [
{
name: `Account created`,
value: profile.createdAt,
inline: true,
},
{
name: `Last played`,
value: profile.lastPlayed,
inline: true,
},
{
name: `Global rank`,
value: profile.stats.rank,
inline: true,
},
{
name: `Combat stats`,
value: `:smiley: ${profile.stats.wins} wins / :pensive: ${profile.stats.losses} losses`,
},
{
name: `Realms explored`,
value: profile.stats.realms,
inline: true,
},
],
url: 'https://discord.com/developers/docs/intro',
thumbnail: {
url: 'https://raw.githubusercontent.com/shaydewael/example-app/main/assets/fake-icon.png',
},
};
}