Spaces:
Sleeping
Sleeping
File size: 1,459 Bytes
e1dafe2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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<boolean>}
*/
async sendPlainText(content) {
const payload = {
content
};
return this.#sendPayload(payload);
}
/**
*
* @param {object} content
* @returns {Promise<boolean>}
*/
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);
}
} |