Files
discordeno/plugins/bot/helpers/webhooks/sendWebhook.ts
Skillz4Killz ffe7cdbc6f feat: base plugin lib idea (#2308)
* feat: base plugin lib idea

* fix: stuff

* fmt

* fix: imports and exports

* fix: errors & tests

* fix: remove logs
2022-06-18 18:46:37 -04:00

62 lines
2.4 KiB
TypeScript

import { Bot } from "../../bot.ts";
import { DiscordMessage, FileContent } from "../../deps.ts";
import { Embed } from "../../transformers/embed.ts";
import { AllowedMentions, MessageComponents } from "../../typings.ts";
/** Send a webhook with webhook Id and webhook token */
export async function sendWebhook(bot: Bot, webhookId: bigint, webhookToken: string, options: ExecuteWebhook) {
const allowedMentions = options.allowedMentions
? {
parse: options.allowedMentions.parse,
replied_user: options.allowedMentions.repliedUser,
users: options.allowedMentions.users?.map((id) => id.toString()),
roles: options.allowedMentions.roles?.map((id) => id.toString()),
}
: { parse: [] };
const result = await bot.rest.runMethod<DiscordMessage>(
bot.rest,
"POST",
bot.constants.routes.WEBHOOK(webhookId, webhookToken, options),
{
wait: options.wait,
thread_id: options.threadId,
content: options.content,
username: options.username,
avatar_url: options.avatarUrl,
tts: options.tts,
file: options.file,
embeds: options.embeds?.map((embed) => bot.transformers.reverse.embed(bot, embed)),
allowed_mentions: allowedMentions,
components: options.components?.map((component) => bot.transformers.reverse.component(bot, component)),
},
);
if (!options.wait) return;
return bot.transformers.message(bot, result);
}
/** https://discord.com/developers/docs/resources/webhook#execute-webhook */
export interface ExecuteWebhook {
/** Waits for server confirmation of message send before response, and returns the created message body (defaults to `false`; when `false` a message that is not saved does not return an error) */
wait?: boolean;
/** Send a message to the specified thread within a webhook's channel. The thread will automatically be unarchived. */
threadId?: bigint;
/** The message contents (up to 2000 characters) */
content?: string;
/** Override the default username of the webhook */
username?: string;
/** Override the default avatar of the webhook */
avatarUrl?: string;
/** True if this is a TTS message */
tts?: boolean;
/** The contents of the file being sent */
file?: FileContent | FileContent[];
/** Embedded `rich` content */
embeds?: Embed[];
/** Allowed mentions for the message */
allowedMentions?: AllowedMentions;
/** the components to include with the message */
components?: MessageComponents;
}