···11-import { walk } from "https://deno.land/std@0.170.0/fs/walk.ts";
22-import { VoidyClient } from "../utils/classes/VoidyClient.ts";
33-44-export async function handleCommands(client: VoidyClient, path: string = "src/commands") {
55- for await (const walkEntry of walk(path)) {
66- if (!walkEntry.isFile) continue;
77-88- const command = (await import(`file://${Deno.cwd()}/${walkEntry.path}`)).default;
99-1010- if ("data" in command && "execute" in command) {
1111- client.commands.set(command.data.name, command);
1212- console.log(`[Voidy] Loaded command: ${command.data.name}`);
1313- } else {
1414- console.log(`[Voidy] Command ${walkEntry.path} is missing the "data" or "execute" property.`);
1515- }
1616- }
1717-}
+56
src/handlers/handleCommand.ts
···11+import { walk } from "https://deno.land/std@0.170.0/fs/walk.ts";
22+import { VoidyClient } from "../utils/classes/VoidyClient.ts";
33+import { Command } from "../utils/classes/Command.ts";
44+import { REST, Routes } from "discord.js";
55+import { RESTPostAPIChatInputApplicationCommandsJSONBody } from "discord-api-types/rest";
66+77+export async function handleCommands(client: VoidyClient, path: string = "src/commands") {
88+ console.log(`[Voidy] Loading commands from ${path}...`);
99+1010+ const commands = [];
1111+1212+ // Iterate through each command found in the specified directory
1313+ for await (const walkEntry of walk(path)) {
1414+ if (!walkEntry.isFile) continue;
1515+1616+ const command: Command = (await import(`file://${Deno.cwd()}/${walkEntry.path}`)).default;
1717+1818+ if ("data" in command && "execute" in command) {
1919+ client.commands.set(command.data.name, command);
2020+ commands.push(command.data.toJSON());
2121+2222+ console.log(`[Voidy] Loaded command: ${command.data.name}`);
2323+ } else {
2424+ console.log(`[Voidy] Command ${walkEntry.path} is missing the "data" or "execute" property.`);
2525+ }
2626+ }
2727+2828+ console.log();
2929+3030+ // Deploy commands to all guilds
3131+ await deployCommands(commands);
3232+}
3333+3434+async function deployCommands(commands: RESTPostAPIChatInputApplicationCommandsJSONBody[]) {
3535+ console.log(`[Voidy] Deploying ${commands.length} commands...`);
3636+3737+ // Grab the bot token from the BOT_TOKEN environment variable
3838+ const token = Deno.env.get("BOT_TOKEN");
3939+ if (!token) throw new Error("BOT_TOKEN environment variable is missing");
4040+4141+ // Generate a new instance of the discord.js rest client using the bot token
4242+ const rest = new REST().setToken(token);
4343+4444+ // Grab the bot client id from the BOT_CLIENT_ID environment variable
4545+ const clientId = Deno.env.get("BOT_CLIENT_ID");
4646+ if (!clientId) throw new Error("BOT_CLIENT_ID environment variable is missing");
4747+4848+ // Send a put request to the application commands endpoint, the request body contains an array of all commands
4949+ await rest.put(
5050+ Routes.applicationCommands(clientId),
5151+ { body: commands },
5252+ );
5353+5454+ console.log(`[Voidy] Successfully deployed ${commands.length} commands.`);
5555+ console.log();
5656+}