mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { rest } from "../../rest/rest.ts";
|
|
import { structures } from "../../structures/mod.ts";
|
|
import { Errors } from "../../types/misc/errors.ts";
|
|
import { endpoints } from "../../util/constants.ts";
|
|
|
|
/** Execute a webhook with webhook Id and webhook token */
|
|
export async function executeWebhook(
|
|
webhookId: string,
|
|
webhookToken: string,
|
|
options: ExecuteWebhookOptions,
|
|
) {
|
|
if (!options.content && !options.file && !options.embeds) {
|
|
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);
|
|
}
|
|
|
|
if (options.mentions) {
|
|
if (options.mentions.users?.length) {
|
|
if (options.mentions.parse.includes("users")) {
|
|
options.mentions.parse = options.mentions.parse.filter(
|
|
(p) => p !== "users",
|
|
);
|
|
}
|
|
|
|
if (options.mentions.users.length > 100) {
|
|
options.mentions.users = options.mentions.users.slice(0, 100);
|
|
}
|
|
}
|
|
|
|
if (options.mentions.roles?.length) {
|
|
if (options.mentions.parse.includes("roles")) {
|
|
options.mentions.parse = options.mentions.parse.filter(
|
|
(p) => p !== "roles",
|
|
);
|
|
}
|
|
|
|
if (options.mentions.roles.length > 100) {
|
|
options.mentions.roles = options.mentions.roles.slice(0, 100);
|
|
}
|
|
}
|
|
}
|
|
|
|
const result = await rest.runMethod(
|
|
"post",
|
|
`${endpoints.WEBHOOK(webhookId, webhookToken)}${
|
|
options.wait ? "?wait=true" : ""
|
|
}`,
|
|
{
|
|
...options,
|
|
allowed_mentions: options.mentions,
|
|
avatar_url: options.avatar_url,
|
|
},
|
|
);
|
|
if (!options.wait) return;
|
|
|
|
return structures.createMessageStruct(result as MessageCreateOptions);
|
|
}
|