diff --git a/src/api/handlers/webhook.ts b/src/api/handlers/webhook.ts index 769f79fa7..4b6643d89 100644 --- a/src/api/handlers/webhook.ts +++ b/src/api/handlers/webhook.ts @@ -227,27 +227,69 @@ export function getSlashCommands(guildID?: string) { /** * Edit an existing slash command. If this command did not exist, it will create it. */ -export function upsertSlashCommand(options: UpsertSlashCommandOptions) { - return RequestManager.post( - options.guildID - ? endpoints.COMMANDS_GUILD_ID(applicationID, options.id, options.guildID) - : endpoints.COMMANDS_ID(applicationID, options.id), - { - ...options, - }, +export function upsertSlashCommand( + commandID: string, + options: UpsertSlashCommandOptions, + guildID?: string, +) { + // Use ... for content length due to unicode characters and js .length handling + if ([...options.name].length < 2 || [...options.name].length > 32) { + throw new Error(Errors.INVALID_SLASH_NAME); + } + + if ( + [...options.description].length < 1 || [...options.description].length > 100 + ) { + throw new Error(Errors.INVALID_SLASH_DESCRIPTION); + } + + const result = RequestManager.patch( + guildID + ? endpoints.COMMANDS_GUILD_ID( + applicationID, + guildID, + commandID, + ) + : endpoints.COMMANDS_ID(applicationID, commandID), + options, ); + + return result; } -/** Edit an existing slash command. */ -export function editSlashCommand(options: EditSlashCommandOptions) { - return RequestManager.patch( - options.guildID - ? endpoints.COMMANDS_GUILD_ID(applicationID, options.id, options.guildID) - : endpoints.COMMANDS_ID(applicationID, options.id), - { - ...options, - }, +// TODO: remove this function for v11 +/** + * Edit an existing slash command. + * @deprecated This function will be removed in v11. Use `upsertSlashCommand()` instead + */ +export function editSlashCommand( + commandID: string, + options: EditSlashCommandOptions, + guildID?: string, +) { + // Use ... for content length due to unicode characters and js .length handling + if ([...options.name].length < 2 || [...options.name].length > 32) { + throw new Error(Errors.INVALID_SLASH_NAME); + } + + if ( + [...options.description].length < 1 || [...options.description].length > 100 + ) { + throw new Error(Errors.INVALID_SLASH_DESCRIPTION); + } + + const result = RequestManager.patch( + guildID + ? endpoints.COMMANDS_GUILD_ID( + applicationID, + guildID, + commandID, + ) + : endpoints.COMMANDS_ID(applicationID, commandID), + options, ); + + return result; } /** Deletes a slash command. */ diff --git a/src/types/webhook.ts b/src/types/webhook.ts index 72b0a68cd..fc71ecbe3 100644 --- a/src/types/webhook.ts +++ b/src/types/webhook.ts @@ -199,9 +199,15 @@ export enum InteractionResponseType { ACK_WITH_SOURCE = 5, } +// TODO: remove this interface for v11 +/** @deprecated Use `UpsertSlashCommandOptions` instead */ export interface EditSlashCommandOptions { - id: string; - guildID?: string; + /** 3-32 character command name */ + name: string; + /** 1-100 character description */ + description: string; + /** The parameters for the command */ + options?: SlashCommandOption[]; } export interface ExecuteSlashCommandOptions { @@ -215,6 +221,10 @@ export interface EditSlashResponseOptions extends SlashCommandCallbackData { } export interface UpsertSlashCommandOptions { - id: string; - guildID?: string; + /** 3-32 character command name */ + name: string; + /** 1-100 character description */ + description: string; + /** The parameters for the command */ + options?: SlashCommandOption[]; }