From 3702925d03a25b35060d98f63ba6d376321b7be0 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Mon, 14 Dec 2020 13:13:38 +0100 Subject: [PATCH] feat: new webhook endpoints (#253) * commit changes * Add max message length error --- src/handlers/webhook.ts | 67 +++++++++++++++++++++++++++++++++++++++++ src/types/channel.ts | 9 ++++++ src/types/webhook.ts | 7 +++++ src/utils/constants.ts | 4 +++ 4 files changed, 87 insertions(+) diff --git a/src/handlers/webhook.ts b/src/handlers/webhook.ts index f59a403ff..a8f58f63d 100644 --- a/src/handlers/webhook.ts +++ b/src/handlers/webhook.ts @@ -1,6 +1,7 @@ import { RequestManager } from "../module/requestManager.ts"; import { structures } from "../structures/structures.ts"; import { + EditWebhookMessageOptions, Errors, ExecuteWebhookOptions, MessageCreateOptions, @@ -56,6 +57,10 @@ export async function executeWebhook( throw new Error(Errors.INVALID_WEBHOOK_OPTIONS); } + if (options.content && options.content.length > 2000) { + throw Error(Errors.MESSAGE_MAX_LENGTH); + } + if (options.embeds && options.embeds.length > 10) { options.embeds.splice(10); } @@ -104,3 +109,65 @@ export async function executeWebhook( export function getWebhook(webhookID: string) { return RequestManager.get(endpoints.WEBHOOK_ID(webhookID)); } + +export function editWebhookMessage( + webhookID: string, + webhookToken: string, + messageID: string, + options: EditWebhookMessageOptions, +) { + if (options.content && options.content.length > 2000) { + throw Error(Errors.MESSAGE_MAX_LENGTH); + } + + if (options.embeds && options.embeds.length > 10) { + options.embeds.splice(10); + } + + if (options.allowed_mentions) { + if (options.allowed_mentions.users?.length) { + if (options.allowed_mentions.parse.includes("users")) { + options.allowed_mentions.parse = options.allowed_mentions.parse.filter(( + p, + ) => p !== "users"); + } + + if (options.allowed_mentions.users.length > 100) { + options.allowed_mentions.users = options.allowed_mentions.users.slice( + 0, + 100, + ); + } + } + + if (options.allowed_mentions.roles?.length) { + if (options.allowed_mentions.parse.includes("roles")) { + options.allowed_mentions.parse = options.allowed_mentions.parse.filter(( + p, + ) => p !== "roles"); + } + + if (options.allowed_mentions.roles.length > 100) { + options.allowed_mentions.roles = options.allowed_mentions.roles.slice( + 0, + 100, + ); + } + } + } + + return RequestManager.patch( + endpoints.WEBHOOK_EDIT(webhookID, webhookToken, messageID), + { ...options, allowed_mentions: options.allowed_mentions }, + ); +} + +export function deleteWebhookMessage( + webhookID: string, + webhookToken: string, + messageID: string, +) { + return RequestManager.delete( + endpoints.WEBHOOK_DELETE(webhookID, webhookToken, messageID), + ); +} diff --git a/src/types/channel.ts b/src/types/channel.ts index e3881bb8a..6c116b853 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -120,6 +120,15 @@ export interface MessageContent { replyMessageID?: string; } +export interface AllowedMentions { + /** An array of allowed mention types to parse from the content. */ + parse: ("roles" | "users" | "everyone")[]; + /** Array of role_ids to mention (Max size of 100) */ + roles?: string[]; + /** Array of user_ids to mention (Max size of 100) */ + users?: string[]; +} + export interface GetMessages { /** Max number of messages to return(1-100). Defaults to 50. */ limit?: number; diff --git a/src/types/webhook.ts b/src/types/webhook.ts index 8974e6575..04f9bb8f7 100644 --- a/src/types/webhook.ts +++ b/src/types/webhook.ts @@ -1,3 +1,4 @@ +import { AllowedMentions } from "./channel.ts"; import { UserPayload } from "./guild.ts"; import { Embed } from "./message.ts"; @@ -59,3 +60,9 @@ export interface ExecuteWebhookOptions { users?: string[]; }; } + +export interface EditWebhookMessageOptions { + content?: string; + embeds?: Embed[]; + allowed_mentions?: AllowedMentions; +} diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 5562d5d2d..a1fefea0f 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -96,6 +96,10 @@ export const endpoints = { WEBHOOK: (id: string, token: string) => `${baseEndpoints.BASE_URL}/webhooks/${id}/${token}`, WEBHOOK_ID: (id: string) => `${baseEndpoints.BASE_URL}/webhooks/${id}`, + WEBHOOK_EDIT: (id: string, token: string, messageID: string) => + `${baseEndpoints.BASE_URL}/webhooks/${id}/${token}/messages/${messageID}`, + WEBHOOK_DELETE: (id: string, token: string, messageID: string) => + `${baseEndpoints.BASE_URL}/webhooks/${id}/${token}/messages/${messageID}`, // User endpoints USER: (id: string) => `${baseEndpoints.BASE_URL}/users/${id}`,