From f5b18513d20c6903054bc76c5b34bf1de1b100cd Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Wed, 23 Oct 2024 12:31:40 -0400 Subject: [PATCH] Change updateGuild to refresh guild and make it actually work --- package.json | 2 +- src/bot/deploy-commands.ts | 2 +- .../{updateGuild.ts => refreshGuild.ts} | 24 +++++++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) rename src/scripts/{updateGuild.ts => refreshGuild.ts} (66%) diff --git a/package.json b/package.json index 6eaa962..5ea0f54 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "rm -rf ./dist && tsc -p .", "start": "node ./dist/main.js", - "refresh": "node ./dist/scripts/updateGuild.js", + "refresh": "node ./dist/scripts/refreshGuild.js", "prettier": "if prettier -v >/dev/null 2>&1; then prettier . --write; else npx prettier . --write; fi", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/bot/deploy-commands.ts b/src/bot/deploy-commands.ts index 311ac02..7445806 100644 --- a/src/bot/deploy-commands.ts +++ b/src/bot/deploy-commands.ts @@ -29,7 +29,7 @@ const commandsData = Object.values(commands).map(command => command.data.toJSON() ); -const rest = new REST({ version: "9" }).setToken(BOT_TOKEN); +export const rest = new REST({ version: "9" }).setToken(BOT_TOKEN); export async function deployCommands({ guildId }: DeployCommandsProps) { try { diff --git a/src/scripts/updateGuild.ts b/src/scripts/refreshGuild.ts similarity index 66% rename from src/scripts/updateGuild.ts rename to src/scripts/refreshGuild.ts index ebc55b6..be4ab69 100644 --- a/src/scripts/updateGuild.ts +++ b/src/scripts/refreshGuild.ts @@ -16,21 +16,31 @@ * along with this program. If not, see . */ -import { Events } from "discord.js"; -import { BotClient, deployCommands } from "../bot"; +import { Events, Routes } from "discord.js"; +import { BOT_ID, BotClient, deployCommands, rest } from "../bot"; if (process.argv.length < 3 || !process.argv[2]) { console.log("You need to specify the guild ID to refresh."); process.exit(1); } +const [, , guildId] = process.argv; + BotClient.on(Events.ClientReady, () => { try { - BotClient.fetchGuildPreview(process.argv[2]) - .then(({ id, name }) => { - deployCommands({ guildId: id }); - console.log(`Updated (/) commands for "${name}".`); - process.exit(0); + rest + .put(Routes.applicationGuildCommands(BOT_ID, guildId), { body: [] }) + .then(() => { + console.log("Deleted (/) commands."); + deployCommands({ guildId: guildId }) + .then(() => { + console.log("Successfully reloaded (/) commands."); + process.exit(0); + }) + .catch(err => { + console.log("An error occurred refreshing (/) commands:", err); + process.exit(1); + }); }) .catch(err => { console.log("An error occured refreshing (/) commands:", err);