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
This commit is contained in:
ITOH
2021-01-24 18:41:28 +00:00
committed by GitHub
parent 2a78e1b92e
commit 1c6d5a60e0
3 changed files with 48 additions and 0 deletions
+2
View File
@@ -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,
+35
View File
@@ -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.