-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
124 lines (111 loc) · 3.13 KB
/
commands.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
require("dotenv/config");
const { program } = require("commander");
program
.version("1.0.0", "-v, --version")
.usage("[OPTIONS]...")
.option("-p, --prod", "Deploy commands to prod")
.option("-d, --dry-run", "Dry run: don't deploy any commands", false)
.parse(process.argv);
const options = program.opts();
const { prod, dryRun } = options;
// Fish command
const FISH_COMMAND = {
name: "fish",
description: "Go fishing",
type: 1,
};
const CATCHES_COMMAND = {
name: "catches",
description: "View your past fish catches",
type: 1,
};
const LEADERBOARD_COMMAND = {
name: "fishing-leaderboard",
description: "View the biggest catches",
type: 1,
options: [
{
type: 3, // STRING
name: "leaderboard",
description: "Which leaderboard to load",
required: true,
choices: [
{
name: "Server",
value: "server",
},
{
name: "Global",
value: "global",
},
],
},
],
};
const SERVER_LEADERBOARD_COMMAND = {
name: "server-fishing-leaderboard",
description: "View the biggest catches on this server",
type: 1,
};
const GLOBAL_LEADERBOARD_COMMAND = {
name: "global-fishing-leaderboard",
description: "View the biggest catches ever",
type: 1,
};
const ALL_COMMANDS = [
FISH_COMMAND,
CATCHES_COMMAND,
SERVER_LEADERBOARD_COMMAND,
GLOBAL_LEADERBOARD_COMMAND,
LEADERBOARD_COMMAND,
];
const ALL_COMMANDS_DEV = ALL_COMMANDS.map((command) => {
const devCommand = { ...command };
devCommand.name = `dev-${command.name}`;
return devCommand;
});
const appId = prod
? process.env.PROD_DISCORD_APP_ID
: process.env.DISCORD_APP_ID;
const token = prod ? process.env.PROD_DISCORD_TOKEN : process.env.DISCORD_TOKEN;
const commands = prod ? ALL_COMMANDS : ALL_COMMANDS_DEV;
if (!dryRun) {
InstallGlobalCommands(appId, commands, token);
} else {
console.log("Dry run commands to register:", commands);
}
/**** Functions from Discord. ****/
async function DiscordRequest(endpoint, options, token) {
// 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);
// Use node-fetch to make requests
const res = await fetch(url, {
headers: {
Authorization: `Bot ${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;
}
async function InstallGlobalCommands(appId, commands, token) {
// 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 }, token);
} catch (err) {
console.error(err);
}
}