feat: new webhook endpoints (#253)

* commit changes

* Add max message length error
This commit is contained in:
ITOH
2020-12-14 13:13:38 +01:00
committed by GitHub
parent 30cff9518f
commit 3702925d03
4 changed files with 87 additions and 0 deletions
+67
View File
@@ -1,6 +1,7 @@
import { RequestManager } from "../module/requestManager.ts"; import { RequestManager } from "../module/requestManager.ts";
import { structures } from "../structures/structures.ts"; import { structures } from "../structures/structures.ts";
import { import {
EditWebhookMessageOptions,
Errors, Errors,
ExecuteWebhookOptions, ExecuteWebhookOptions,
MessageCreateOptions, MessageCreateOptions,
@@ -56,6 +57,10 @@ export async function executeWebhook(
throw new Error(Errors.INVALID_WEBHOOK_OPTIONS); 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) { if (options.embeds && options.embeds.length > 10) {
options.embeds.splice(10); options.embeds.splice(10);
} }
@@ -104,3 +109,65 @@ export async function executeWebhook(
export function getWebhook(webhookID: string) { export function getWebhook(webhookID: string) {
return RequestManager.get(endpoints.WEBHOOK_ID(webhookID)); 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),
);
}
+9
View File
@@ -120,6 +120,15 @@ export interface MessageContent {
replyMessageID?: string; 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 { export interface GetMessages {
/** Max number of messages to return(1-100). Defaults to 50. */ /** Max number of messages to return(1-100). Defaults to 50. */
limit?: number; limit?: number;
+7
View File
@@ -1,3 +1,4 @@
import { AllowedMentions } from "./channel.ts";
import { UserPayload } from "./guild.ts"; import { UserPayload } from "./guild.ts";
import { Embed } from "./message.ts"; import { Embed } from "./message.ts";
@@ -59,3 +60,9 @@ export interface ExecuteWebhookOptions {
users?: string[]; users?: string[];
}; };
} }
export interface EditWebhookMessageOptions {
content?: string;
embeds?: Embed[];
allowed_mentions?: AllowedMentions;
}
+4
View File
@@ -96,6 +96,10 @@ export const endpoints = {
WEBHOOK: (id: string, token: string) => WEBHOOK: (id: string, token: string) =>
`${baseEndpoints.BASE_URL}/webhooks/${id}/${token}`, `${baseEndpoints.BASE_URL}/webhooks/${id}/${token}`,
WEBHOOK_ID: (id: string) => `${baseEndpoints.BASE_URL}/webhooks/${id}`, 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 endpoints
USER: (id: string) => `${baseEndpoints.BASE_URL}/users/${id}`, USER: (id: string) => `${baseEndpoints.BASE_URL}/users/${id}`,