-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-slash-commands.js
217 lines (190 loc) · 5.5 KB
/
add-slash-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
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
const fs = require('fs');
const APP_ID = process.env.DISCORD_APP_ID;
const BOT_TOKEN = process.env.DISCORD_APP_BOT_TOKEN;
const BASE_URL = 'https://discord.com/api/v10';
if (!APP_ID || !BOT_TOKEN) {
throw new Error('Missing env vars');
}
console.log('Reading servers.json');
const servers = JSON.parse(fs.readFileSync('./scripts/servers.json', 'utf8'));
const mapServersToGuildMap = (data) => {
const guildMap = {};
data.forEach((item, i) => {
const { region, discordGuildId, gameServerId, choiceDisplayName } = item;
if (!region || !discordGuildId || !gameServerId || !choiceDisplayName) {
throw new Error(
`Server ${i + 1}/${
data.length
} missing Region (${region}, Discord Guild ID (${discordGuildId}), game server ID (${gameServerId}) or/and choice display name (${choiceDisplayName})`
);
}
if (!guildMap[discordGuildId]) {
guildMap[discordGuildId] = [];
}
guildMap[discordGuildId].push({
region,
gameServerId,
choiceDisplayName,
});
});
return guildMap;
};
const guildMap = mapServersToGuildMap(servers);
console.log('Guild map', guildMap);
// ---------------------------------------------------------------------------
const listGuildCommands = async () => {
const res = await fetch(
`${BASE_URL}/applications/${APP_ID}/guilds/${GUILD_ID}/commands`,
{
headers: {
Authorization: 'Bot ' + BOT_TOKEN,
},
}
);
console.log('List', res.status, new URL(res.url).pathname);
console.log(JSON.stringify(await res.json(), null, ' '), '\n');
};
/**
* @param {string} id
*/
const deleteGuildCommand = async (id) => {
const res = await fetch(
`${BASE_URL}/applications/${APP_ID}/guilds/${GUILD_ID}/commands/${id}`,
{
method: 'DELETE',
headers: {
Authorization: 'Bot ' + BOT_TOKEN,
},
}
);
console.log('Delete', res.status, new URL(res.url).pathname);
console.log(JSON.stringify(await res.json(), null, ' '), '\n');
};
/**
* @param {import('../node_modules/discord-api-types/rest/v10').RESTPostAPIChatInputApplicationCommandsJSONBody} data
*/
const createGuildCommand = async (data) => {
const res = await fetch(
`${BASE_URL}/applications/${APP_ID}/guilds/${GUILD_ID}/commands`,
{
method: 'POST',
body: JSON.stringify(data),
headers: {
Authorization: 'Bot ' + BOT_TOKEN,
'Content-Type': 'application/json',
},
}
);
console.log(`Command ${data.name}:`, res.status, new URL(res.url).pathname);
console.log(JSON.stringify(await res.json(), null, ' '), '\n');
};
/**
* @param {import('../node_modules/discord-api-types/rest/v10').RESTPutAPIApplicationGuildCommandsJSONBody} data
*/
const createGuildCommandBulk = async (data, guildId) => {
const res = await fetch(
`${BASE_URL}/applications/${APP_ID}/guilds/${guildId}/commands`,
{
method: 'PUT',
body: JSON.stringify(data),
headers: {
Authorization: 'Bot ' + BOT_TOKEN,
'Content-Type': 'application/json',
},
}
);
console.log(
`Overwrite guild application commands:`,
res.status,
new URL(res.url).pathname
);
console.log(JSON.stringify(await res.json(), null, ' '), '\n');
};
// listGuildCommands();
// deleteGuildCommand(123);
// ---------------------------------------------------------------------------
console.log('\nCreating commands');
Object.entries(guildMap).forEach(([guildId, servers]) => {
const serverIdChoices = servers.map(
({ region, gameServerId, choiceDisplayName }) =>
/** @type {import('../node_modules/discord-api-types/payloads/v10').APIApplicationCommandOptionChoice} */
({
name: choiceDisplayName,
value: `${region}|${gameServerId}`,
})
);
const options = [
{
type: 3, // string
name: 'server',
name_localizations: {
'pt-BR': 'servidor',
},
description: 'Which server to run the command for',
description_localizations: {
'pt-BR': 'Para qual servidor executar o comando',
},
required: true,
choices: serverIdChoices,
},
];
createGuildCommandBulk(
[
{
type: 1,
name: 'start',
name_localizations: {
'pt-BR': 'iniciar',
},
description: 'Starts a server',
description_localizations: {
'pt-BR': 'Inicia um servidor',
},
options,
},
{
type: 1,
name: 'stop',
name_localizations: {
'pt-BR': 'parar',
},
description: "Stops a server, if it's online",
description_localizations: {
'pt-BR': 'Desliga um servidor, se estiver online',
},
options,
},
{
type: 1,
name: 'restart',
name_localizations: {
'pt-BR': 'reiniciar',
},
description: "Restarts a server, if it's online",
description_localizations: {
'pt-BR': 'Reinicia um servidor, se estiver online',
},
options,
},
{
type: 1,
name: 'ip',
description: 'Shows the server IP address',
description_localizations: {
'pt-BR': 'Exibe o endereço IP do servidor',
},
options,
},
{
type: 1,
name: 'status',
description: 'Shows server host status',
description_localizations: {
'pt-BR': 'Exibe o estado do host do servidor',
},
options,
},
],
guildId
).then(() => console.log('Done guild', guildId, '\n'));
});