|
1 | | -import { |
2 | | - type ChatInputCommandInteraction, |
3 | | - type Client, |
4 | | - Collection, |
5 | | - REST, |
6 | | - Routes, |
7 | | -} from "discord.js"; |
8 | | -import { commands } from "../commands/index.js"; |
9 | | -import type { Command } from "../commands/types.js"; |
10 | | -import { config } from "../env.js"; |
| 1 | +import type { ChatInputCommandInteraction } from "discord.js"; |
11 | 2 |
|
12 | 3 | export const buildCommandString = (interaction: ChatInputCommandInteraction): string => { |
13 | 4 | const commandName = interaction.commandName; |
14 | 5 | return `/${commandName} ${interaction.options.data |
15 | 6 | .map((option) => `${option.name}:${option.value}`) |
16 | 7 | .join(" ")}`; |
17 | 8 | }; |
18 | | - |
19 | | -export async function loadCommands(client: Client) { |
20 | | - if (!client.commands) { |
21 | | - client.commands = new Collection<string, Command>(); |
22 | | - } |
23 | | - |
24 | | - let loaded = 0; |
25 | | - for (const command of commands.values()) { |
26 | | - client.commands.set(command.data.name, command); |
27 | | - loaded += 1; |
28 | | - } |
29 | | - |
30 | | - console.log(`Successfully loaded ${loaded} command handlers into client.commands.`); |
31 | | -} |
32 | | - |
33 | | -export async function registerCommands() { |
34 | | - const commandsData = Array.from(commands.values()).map((command) => command.data); |
35 | | - |
36 | | - try { |
37 | | - const guildId = config.discord.serverId; |
38 | | - const scope = guildId ? `guild ${guildId}` : "global"; |
39 | | - console.log(`Started refreshing ${commandsData.length} ${scope} application commands.`); |
40 | | - |
41 | | - const rest = new REST({ version: "10" }).setToken(config.discord.token); |
42 | | - |
43 | | - await rest.put(Routes.applicationGuildCommands(config.discord.clientId, guildId), { |
44 | | - body: commandsData, |
45 | | - }); |
46 | | - |
47 | | - console.log(`Successfully reloaded ${commandsData.length} ${scope} commands.`); |
48 | | - } catch (error) { |
49 | | - console.error(JSON.stringify(error, null, 2)); |
50 | | - } |
51 | | -} |
0 commit comments