fix(handlers): edit/upsertSlashCommand function (#441)

* fix(handlers): editSlashCommand function

* this check is needed

* fix(handlers): upsertSlashCommand function

* fmt

* add deprecated note

* up

* full use of options

* after thinking it is better not
This commit is contained in:
ITOH
2021-01-24 11:53:37 +01:00
committed by GitHub
parent 8d8c792747
commit fada547388
2 changed files with 73 additions and 21 deletions
+59 -17
View File
@@ -227,27 +227,69 @@ export function getSlashCommands(guildID?: string) {
/** /**
* Edit an existing slash command. If this command did not exist, it will create it. * Edit an existing slash command. If this command did not exist, it will create it.
*/ */
export function upsertSlashCommand(options: UpsertSlashCommandOptions) { export function upsertSlashCommand(
return RequestManager.post( commandID: string,
options.guildID options: UpsertSlashCommandOptions,
? endpoints.COMMANDS_GUILD_ID(applicationID, options.id, options.guildID) guildID?: string,
: endpoints.COMMANDS_ID(applicationID, options.id), ) {
{ // Use ... for content length due to unicode characters and js .length handling
...options, 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. */ // TODO: remove this function for v11
export function editSlashCommand(options: EditSlashCommandOptions) { /**
return RequestManager.patch( * Edit an existing slash command.
options.guildID * @deprecated This function will be removed in v11. Use `upsertSlashCommand()` instead
? endpoints.COMMANDS_GUILD_ID(applicationID, options.id, options.guildID) */
: endpoints.COMMANDS_ID(applicationID, options.id), export function editSlashCommand(
{ commandID: string,
...options, 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. */ /** Deletes a slash command. */
+14 -4
View File
@@ -199,9 +199,15 @@ export enum InteractionResponseType {
ACK_WITH_SOURCE = 5, ACK_WITH_SOURCE = 5,
} }
// TODO: remove this interface for v11
/** @deprecated Use `UpsertSlashCommandOptions` instead */
export interface EditSlashCommandOptions { export interface EditSlashCommandOptions {
id: string; /** 3-32 character command name */
guildID?: string; name: string;
/** 1-100 character description */
description: string;
/** The parameters for the command */
options?: SlashCommandOption[];
} }
export interface ExecuteSlashCommandOptions { export interface ExecuteSlashCommandOptions {
@@ -215,6 +221,10 @@ export interface EditSlashResponseOptions extends SlashCommandCallbackData {
} }
export interface UpsertSlashCommandOptions { export interface UpsertSlashCommandOptions {
id: string; /** 3-32 character command name */
guildID?: string; name: string;
/** 1-100 character description */
description: string;
/** The parameters for the command */
options?: SlashCommandOption[];
} }