From 1c6d5a60e0259dc88c6a474c4c3741a693012171 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Sun, 24 Jan 2021 18:41:28 +0000 Subject: [PATCH] feat(handlers): add upsertSlashCommands() (#442) * feat(handlers): add bulk upsert commands function * Update mod.ts * add jsdoc * add ID info * ID is optional * Update webhook.ts * use //@ts-ignore * idk --- src/api/handlers/mod.ts | 2 ++ src/api/handlers/webhook.ts | 35 +++++++++++++++++++++++++++++++++++ src/types/webhook.ts | 11 +++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/api/handlers/mod.ts b/src/api/handlers/mod.ts index 9de0e938d..93083ba57 100644 --- a/src/api/handlers/mod.ts +++ b/src/api/handlers/mod.ts @@ -120,6 +120,7 @@ import { getSlashCommands, getWebhook, upsertSlashCommand, + upsertSlashCommands, } from "./webhook.ts"; export let handlers = { @@ -241,6 +242,7 @@ export let handlers = { getSlashCommand, getSlashCommands, upsertSlashCommand, + upsertSlashCommands, editSlashCommand, deleteSlashCommand, executeSlashCommand, diff --git a/src/api/handlers/webhook.ts b/src/api/handlers/webhook.ts index fc04553c4..3472f70f2 100644 --- a/src/api/handlers/webhook.ts +++ b/src/api/handlers/webhook.ts @@ -11,6 +11,7 @@ import { MessageCreateOptions, SlashCommand, UpsertSlashCommandOptions, + UpsertSlashCommandsOptions, WebhookCreateOptions, WebhookPayload, } from "../../types/mod.ts"; @@ -270,6 +271,40 @@ export function upsertSlashCommand( return result; } +/** + * Bulk edit existing slash commands. If a command does not exist, it will create it. + * + * **NOTE:** Any slash commands that are not specified in this function will be **deleted**. If you don't provide the commandID and rename your command, the command gets a new ID. + */ +export async function upsertSlashCommands( + options: UpsertSlashCommandsOptions[], + guildID?: string, +) { + const data = options.map((option) => { + // Use ... for content length due to unicode characters and js .length handling + if ([...option.name].length < 2 || [...option.name].length > 32) { + throw new Error(Errors.INVALID_SLASH_NAME); + } + + if ( + [...option.description].length < 1 || [...option.description].length > 100 + ) { + throw new Error(Errors.INVALID_SLASH_DESCRIPTION); + } + + return option; + }); + + const result = await RequestManager.put( + guildID + ? endpoints.COMMANDS_GUILD(applicationID, guildID) + : endpoints.COMMANDS(applicationID), + data, + ); + + return result; +} + // TODO: remove this function for v11 /** * Edit an existing slash command. diff --git a/src/types/webhook.ts b/src/types/webhook.ts index fc71ecbe3..27ccb2e87 100644 --- a/src/types/webhook.ts +++ b/src/types/webhook.ts @@ -228,3 +228,14 @@ export interface UpsertSlashCommandOptions { /** The parameters for the command */ options?: SlashCommandOption[]; } + +export interface UpsertSlashCommandsOptions { + /** The id of the command */ + id: string; + /** 3-32 character command name */ + name: string; + /** 1-100 character description */ + description: string; + /** The parameters for the command */ + options?: SlashCommandOption[]; +}