This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
updateCommands.ts
72 lines (60 loc) · 2.67 KB
/
updateCommands.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
import { Routes, type RESTPutAPIApplicationCommandsResult } from 'discord-api-types/v9';
import ApplicationCommand from './src/lib/command';
import dotenvExpand from 'dotenv-expand';
import { REST } from '@discordjs/rest';
import dotenv from 'dotenv';
import chalk from 'chalk';
import fs from 'fs';
import type { SlashCommand, ContextMenu } from '@discord/types';
const isJavaScript = (fileName: string) => fileName.endsWith(".js");
const chatCommandFiles = fs.readdirSync('./client/discord/commands').filter(isJavaScript);
const menuCommandFiles = fs.readdirSync('./client/discord/context').filter(isJavaScript);
const commands: ApplicationCommand[] = [];
const isDev = process.argv.includes("--dev");
console.log(`Deploying ${isDev ? "developement" : "main"} commands...`);
const env = dotenv.config({ path: isDev ? ".env.local" : ".env" });
dotenvExpand(env);
const clientId = process.env.CLIENT_ID;
if (clientId === undefined) throw new Error("CLIENT_ID must be provided!");
const botToken = process.env.DISCORD_BOT_TOKEN;
if (botToken === undefined) throw new Error("DISCORD_BOT_TOKEN must be provided!");
const rest = new REST({ version: "9" }).setToken(botToken);
void async function () {
for (const file of chatCommandFiles) {
const command: SlashCommand = await import(`./client/discord/commands/${file}`);
if (isDev && !command.data.defaultPermission) command.data.defaultPermission = true;
if (!isDev && !command.isGlobal) continue;
const commandData = new ApplicationCommand(command.data);
commands.push(commandData);
}
for (const file of menuCommandFiles) {
const command: ContextMenu = await import(`./client/discord/context/${file}`);
if (isDev && !command.data.defaultPermission) command.data.defaultPermission = true;
if (!command.isGlobal) continue;
const commandData = new ApplicationCommand(command.data);
commands.push(commandData);
}
try {
console.log('Started refreshing application commands...');
let fullRoute: `/${string}`;
if (isDev) {
const guildId = process.env.TEST_GUILD_ID;
if (guildId === undefined) throw new Error("TEST_GUILD_ID must be provided!");
fullRoute = Routes.applicationGuildCommands(clientId, guildId);
} else {
fullRoute = Routes.applicationCommands(clientId);
}
const applicationCommands = await rest.put(fullRoute, { body: commands }) as RESTPutAPIApplicationCommandsResult;
console.log(chalk.green('Successfully reloaded application commands.'));
console.table(
applicationCommands.map(command => ({
"Command Name": command.name,
"Command ID": command.id,
"Version": command.version
}))
);
}
catch (error) {
console.error(chalk.red("Failed to reload application commands:"), error);
}
}();