From 0fbd8f9c62803d79aeaeb6683707f74ab4f71a0b Mon Sep 17 00:00:00 2001 From: ashley zomo Date: Thu, 27 Jan 2022 16:51:48 -0600 Subject: [PATCH] discord slash commands base --- dist/discord.js | 41 +++++++++++++++++++++++++++++++++++++++++ src/discord.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 dist/discord.js create mode 100644 src/discord.ts diff --git a/dist/discord.js b/dist/discord.js new file mode 100644 index 0000000..b604ef6 --- /dev/null +++ b/dist/discord.js @@ -0,0 +1,41 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.registerCommands = void 0; +const rest_1 = require("@discordjs/rest"); +const v9_1 = require("discord-api-types/v9"); +const commands = [{ + name: 'queue', + description: 'creates a queue' + }]; +/** + * register/reload commands on guild(s) + * @param token discord bot token + * @param clientId discord bot client id + * @param guildIds discord guild id(s) + */ +function registerCommands(token, clientId, guildIds) { + return __awaiter(this, void 0, void 0, function* () { + const rest = new rest_1.REST({ version: '9' }).setToken(token); + if (typeof guildIds === 'string') + guildIds = [guildIds]; + for (let i = 0; i < guildIds.length; i++) { + try { + yield rest.put(v9_1.Routes.applicationGuildCommands(clientId, guildIds[i]), { body: commands }); + console.log(`[${guildIds[i]}] registered command`); + } + catch (error) { + console.error(error); + } + } + }); +} +exports.registerCommands = registerCommands; diff --git a/src/discord.ts b/src/discord.ts new file mode 100644 index 0000000..b16d4cc --- /dev/null +++ b/src/discord.ts @@ -0,0 +1,34 @@ +import { REST } from '@discordjs/rest'; +import { Routes } from 'discord-api-types/v9'; + +const commands = [{ + name: 'queue', + description: 'creates a queue' +}]; + +/** + * register/reload commands on guild(s) + * @param token discord bot token + * @param clientId discord bot client id + * @param guildIds discord guild id(s) + */ +export async function registerCommands(token: string, clientId: string, guildIds: string|string[]) { + + const rest = new REST({ version: '9' }).setToken(token); + + if (typeof guildIds === 'string') + guildIds = [guildIds]; + + for (let i = 0; i < guildIds.length; i++) { + try { + await rest.put( + Routes.applicationGuildCommands(clientId, guildIds[i]), + { body: commands }, + ); + console.log(`[${guildIds[i]}] registered command`); + } catch (error) { + console.error(error); + } + } + +} \ No newline at end of file