-
Notifications
You must be signed in to change notification settings - Fork 0
/
register-commands.ts
79 lines (66 loc) · 2.28 KB
/
register-commands.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
RouteLike,
Routes
} from 'discord.js';
import * as dotenv from 'dotenv';
import { AccountCommand } from './src/commands/account.js';
import { BotInfoCommand } from './src/commands/bot-info.js';
import { HelpCommand } from './src/commands/help.js';
import { HelpstartCommand } from './src/commands/helpstart.js';
import { RetryCommand } from './src/commands/retry.js';
import { CancellAllCommand } from './src/commands/cancel-all.js';
dotenv.config();
if (!process.env.TOKEN) {
throw new Error('No token was found in the dotenv config');
}
if (!process.env.CLIENT_ID) {
throw new Error('No client ID was found in the dotenv config');
}
const rest = new REST().setToken(process.env.TOKEN);
const clientId = process.env.CLIENT_ID;
const guildId = process.env.GUILD_ID;
const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
commands.push(AccountCommand.data.toJSON());
commands.push(BotInfoCommand.data.toJSON());
commands.push(CancellAllCommand.data.toJSON());
commands.push(HelpCommand.data.toJSON());
commands.push(HelpstartCommand.data.toJSON());
commands.push(RetryCommand.data.toJSON());
function createDeleteCommandLogMessage(): string {
if (guildId) {
return `Deleting all guild Slash Commands from ${guildId}`;
}
return `Deleting all global Slash Commands`;
}
function createRegisterCommandLogMessage(): string {
if (guildId) {
return `Registering ${commands.length} guild Slash Commands to ${guildId}`;
}
return `Registering ${commands.length} global Slash Commands`;
}
function createRoute(): RouteLike {
if (guildId) {
return Routes.applicationGuildCommands(clientId, guildId);
}
return Routes.applicationCommands(clientId);
}
console.log(createDeleteCommandLogMessage());
const deleteResult = await rest.put(createRoute(), {
body: []
});
if (deleteResult instanceof Array) {
console.log('Slash Commands deleted.');
} else {
throw new Error('Slash Command deletion unsuccessful');
}
console.log(createRegisterCommandLogMessage());
const registerResult = await rest.put(createRoute(), {
body: commands
});
if (registerResult instanceof Array) {
console.log(`${registerResult.length} Slash Commands registered`);
} else {
throw new Error('Slash Command registration unsuccessful');
}