import { urlSchema } from "./schemas.js"; export class DiscordWebhook { #url; constructor(url) { this.#url = urlSchema.parse(url); } /** * * @param {object} payload * @returns {boolean} */ #sendPayload = async (payload) => { const response = await fetch(this.#url, { method: 'POST', body: JSON.stringify(payload), headers: { 'Content-Type': 'application/json' } }); const sent = response.status === 204; if (!sent) { console.error(await response.text()); } return sent; }; /** * @param {string} content * @returns {Promise} */ async sendPlainText(content) { const payload = { content }; return this.#sendPayload(payload); } /** * * @param {object} content * @returns {Promise} */ async sendEmbed(content) { const fields = []; for (const [label, value] of Object.entries(content)) { const field = { name: label, value: value }; fields.push(field); } const embed = { title: "Nuevo mensaje", fields }; const payload = { embeds: [embed] }; return this.#sendPayload(payload); } }