add check

This commit is contained in:
ITOH
2021-05-06 14:53:24 +02:00
parent 1555fc43e1
commit 5f6d556e1d
4 changed files with 66 additions and 49 deletions
+6 -1
View File
@@ -2,12 +2,13 @@ import { botId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { DiscordenoMessage } from "../../structures/message.ts"; import { DiscordenoMessage } from "../../structures/message.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { EditMessage } from "../../types/messages/edit_message.ts"; import { EditMessage } from "../../types/messages/edit_message.ts";
import type { Message } from "../../types/messages/message.ts"; import type { Message } from "../../types/messages/message.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { validateComponents } from "../../util/utils.ts";
/** Edit the message. */ /** Edit the message. */
export async function editMessage( export async function editMessage(
@@ -20,6 +21,10 @@ export async function editMessage(
if (typeof content === "string") content = { content }; if (typeof content === "string") content = { content };
if (content.components?.length) {
validateComponents(content.components);
}
const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"]; const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"];
await requireBotChannelPermissions(message.channelId, requiredPerms); await requireBotChannelPermissions(message.channelId, requiredPerms);
+2 -47
View File
@@ -4,16 +4,13 @@ import { structures } from "../../structures/mod.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { Errors } from "../../types/discordeno/errors.ts"; import { Errors } from "../../types/discordeno/errors.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts"; import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
import { ButtonStyles } from "../../types/messages/components/button_styles.ts";
import type { CreateMessage } from "../../types/messages/create_message.ts"; import type { CreateMessage } from "../../types/messages/create_message.ts";
import type { Message } from "../../types/messages/message.ts"; import type { Message } from "../../types/messages/message.ts";
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakelize } from "../../util/utils.ts"; import { snakelize, validateComponents } from "../../util/utils.ts";
import { validateLength } from "../../util/validate_length.ts"; import { validateLength } from "../../util/validate_length.ts";
import { isActionRow } from "../type_guards/is_action_row.ts";
import { isButton } from "../type_guards/is_button.ts";
/** Send a message to the channel. Requires SEND_MESSAGES permission. */ /** Send a message to the channel. Requires SEND_MESSAGES permission. */
export async function sendMessage( export async function sendMessage(
@@ -100,49 +97,7 @@ export async function sendMessage(
} }
if (content.components?.length) { if (content.components?.length) {
let actionRowCounter = 0; validateComponents(content.components);
for (const component of content.components) {
// 5 Link buttons can not have a customId
if (isButton(component)) {
if (
component.type === ButtonStyles.Link &&
component.customId
) {
throw new Error(Errors.LINK_BUTTON_CANNOT_HAVE_CUSTOM_ID);
}
// Other buttons must have a customId
if (
!component.customId && component.type !== ButtonStyles.Link
) {
throw new Error(Errors.BUTTON_REQUIRES_CUSTOM_ID);
}
if (!validateLength(component.label, { max: 80 })) {
throw new Error(Errors.COMPONENT_LABEL_TOO_BIG);
}
if (
component.customId &&
!validateLength(component.customId, { max: 100 })
) {
throw new Error(Errors.COMPONENT_CUSTOM_ID_TOO_BIG);
}
}
if (!isActionRow(component)) {
continue;
}
actionRowCounter++;
// Max of 5 ActionRows per message
if (actionRowCounter > 5) throw new Error(Errors.TOO_MANY_ACTION_ROWS);
// Max of 5 Buttons (or any component type) within an ActionRow
if (component.components?.length > 5) {
throw new Error(Errors.TOO_MANY_COMPONENTS);
}
}
} }
const result = await rest.runMethod<Message>( const result = await rest.runMethod<Message>(
+6 -1
View File
@@ -1,10 +1,11 @@
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts"; import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
import type { Message } from "../../types/messages/message.ts"; import type { Message } from "../../types/messages/message.ts";
import { Errors } from "../../types/discordeno/errors.ts";
import type { EditWebhookMessage } from "../../types/webhooks/edit_webhook_message.ts"; import type { EditWebhookMessage } from "../../types/webhooks/edit_webhook_message.ts";
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { validateComponents } from "../../util/utils.ts";
export async function editWebhookMessage( export async function editWebhookMessage(
webhookId: bigint, webhookId: bigint,
@@ -59,6 +60,10 @@ export async function editWebhookMessage(
} }
} }
if (options.components?.length) {
validateComponents(options.components);
}
const result = await rest.runMethod<Message>( const result = await rest.runMethod<Message>(
"patch", "patch",
options.messageId options.messageId
+52
View File
@@ -1,11 +1,15 @@
import { encode } from "../../deps.ts"; import { encode } from "../../deps.ts";
import { eventHandlers } from "../bot.ts"; import { eventHandlers } from "../bot.ts";
import { isActionRow } from "../helpers/type_guards/is_action_row.ts";
import { isButton } from "../helpers/type_guards/is_button.ts";
import { Errors } from "../types/discordeno/errors.ts"; import { Errors } from "../types/discordeno/errors.ts";
import type { ApplicationCommandOption } from "../types/interactions/commands/application_command_option.ts"; import type { ApplicationCommandOption } from "../types/interactions/commands/application_command_option.ts";
import type { ApplicationCommandOptionChoice } from "../types/interactions/commands/application_command_option_choice.ts"; import type { ApplicationCommandOptionChoice } from "../types/interactions/commands/application_command_option_choice.ts";
import { DiscordApplicationCommandOptionTypes } from "../types/interactions/commands/application_command_option_types.ts"; import { DiscordApplicationCommandOptionTypes } from "../types/interactions/commands/application_command_option_types.ts";
import type { CreateGlobalApplicationCommand } from "../types/interactions/commands/create_global_application_command.ts"; import type { CreateGlobalApplicationCommand } from "../types/interactions/commands/create_global_application_command.ts";
import type { EditGlobalApplicationCommand } from "../types/interactions/commands/edit_global_application_command.ts"; import type { EditGlobalApplicationCommand } from "../types/interactions/commands/edit_global_application_command.ts";
import { ButtonStyles } from "../types/messages/components/button_styles.ts";
import type { MessageComponents } from "../types/messages/components/message_components.ts";
import type { DiscordImageFormat } from "../types/misc/image_format.ts"; import type { DiscordImageFormat } from "../types/misc/image_format.ts";
import type { DiscordImageSize } from "../types/misc/image_size.ts"; import type { DiscordImageSize } from "../types/misc/image_size.ts";
import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts"; import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts";
@@ -216,3 +220,51 @@ export function hasOwnProperty<T extends {}, Y extends PropertyKey = string>(
// deno-lint-ignore no-prototype-builtins // deno-lint-ignore no-prototype-builtins
return obj.hasOwnProperty(prop); return obj.hasOwnProperty(prop);
} }
export function validateComponents(components: MessageComponents) {
if (components?.length) {
let actionRowCounter = 0;
for (const component of components) {
// 5 Link buttons can not have a customId
if (isButton(component)) {
if (
component.type === ButtonStyles.Link &&
component.customId
) {
throw new Error(Errors.LINK_BUTTON_CANNOT_HAVE_CUSTOM_ID);
}
// Other buttons must have a customId
if (
!component.customId && component.type !== ButtonStyles.Link
) {
throw new Error(Errors.BUTTON_REQUIRES_CUSTOM_ID);
}
if (!validateLength(component.label, { max: 80 })) {
throw new Error(Errors.COMPONENT_LABEL_TOO_BIG);
}
if (
component.customId &&
!validateLength(component.customId, { max: 100 })
) {
throw new Error(Errors.COMPONENT_CUSTOM_ID_TOO_BIG);
}
}
if (!isActionRow(component)) {
continue;
}
actionRowCounter++;
// Max of 5 ActionRows per message
if (actionRowCounter > 5) throw new Error(Errors.TOO_MANY_ACTION_ROWS);
// Max of 5 Buttons (or any component type) within an ActionRow
if (component.components?.length > 5) {
throw new Error(Errors.TOO_MANY_COMPONENTS);
}
}
}
}