From ae3a5133b3df81a235eaca39aaa09c47b3d96687 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Mon, 8 May 2023 09:15:34 +0200 Subject: [PATCH] Create helper script for pushing commands --- package.json | 3 ++- src/index.ts | 2 +- src/scripts/pushCommands.ts | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/scripts/pushCommands.ts diff --git a/package.json b/package.json index fc0484d..56a4f1c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "./dist/index.js", "scripts": { "start": "tsc && node dist/index.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "publishCommands": "tsc && node dist/scripts/pushCommands.js" }, "author": "Wroclaw", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index 35f01ca..dbfa66d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,4 +34,4 @@ discord.on("messageCreate", async message => { queueRequest(message); }); -discord.login(config.tokens.Discord); +if (require.main === module) discord.login(config.tokens.Discord); diff --git a/src/scripts/pushCommands.ts b/src/scripts/pushCommands.ts new file mode 100644 index 0000000..5b17fbb --- /dev/null +++ b/src/scripts/pushCommands.ts @@ -0,0 +1,40 @@ +// https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands + +import { REST, RESTGetAPIOAuth2CurrentApplicationResult, RESTPostAPIApplicationCommandsJSONBody, Routes } from "discord.js"; +import config from "../config"; +import requireDirectory from "require-directory"; + +import Command from "../command"; + +const post: RESTPostAPIApplicationCommandsJSONBody[] = []; + +const guildId = process.argv.slice(2)[0]; +requireDirectory<{default: Command}, void>(module, "../commands", { + visit: function (obj) { + console.log(obj); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + post.push(new obj.default().toRESTPostApplicationCommands()); + }, +}); + +const rest = new REST().setToken(config.tokens.Discord); + +(async () => { + const me = await rest.get(Routes.oauth2CurrentApplication()) as RESTGetAPIOAuth2CurrentApplicationResult; + console.log(`Started refreshing ${post.length} application commands.`); + if (guildId && guildId != "") + await rest.put( + Routes.applicationGuildCommands(me.id, guildId), + { body: post }, + ); + else { + await rest.put( + Routes.applicationCommands(me.id), + { body: post }, + ); + } + console.log("Refreshed successfully"); +})().catch( e => { + console.error(e); +});