UNPKG

@vermaysha/discord-webhook

Version:

Discord Webhook built using TypeScript which supports Browser and Node

93 lines (92 loc) 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Webhook = void 0; const tslib_1 = require("tslib"); const axios_1 = tslib_1.__importDefault(require("axios")); const Request_1 = require("./Request"); class Webhook { constructor(webhookUrl) { this.client = new Request_1.Request(axios_1.default.create({ baseURL: webhookUrl, })); } setUsername(username) { this.username = username; return this; } setAvatarUrl(url) { this.avatar_url = url; return this; } setTTS(flag) { this.tts = flag; return this; } setContent(content) { this.content = content.substring(0, 2000); return this; } addEmbed(embed) { var _a; if (typeof this.embeds === 'undefined') this.embeds = [embed.toObject()]; else if (this.embeds.length <= 10) (_a = this.embeds) === null || _a === void 0 ? void 0 : _a.push(embed.toObject()); return this; } setFile(file) { this.file = file; return this; } send() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.client .send('POST', this.toObject()) .then((res) => res) .catch((err) => { var _a, _b, _c; throw new Error((_c = (_b = (_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : err); }); }); } modify(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.client .send('PATCH', options) .then((res) => res) .catch((err) => { var _a, _b, _c; throw new Error((_c = (_b = (_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : err); }); }); } get() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.client .send('GET', '') .then((res) => res) .catch((err) => { var _a, _b, _c; throw new Error((_c = (_b = (_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) !== null && _c !== void 0 ? _c : err); }); }); } isValid() { return tslib_1.__awaiter(this, void 0, void 0, function* () { return this.get() .then(() => true) .catch(() => false); }); } toObject() { return { username: this.username, avatar_url: this.avatar_url, tts: this.tts, content: this.content, file: this.file, embeds: this.embeds, }; } } exports.Webhook = Webhook;