CharCord/scripts/deployCommands.ts

42 lines
1.2 KiB
TypeScript

import { REST, RESTPostAPIChatInputApplicationCommandsJSONBody, Routes } from "npm:discord.js";
const { clientId, guildId, token } = Deno.env.toObject();
import { Command } from "../src/types.ts";
import { gatherCommandsPaths } from "../src/commands.ts";
const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [];
async function register(commandPath: string) {
const { default: command }: { default: Command } = await import(commandPath);
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
console.log(`Deploying ${command.data.name}...`);
} else {
console.log(
`[WARNING] The command at ${commandPath} is missing a required "data" or "execute" property.`
);
}
}
for (const commandPath of await gatherCommandsPaths()) {
await register(commandPath)
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(
`Started refreshing ${commands.length} application (/) commands.`
);
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands }
);
console.log(
`Successfully reloaded ${commands.length} application (/) commands.`
);
} catch (error) {
console.error(error);
}
})();