feat: add validateLength util

This commit is contained in:
Skillz4Killz
2021-04-13 16:45:39 +00:00
committed by GitHub
parent 16a5ac684a
commit d746622681
6 changed files with 88 additions and 29 deletions
+2 -1
View File
@@ -10,6 +10,7 @@ import { PermissionStrings } from "../../types/permissions/permission_strings.ts
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
import { validateLength } from "../../util/validate_length.ts";
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
export async function sendMessage(
@@ -48,7 +49,7 @@ export async function sendMessage(
}
// Use ... for content length due to unicode characters and js .length handling
if (content.content && [...content.content].length > 2000) {
if (content.content && !validateLength(content.content, { max: 2000 })) {
throw new Error(Errors.MESSAGE_MAX_LENGTH);
}
+2 -3
View File
@@ -6,6 +6,7 @@ import { DiscordWebhook } from "../../types/webhooks/webhook.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase, urlToBase64 } from "../../util/utils.ts";
import { validateLength } from "../../util/validate_length.ts";
/**
* Create a new webhook. Requires the MANAGE_WEBHOOKS permission. Returns a webhook object on success. Webhook names follow our naming restrictions that can be found in our Usernames and Nicknames documentation, with the following additional stipulations:
@@ -21,9 +22,7 @@ export async function createWebhook(
if (
// Specific usernames that discord does not allow
options.name === "clyde" ||
// Character limit checks. [...] checks are because of js unicode length handling
[...options.name].length < 2 ||
[...options.name].length > 32
!validateLength(options.name, { min: 2, max: 32 })
) {
throw new Error(Errors.INVALID_WEBHOOK_NAME);
}