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.
*/
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. */