From cc2c3ac1211fd5f0bc4d134f4c6fef096e955629 Mon Sep 17 00:00:00 2001 From: Taureon <45183108+Taureon@users.noreply.github.com> Date: Sat, 14 Mar 2026 09:37:12 +0000 Subject: [PATCH] Update ChannelWebhook.ts Signed-off-by: Taureon <45183108+Taureon@users.noreply.github.com> --- src/classes/ChannelWebhook.ts | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/classes/ChannelWebhook.ts b/src/classes/ChannelWebhook.ts index 2aa5b56a..76e2703e 100644 --- a/src/classes/ChannelWebhook.ts +++ b/src/classes/ChannelWebhook.ts @@ -1,10 +1,12 @@ -import { DataEditWebhook } from "stoat-api"; +import { DataEditWebhook, DataMessageSend } from "stoat-api"; import type { ChannelWebhookCollection } from "../collections/ChannelWebhookCollection.js"; import { hydrate } from "../hydration/index.js"; import type { Channel } from "./Channel.js"; import type { File } from "./File.js"; +import { Message } from "./Message.js"; +import { ulid } from "ulid"; /** * Channel Webhook Class @@ -101,4 +103,40 @@ export class ChannelWebhook { this.#collection.delete(this.id); } + + /** + * Send a message through this webhook + * @param data Either the message as a string or message sending route data + * @returns Sent message + */ + async sendMessage( + data: string | DataMessageSend, + idempotencyKey: string = ulid(), + ): Promise { + const msg: DataMessageSend = + typeof data === "string" ? { content: data } : data; + + // Mark as silent message + if (msg.content?.startsWith("@silent ")) { + msg.content = msg.content.substring(8); + msg.flags ||= 1; + msg.flags |= 1; + } + + const message = await this.#collection.client.api.post( + `/webhooks/${this.id as ""}/${this.token as ""}`, + msg, + { + headers: { + "Idempotency-Key": idempotencyKey, + }, + }, + ); + + return this.#collection.client.messages.getOrCreate( + message._id, + message, + true, + ); + } }