-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
55 lines (47 loc) · 1.5 KB
/
utils.ts
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
import "@std/dotenv/load";
import { APIApplicationCommand } from "https://deno.land/x/[email protected]/v10.ts";
export async function DiscordRequest(
endpoint: string,
options: RequestInit,
): Promise<Response> {
const DISCORD_API_URL = Deno.env.get("DISCORD_API_URL");
const DISCORD_TOKEN = Deno.env.get("DISCORD_TOKEN");
if (!DISCORD_API_URL || !DISCORD_TOKEN) {
throw new Error(
"Missing DISCORD_API_URL or DISCORD_TOKEN environment variable",
);
}
const url = DISCORD_API_URL + endpoint;
if (options.body) options.body = JSON.stringify(options.body);
const res = await fetch(url, {
headers: {
Authorization: `Bot ${DISCORD_TOKEN}`,
"Content-Type": "application/json; charset=UTF-8",
"User-Agent":
"DiscordBot (https://github.com/discord/discord-example-app, 1.0.0)",
},
...options,
});
if (!res.ok) {
const data = await res.json();
console.error(`Discord API Error (${res.status}):`, data);
throw new Error(`Discord API Error: ${res.status} ${res.statusText}`);
}
return res;
}
export async function InstallGlobalCommands(
appId: string,
commands: Partial<APIApplicationCommand>[],
): Promise<void> {
const endpoint = `applications/${appId}/commands`;
try {
await DiscordRequest(endpoint, {
method: "PUT",
body: JSON.stringify(commands),
});
console.log("Successfully installed global commands");
} catch (err) {
console.error("Error installing global commands:", err);
throw err;
}
}