From 4af6f363f88eb2c2d20aa51aa7d636eeea183457 Mon Sep 17 00:00:00 2001 From: ayntee Date: Sun, 7 Mar 2021 18:48:12 +0400 Subject: [PATCH] feat(handlers/webhook): test slash command name against ^[\w-]{1,32}$ (#613) * feat(handlers/webhook): test slash command name against ^[\w-]{1,32}$ * Update src/api/handlers/webhook.ts Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> * Update src/api/handlers/webhook.ts Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> * Update src/api/handlers/webhook.ts Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> * Update src/api/handlers/webhook.ts Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> * move to constants file * idk * idk Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> --- src/api/handlers/webhook.ts | 14 +++++--------- src/util/constants.ts | 2 ++ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/api/handlers/webhook.ts b/src/api/handlers/webhook.ts index 8533ab256..bc181def7 100644 --- a/src/api/handlers/webhook.ts +++ b/src/api/handlers/webhook.ts @@ -17,7 +17,7 @@ import { WebhookPayload, } from "../../types/mod.ts"; import { cache } from "../../util/cache.ts"; -import { endpoints } from "../../util/constants.ts"; +import { endpoints, SLASH_COMMANDS_NAME_REGEX } from "../../util/constants.ts"; import { botHasChannelPermissions } from "../../util/permissions.ts"; import { urlToBase64 } from "../../util/utils.ts"; import { structures } from "../structures/mod.ts"; @@ -282,8 +282,7 @@ export async function deleteWebhookMessage( * Guild commands update **instantly**. We recommend you use guild commands for quick testing, and global commands when they're ready for public use. */ export async function createSlashCommand(options: CreateSlashCommandOptions) { - // Use ... for content length due to unicode characters and js .length handling - if ([...options.name].length < 2 || [...options.name].length > 32) { + if (!SLASH_COMMANDS_NAME_REGEX.test(options.name)) { throw new Error(Errors.INVALID_SLASH_NAME); } @@ -336,8 +335,7 @@ export async function upsertSlashCommand( 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) { + if (!SLASH_COMMANDS_NAME_REGEX.test(options.name)) { throw new Error(Errors.INVALID_SLASH_NAME); } @@ -371,8 +369,7 @@ export async function upsertSlashCommands( 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) { + if (!SLASH_COMMANDS_NAME_REGEX.test(option.name)) { throw new Error(Errors.INVALID_SLASH_NAME); } @@ -405,8 +402,7 @@ export async function editSlashCommand( 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) { + if (!SLASH_COMMANDS_NAME_REGEX.test(options.name)) { throw new Error(Errors.INVALID_SLASH_NAME); } diff --git a/src/util/constants.ts b/src/util/constants.ts index afad4433c..af1d8ef57 100644 --- a/src/util/constants.ts +++ b/src/util/constants.ts @@ -175,3 +175,5 @@ export const endpoints = { // oAuth2 OAUTH2_APPLICATION: `${baseEndpoints.BASE_URL}/oauth2/applications/@me`, }; + +export const SLASH_COMMANDS_NAME_REGEX = /^[\w-]{1,32}$/;