-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
81 lines (69 loc) · 2.1 KB
/
Copy pathutils.js
File metadata and controls
81 lines (69 loc) · 2.1 KB
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
import * as https from "node:https";
export async function DiscordRequest(endpoint, options) {
const url = `https://discord.com/api/v10/${endpoint}`;
const isFormData = options.body instanceof FormData;
const body = options.body && !isFormData
? JSON.stringify(options.body)
: options.body;
const headers = {
Authorization: `Bot ${process.env.DISCORD_TOKEN}`,
// FIXME: Would be nice to source this from package.json if we ever bother using versions.
'User-Agent': 'BrickBot/1.0',
};
// fetch() automatically sorts this out for FormData.
if (!isFormData) {
headers['Content-Type'] = 'application/json; charset=UTF-8';
}
const res = await fetch(url, {
// NB: options comes first, because it still has a `body` field that we want to be overridden.
...options,
headers,
body,
});
if (!res.ok) {
const data = await res.json();
console.log(res.status);
throw new Error(JSON.stringify(data));
}
return res;
}
export async function InstallGlobalCommands(appId, commands) {
// API endpoint to overwrite global commands
const endpoint = `applications/${appId}/commands`;
console.log(`Registering ${commands.length} 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 });
console.log(`Done!`);
} catch (err) {
console.error(err);
}
}
export async function getJSON(url) {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
response.on("end", () => {
try {
resolve(JSON.parse(data));
} catch (error) {
reject(error);
}
});
}).on("error", (error) => {
reject(error);
});
})
}
export function sortObjectByKeys(unordered) {
return Object.keys(unordered).sort().reduce(
(obj, key) => {
obj[key] = unordered[key];
return obj;
},
{}
);
}