From 4333e6817229d98c9af2a6dd54c75f8eb4381be7 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 5 Sep 2022 11:33:59 +0200 Subject: [PATCH] feat: typescript and custom error support closes #5, #4 --- .gitignore | 3 +- Message.js | 53 ---- README.md | 3 +- package-lock.json | 456 ++++++++++++++++++++++++++++++----- package.json | 18 +- src/Message.ts | 88 +++++++ Webhook.js => src/Webhook.ts | 49 ++-- src/WebhookError.ts | 19 ++ src/index.ts | 2 + test/{test.js => test.ts} | 6 +- tsconfig.json | 12 + 11 files changed, 568 insertions(+), 141 deletions(-) delete mode 100644 Message.js create mode 100644 src/Message.ts rename Webhook.js => src/Webhook.ts (57%) create mode 100644 src/WebhookError.ts create mode 100644 src/index.ts rename test/{test.js => test.ts} (97%) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index b512c09..76add87 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +dist \ No newline at end of file diff --git a/Message.js b/Message.js deleted file mode 100644 index 80bed8b..0000000 --- a/Message.js +++ /dev/null @@ -1,53 +0,0 @@ -const centra = require('@aero/centra'); - -/** - * Represents a discord-message - */ -class Message { - /** - * Represents a discord-message - * @param {Object} messageData Raw API-Data from Discord - * @param {URL} webhookurl Webhook-URL this message belongs to - */ - constructor(messageData, webhookurl) { - this.webhookUrl = webhookurl; - for (const data in messageData) { - this[data] = messageData[data]; - } - } - - /** - * Deletes this message - * @return {Promise} - */ - async delete() { - const res = await centra(this.webhookUrl + `/messages/${this.id}`, 'DELETE') - .header('Content-Type', 'application/json') - .send('form'); - if (res.statusCode !== 204) throw new Error(`Something went wrong while deleting the message ${this.id} with the webhook ${this.webhookUrl}. Here is the answer from discord: ` + res.body.toString()); - return true; - } - - /** - * Edits this message - * @param {String} content Content of this message - * @param {Array} embeds Array of [Embed-Objects](https://discord.com/developers/docs/resources/channel#embed-object) - * @param {Object} allowedMentions [Allowed-Mentions-Object](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) - * @param {Array} components [Message-Component-Object](https://discord.com/developers/docs/interactions/message-components#component-object). ⚠ This will only work, if your webhook is owned by an application. - * @return {Promise} New message - */ - async edit(content, embeds = [], allowedMentions = {}, components = []) { - const res = await centra(this.webhookUrl + `/messages/${this.id}`, 'PATCH').body({ - content: content, - embeds: embeds, - allowed_mentions: allowedMentions, - components - }) - .header('Content-Type', 'application/json') - .send('form'); - if (res.statusCode === 204) throw new Error(`Something went wrong while editing the message ${this.id} with the webhook ${this.webhookUrl}. Here is the answer from discord: ` + res.body.toString()); - return new Message(JSON.parse(res.body.toString()), this.webhookUrl); - } -} - -module.exports.Message = Message; \ No newline at end of file diff --git a/README.md b/README.md index 045c108..2ce46c5 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ # Features +* New: TypeScript support * Send webhook messages * Edit webhook messages * Delete webhook messages @@ -56,4 +57,4 @@ Please create [a discussion](https://github.com/SCDerox/simple-discord-webhooks/ Feel free to create any issues and PRs in our [github repository](https://github.com/SCDerox/simple-discord-webhooks) if you want to contribute. -© Simon Csaba, 22021 | mail[at]scderox.de \ No newline at end of file +© Simon Csaba, 2022 | mail[at]scderox.de \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 53e02a5..d65b1dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,320 @@ { "name": "simple-discord-webhooks", - "version": "1.1.1", - "lockfileVersion": 1, + "version": "2.0.0", + "lockfileVersion": 2, "requires": true, - "dependencies": { - "@aero/centra": { + "packages": { + "": { + "name": "simple-discord-webhooks", + "version": "2.0.0", + "funding": [ + "https://github.com/scderox/simple-discord-webhooks?sponsor=1", + "https://paypal.me/therealscderox", + "https://bunq.me/scderox", + { + "type": "patreon", + "url": "https://www.patreon.com/scnetwork" + } + ], + "license": "MIT", + "dependencies": { + "centra": "2.5.0", + "discord-api-types": "^0.37.5" + }, + "devDependencies": { + "@pixi/jsdoc-template": "2.6.0", + "@types/centra": "2.2.0", + "jsdoc": "3.6.11" + } + }, + "node_modules/@babel/parser": { + "version": "7.15.7", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz", + "integrity": "sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==", + "dev": true, + "bin": { + "parser": "bin/babel-parser.js" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@pixi/jsdoc-template": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@pixi/jsdoc-template/-/jsdoc-template-2.6.0.tgz", + "integrity": "sha512-WsSGk2x0ywXQu4RqiKfBCnEbT1izRgVRSkHWSv8VUOQk96tvLOlayZdlCCfudrNmpPw9tJADsWPnAXkxSWFKRg==", + "dev": true, + "dependencies": { + "taffydb": "^2.7.2", + "underscore": "^1.7.0" + } + }, + "node_modules/@types/centra": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@types/centra/-/centra-2.2.0.tgz", + "integrity": "sha512-TUpM1QoIgXa2VL1LnWbTgde39+rRnsOtvb0v8ZrX8t51g8K1Wwu4HAEXcBg5a56GV5Vm/KazyE5+g4AW8YbKNg==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/linkify-it": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.2.tgz", + "integrity": "sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==", + "dev": true + }, + "node_modules/@types/markdown-it": { + "version": "12.2.3", + "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-12.2.3.tgz", + "integrity": "sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==", + "dev": true, + "dependencies": { + "@types/linkify-it": "*", + "@types/mdurl": "*" + } + }, + "node_modules/@types/mdurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-1.0.2.tgz", + "integrity": "sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==", + "dev": true + }, + "node_modules/@types/node": { + "version": "18.7.15", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.15.tgz", + "integrity": "sha512-XnjpaI8Bgc3eBag2Aw4t2Uj/49lLBSStHWfqKvIuXD7FIrZyMLWp8KuAFHAqxMZYTF9l08N1ctUn9YNybZJVmQ==", + "dev": true + }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, + "node_modules/bluebird": { + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", + "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==", + "dev": true + }, + "node_modules/catharsis": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/catharsis/-/catharsis-0.9.0.tgz", + "integrity": "sha512-prMTQVpcns/tzFgFVkVp6ak6RykZyWb3gu8ckUpd6YkTlacOd3DXGJjIpD4Q6zJirizvaiAjSSHlOsA+6sNh2A==", + "dev": true, + "dependencies": { + "lodash": "^4.17.15" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/centra": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/centra/-/centra-2.5.0.tgz", + "integrity": "sha512-CnSF1HD8vOOgNbE4P2fZEhdhfAohvpcF3DSdSvEcSHDAZvr+Xfw73isT8SXJJc3VMBqSwjXhr29/ikHUgFcypg==" + }, + "node_modules/discord-api-types": { + "version": "0.37.5", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.5.tgz", + "integrity": "sha512-RhzoX02jw2M+n/AU5K74KTM4J8Sn3ZImUJvoA4lh+SDcrqi1ddSjrafciF4bECj4rPc2vHwoyyTNgbUwE8vbpA==" + }, + "node_modules/entities": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", + "integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==", + "dev": true, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/escape-string-regexp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz", + "integrity": "sha512-UpzcLCXolUWcNu5HtVMHYdXJjArjsF9C0aNnquZYY4uW/Vu0miy5YoWvbV345HauVvcAUnpRuhMMcqTcGOY2+w==", + "dev": true, + "engines": { + "node": ">=8" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.8", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.8.tgz", + "integrity": "sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg==", + "dev": true + }, + "node_modules/js2xmlparser": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", + "integrity": "sha512-6n4D8gLlLf1n5mNLQPRfViYzu9RATblzPEtm1SthMX1Pjao0r9YI9nw7ZIfRxQMERS87mcswrg+r/OYrPRX6jA==", + "dev": true, + "dependencies": { + "xmlcreate": "^2.0.4" + } + }, + "node_modules/jsdoc": { + "version": "3.6.11", + "resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-3.6.11.tgz", + "integrity": "sha512-8UCU0TYeIYD9KeLzEcAu2q8N/mx9O3phAGl32nmHlE0LpaJL71mMkP4d+QE5zWfNt50qheHtOZ0qoxVrsX5TUg==", + "dev": true, + "dependencies": { + "@babel/parser": "^7.9.4", + "@types/markdown-it": "^12.2.3", + "bluebird": "^3.7.2", + "catharsis": "^0.9.0", + "escape-string-regexp": "^2.0.0", + "js2xmlparser": "^4.0.2", + "klaw": "^3.0.0", + "markdown-it": "^12.3.2", + "markdown-it-anchor": "^8.4.1", + "marked": "^4.0.10", + "mkdirp": "^1.0.4", + "requizzle": "^0.2.3", + "strip-json-comments": "^3.1.0", + "taffydb": "2.6.2", + "underscore": "~1.13.2" + }, + "bin": { + "jsdoc": "jsdoc.js" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/jsdoc/node_modules/taffydb": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/taffydb/-/taffydb-2.6.2.tgz", + "integrity": "sha1-fLy2S1oUG2ou/CxdLGe04VCyomg=", + "dev": true + }, + "node_modules/klaw": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/klaw/-/klaw-3.0.0.tgz", + "integrity": "sha512-0Fo5oir+O9jnXu5EefYbVK+mHMBeEVEy2cmctR1O1NECcCkPRreJKrS6Qt/j3KC2C148Dfo9i3pCmCMsdqGr0g==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.1.9" + } + }, + "node_modules/linkify-it": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-3.0.3.tgz", + "integrity": "sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==", + "dev": true, + "dependencies": { + "uc.micro": "^1.0.1" + } + }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, + "node_modules/markdown-it": { + "version": "12.3.2", + "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-12.3.2.tgz", + "integrity": "sha512-TchMembfxfNVpHkbtriWltGWc+m3xszaRD0CZup7GFFhzIgQqxIfn3eGj1yZpfuflzPvfkt611B2Q/Bsk1YnGg==", + "dev": true, + "dependencies": { + "argparse": "^2.0.1", + "entities": "~2.1.0", + "linkify-it": "^3.0.1", + "mdurl": "^1.0.1", + "uc.micro": "^1.0.5" + }, + "bin": { + "markdown-it": "bin/markdown-it.js" + } + }, + "node_modules/markdown-it-anchor": { + "version": "8.6.4", + "resolved": "https://registry.npmjs.org/markdown-it-anchor/-/markdown-it-anchor-8.6.4.tgz", + "integrity": "sha512-Ul4YVYZNxMJYALpKtu+ZRdrryYt/GlQ5CK+4l1bp/gWXOG2QWElt6AqF3Mih/wfUKdZbNAZVXGR73/n6U/8img==", + "dev": true, + "peerDependencies": { + "@types/markdown-it": "*", + "markdown-it": "*" + } + }, + "node_modules/marked": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-4.1.0.tgz", + "integrity": "sha512-+Z6KDjSPa6/723PQYyc1axYZpYYpDnECDaU6hkaf5gqBieBkMKYReL5hteF2QizhlMbgbo8umXl/clZ67+GlsA==", + "dev": true, + "bin": { + "marked": "bin/marked.js" + }, + "engines": { + "node": ">= 12" + } + }, + "node_modules/mdurl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", + "integrity": "sha512-/sKlQJCBYVY9Ers9hqzKou4H6V5UWc/M59TH2dvkt+84itfnq7uFOMLpOiOS4ujvHP4etln18fmIxA5R5fll0g==", + "dev": true + }, + "node_modules/mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true, + "bin": { + "mkdirp": "bin/cmd.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/requizzle": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/requizzle/-/requizzle-0.2.3.tgz", + "integrity": "sha512-YanoyJjykPxGHii0fZP0uUPEXpvqfBDxWV7s6GKAiiOsiqhX6vHNyW3Qzdmqp/iq/ExbhaGbVrjB4ruEVSM4GQ==", + "dev": true, + "dependencies": { + "lodash": "^4.17.14" + } + }, + "node_modules/strip-json-comments": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", + "dev": true, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/taffydb": { + "version": "2.7.3", + "resolved": "https://registry.npmjs.org/taffydb/-/taffydb-2.7.3.tgz", + "integrity": "sha1-KtNxaWKUmPylvIQkMJbTzeDsOjQ=", + "dev": true + }, + "node_modules/uc.micro": { "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@aero/centra/-/centra-1.0.6.tgz", - "integrity": "sha512-e7sUh/VjugT8LsHb4/hcbmhaVVi9deWWPQnlMnU0RAnF8+iCdGdCcKvoHnDNl/ELykwjpbKpBYbWPx0yKZaDpw==" + "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", + "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==", + "dev": true + }, + "node_modules/underscore": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.4.tgz", + "integrity": "sha512-BQFnUDuAQ4Yf/cYY5LNrK9NCJFKriaRbD9uR1fTeXnBeoa97W0i41qkZfGO9pSo8I5KzjAcSY2XYtdf0oKd7KQ==", + "dev": true }, + "node_modules/xmlcreate": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/xmlcreate/-/xmlcreate-2.0.4.tgz", + "integrity": "sha512-nquOebG4sngPmGPICTS5EnxqhKbCmz5Ox5hsszI2T6U5qdrJizBc+0ilYSEjTSzU0yZcmvppztXe/5Al5fUwdg==", + "dev": true + } + }, + "dependencies": { "@babel/parser": { "version": "7.15.7", "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz", @@ -25,15 +331,49 @@ "underscore": "^1.7.0" } }, - "argparse": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", - "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "@types/centra": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@types/centra/-/centra-2.2.0.tgz", + "integrity": "sha512-TUpM1QoIgXa2VL1LnWbTgde39+rRnsOtvb0v8ZrX8t51g8K1Wwu4HAEXcBg5a56GV5Vm/KazyE5+g4AW8YbKNg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/linkify-it": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/linkify-it/-/linkify-it-3.0.2.tgz", + "integrity": "sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==", + "dev": true + }, + "@types/markdown-it": { + "version": "12.2.3", + "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-12.2.3.tgz", + "integrity": "sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==", "dev": true, "requires": { - "sprintf-js": "~1.0.2" + "@types/linkify-it": "*", + "@types/mdurl": "*" } }, + "@types/mdurl": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@types/mdurl/-/mdurl-1.0.2.tgz", + "integrity": "sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==", + "dev": true + }, + "@types/node": { + "version": "18.7.15", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.15.tgz", + "integrity": "sha512-XnjpaI8Bgc3eBag2Aw4t2Uj/49lLBSStHWfqKvIuXD7FIrZyMLWp8KuAFHAqxMZYTF9l08N1ctUn9YNybZJVmQ==", + "dev": true + }, + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", + "dev": true + }, "bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -49,10 +389,20 @@ "lodash": "^4.17.15" } }, + "centra": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/centra/-/centra-2.5.0.tgz", + "integrity": "sha512-CnSF1HD8vOOgNbE4P2fZEhdhfAohvpcF3DSdSvEcSHDAZvr+Xfw73isT8SXJJc3VMBqSwjXhr29/ikHUgFcypg==" + }, + "discord-api-types": { + "version": "0.37.5", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.5.tgz", + "integrity": "sha512-RhzoX02jw2M+n/AU5K74KTM4J8Sn3ZImUJvoA4lh+SDcrqi1ddSjrafciF4bECj4rPc2vHwoyyTNgbUwE8vbpA==" + }, "entities": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/entities/-/entities-2.0.3.tgz", - "integrity": "sha512-MyoZ0jgnLvB2X3Lg5HqpFmn1kybDiIfEQmKzTb5apr51Rb+T3KdmMiqa70T+bhGnyv7bQ6WMj2QMHpGMmlrUYQ==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-2.1.0.tgz", + "integrity": "sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==", "dev": true }, "escape-string-regexp": { @@ -68,34 +418,35 @@ "dev": true }, "js2xmlparser": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.1.tgz", - "integrity": "sha512-KrPTolcw6RocpYjdC7pL7v62e55q7qOMHvLX1UCLc5AAS8qeJ6nukarEJAF2KL2PZxlbGueEbINqZR2bDe/gUw==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/js2xmlparser/-/js2xmlparser-4.0.2.tgz", + "integrity": "sha512-6n4D8gLlLf1n5mNLQPRfViYzu9RATblzPEtm1SthMX1Pjao0r9YI9nw7ZIfRxQMERS87mcswrg+r/OYrPRX6jA==", "dev": true, "requires": { - "xmlcreate": "^2.0.3" + "xmlcreate": "^2.0.4" } }, "jsdoc": { - "version": "3.6.7", - "resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-3.6.7.tgz", - "integrity": "sha512-sxKt7h0vzCd+3Y81Ey2qinupL6DpRSZJclS04ugHDNmRUXGzqicMJ6iwayhSA0S0DwwX30c5ozyUthr1QKF6uw==", + "version": "3.6.11", + "resolved": "https://registry.npmjs.org/jsdoc/-/jsdoc-3.6.11.tgz", + "integrity": "sha512-8UCU0TYeIYD9KeLzEcAu2q8N/mx9O3phAGl32nmHlE0LpaJL71mMkP4d+QE5zWfNt50qheHtOZ0qoxVrsX5TUg==", "dev": true, "requires": { "@babel/parser": "^7.9.4", + "@types/markdown-it": "^12.2.3", "bluebird": "^3.7.2", "catharsis": "^0.9.0", "escape-string-regexp": "^2.0.0", - "js2xmlparser": "^4.0.1", + "js2xmlparser": "^4.0.2", "klaw": "^3.0.0", - "markdown-it": "^10.0.0", - "markdown-it-anchor": "^5.2.7", - "marked": "^2.0.3", + "markdown-it": "^12.3.2", + "markdown-it-anchor": "^8.4.1", + "marked": "^4.0.10", "mkdirp": "^1.0.4", "requizzle": "^0.2.3", "strip-json-comments": "^3.1.0", "taffydb": "2.6.2", - "underscore": "~1.13.1" + "underscore": "~1.13.2" }, "dependencies": { "taffydb": { @@ -116,9 +467,9 @@ } }, "linkify-it": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-2.2.0.tgz", - "integrity": "sha512-GnAl/knGn+i1U/wjBz3akz2stz+HrHLsxMwHQGofCDfPvlf+gDKN58UtfmUquTY4/MXeE2x7k19KQmeoZi94Iw==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-3.0.3.tgz", + "integrity": "sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==", "dev": true, "requires": { "uc.micro": "^1.0.1" @@ -131,34 +482,35 @@ "dev": true }, "markdown-it": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-10.0.0.tgz", - "integrity": "sha512-YWOP1j7UbDNz+TumYP1kpwnP0aEa711cJjrAQrzd0UXlbJfc5aAq0F/PZHjiioqDC1NKgvIMX+o+9Bk7yuM2dg==", + "version": "12.3.2", + "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-12.3.2.tgz", + "integrity": "sha512-TchMembfxfNVpHkbtriWltGWc+m3xszaRD0CZup7GFFhzIgQqxIfn3eGj1yZpfuflzPvfkt611B2Q/Bsk1YnGg==", "dev": true, "requires": { - "argparse": "^1.0.7", - "entities": "~2.0.0", - "linkify-it": "^2.0.0", + "argparse": "^2.0.1", + "entities": "~2.1.0", + "linkify-it": "^3.0.1", "mdurl": "^1.0.1", "uc.micro": "^1.0.5" } }, "markdown-it-anchor": { - "version": "5.3.0", - "resolved": "https://registry.npmjs.org/markdown-it-anchor/-/markdown-it-anchor-5.3.0.tgz", - "integrity": "sha512-/V1MnLL/rgJ3jkMWo84UR+K+jF1cxNG1a+KwqeXqTIJ+jtA8aWSHuigx8lTzauiIjBDbwF3NcWQMotd0Dm39jA==", - "dev": true + "version": "8.6.4", + "resolved": "https://registry.npmjs.org/markdown-it-anchor/-/markdown-it-anchor-8.6.4.tgz", + "integrity": "sha512-Ul4YVYZNxMJYALpKtu+ZRdrryYt/GlQ5CK+4l1bp/gWXOG2QWElt6AqF3Mih/wfUKdZbNAZVXGR73/n6U/8img==", + "dev": true, + "requires": {} }, "marked": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/marked/-/marked-2.1.3.tgz", - "integrity": "sha512-/Q+7MGzaETqifOMWYEA7HVMaZb4XbcRfaOzcSsHZEith83KGlvaSG33u0SKu89Mj5h+T8V2hM+8O45Qc5XTgwA==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/marked/-/marked-4.1.0.tgz", + "integrity": "sha512-+Z6KDjSPa6/723PQYyc1axYZpYYpDnECDaU6hkaf5gqBieBkMKYReL5hteF2QizhlMbgbo8umXl/clZ67+GlsA==", "dev": true }, "mdurl": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", - "integrity": "sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=", + "integrity": "sha512-/sKlQJCBYVY9Ers9hqzKou4H6V5UWc/M59TH2dvkt+84itfnq7uFOMLpOiOS4ujvHP4etln18fmIxA5R5fll0g==", "dev": true }, "mkdirp": { @@ -176,12 +528,6 @@ "lodash": "^4.17.14" } }, - "sprintf-js": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", - "dev": true - }, "strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -201,15 +547,15 @@ "dev": true }, "underscore": { - "version": "1.13.1", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.1.tgz", - "integrity": "sha512-hzSoAVtJF+3ZtiFX0VgfFPHEDRm7Y/QPjGyNo4TVdnDTdft3tr8hEkD25a1jC+TjTuE7tkHGKkhwCgs9dgBB2g==", + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.4.tgz", + "integrity": "sha512-BQFnUDuAQ4Yf/cYY5LNrK9NCJFKriaRbD9uR1fTeXnBeoa97W0i41qkZfGO9pSo8I5KzjAcSY2XYtdf0oKd7KQ==", "dev": true }, "xmlcreate": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/xmlcreate/-/xmlcreate-2.0.3.tgz", - "integrity": "sha512-HgS+X6zAztGa9zIK3Y3LXuJes33Lz9x+YyTxgrkIdabu2vqcGOWwdfCpf1hWLRrd553wd4QCDf6BBO6FfdsRiQ==", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/xmlcreate/-/xmlcreate-2.0.4.tgz", + "integrity": "sha512-nquOebG4sngPmGPICTS5EnxqhKbCmz5Ox5hsszI2T6U5qdrJizBc+0ilYSEjTSzU0yZcmvppztXe/5Al5fUwdg==", "dev": true } } diff --git a/package.json b/package.json index 2f6b3ab..d221b91 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { "name": "simple-discord-webhooks", - "version": "1.1.1", - "main": "Webhook.js", + "version": "2.0.0", "description": "Very simple package to send, edit and delete Discord Webhook Messages", "scripts": { "test": "node test/test.js" }, + "main": "dist/index.js", + "types": "dist/index.d.ts", + "files": [ + "/dist" + ], "keywords": [ "discord", "discord-webhook", @@ -35,10 +39,12 @@ "url": "https://github.com/SCDerox/simple-discord-webhooks" }, "dependencies": { - "@aero/centra": "^1.0.6" + "centra": "2.5.0", + "discord-api-types": "^0.37.5" }, "devDependencies": { - "@pixi/jsdoc-template": "^2.6.0", - "jsdoc": "^3.6.7" + "@pixi/jsdoc-template": "2.6.0", + "jsdoc": "3.6.11", + "@types/centra": "2.2.0" } -} +} \ No newline at end of file diff --git a/src/Message.ts b/src/Message.ts new file mode 100644 index 0000000..cc6d970 --- /dev/null +++ b/src/Message.ts @@ -0,0 +1,88 @@ +import {Snowflake} from "discord-api-types/globals"; +import { + APIActionRowComponent, + APIAllowedMentions, + APIAttachment, + APIChannelMention, + APIEmbed, + APIMessage, + APIMessageActionRowComponent, + APIMessageActivity, + APIUser, + MessageFlags, + MessageType +} from "discord-api-types/v10"; +import {WebhookError} from "./WebhookError"; + +const centra = require('centra'); + +/** + * Represents a discord-message + */ +export class Message implements APIMessage { + id: Snowflake; + webhookUrl: URL; + + channel_id: string; + author: APIUser; + content: string; + timestamp: string; + edited_timestamp: string; + tts: boolean; + mention_everyone: boolean; + mentions: APIUser[]; + mention_roles: string[]; + mention_channels?: APIChannelMention[]; + attachments: APIAttachment[]; + embeds: APIEmbed[]; + pinned: boolean; + webhook_id?: string; + type: MessageType; + activity?: APIMessageActivity; + flags?: MessageFlags; + components?: APIActionRowComponent[]; + + /** + * Represents a discord-message + * @param {Object} messageData Raw API-Data from Discord + * @param {URL} webhookurl Webhook-URL this message belongs to + */ + constructor(messageData, webhookurl) { + this.webhookUrl = webhookurl; + for (const data in messageData) { + this[data] = messageData[data]; + } + } + + /** + * Deletes this message + * @return {Promise} + */ + async delete() { + const res = await centra(this.webhookUrl + `/messages/${this.id}`, 'DELETE') + .header('Content-Type', 'application/json') + .send(); + if (res.statusCode !== 204) throw new WebhookError(JSON.parse(res.body.toString())); + return true; + } + + /** + * Edits this message + * @param {String} content Content of this message + * @param {APIEmbed} embeds Array of [embeds](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIEmbed) attached to this message + * @param {Object} allowedMentions [Allowed-Mentions-Object](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIAllowedMentions) + * @param {Array} components Array of [Message-Component-Action-Rows](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIActionRowComponent). ⚠ Most components will only work, if the webhook is owned by an application. * @return {Promise} New message + */ + async edit(content, embeds: APIEmbed[] = [], allowedMentions: APIAllowedMentions = {}, components: APIActionRowComponent[] = []) { + const res = await centra(this.webhookUrl + `/messages/${this.id}`, 'PATCH').body({ + content: content, + embeds: embeds, + allowed_mentions: allowedMentions, + components + }) + .header('Content-Type', 'application/json') + .send(); + if (res.statusCode === 204) throw new WebhookError(JSON.parse(res.body.toString())); + return new Message(JSON.parse(res.body.toString()), this.webhookUrl); + } +} \ No newline at end of file diff --git a/Webhook.js b/src/Webhook.ts similarity index 57% rename from Webhook.js rename to src/Webhook.ts index 0b78e8d..e27e4c3 100644 --- a/Webhook.js +++ b/src/Webhook.ts @@ -1,17 +1,24 @@ -const centra = require('@aero/centra'); -const {Message} = require('./Message'); +import {WebhookError} from "./WebhookError"; +import {Message} from './Message' +import {APIActionRowComponent, APIAllowedMentions, APIEmbed, APIMessageActionRowComponent} from 'discord-api-types/v10' + +const centra = require('centra') /** * Represents a webhook */ -class Webhook { +export class Webhook { + url: URL; + username?: String; + avatarUrl?: String; + /** * Represents a new webhook * @param {URL} url Webhook-URL * @param {String} username Username of the webhook * @param {URL} avatarUrl URL to a avatar */ - constructor(url, username = null, avatarUrl = null) { + constructor(url: URL, username?: String, avatarUrl?: String) { this.url = url; this.username = username; this.avatarUrl = avatarUrl; @@ -20,13 +27,13 @@ class Webhook { /** * Creates a new message as the webhook * @param {String} content Content of this message - * @param {Array} embeds Array of [Embed-Objects](https://discord.com/developers/docs/resources/channel#embed-object) - * @param {Object} allowedMentions [Allowed-Mentions-Object](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) - * @param {Boolean} tts If enabled, discord will read out the messages to everyone who hase this channel open - * @param {Array} components [Message-Component-Object](https://discord.com/developers/docs/interactions/message-components#component-object). ⚠ This will only work, if your webhook is owned by an application. + * @param {APIEmbed} embeds Array of [embeds](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIEmbed) attached to this message + * @param {Object} allowedMentions [Allowed-Mentions-Object](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIAllowedMentions) + * @param {Boolean} tts If enabled, discord will read out the messages to everyone who has this channel open + * @param {Array} components Array of [Message-Component-Action-Rows](https://discord-api-types.dev/api/discord-api-types-v10/interface/APIActionRowComponent). ⚠ Most components will only work, if the webhook is owned by an application. * @return {Promise} New message */ - async send(content, embeds = [], allowedMentions = {}, tts = false, components = []) { + async send(content: String, embeds: APIEmbed[] = [], allowedMentions: APIAllowedMentions = {}, tts = false, components: APIActionRowComponent[] = []) { const res = await centra(this.url + '?wait=true', 'POST').body({ content: content, username: this.username, @@ -37,8 +44,8 @@ class Webhook { components }) .header('Content-Type', 'application/json') - .send('form'); - if (res.statusCode !== 200) throw new Error(`Something went wrong while sending while sending the webhook ${this.url}. Here is the answer from discord: ` + res.body.toString()) + .send(); + if (res.statusCode !== 200) throw new WebhookError(JSON.parse(res.body.toString())); return new Message(JSON.parse(res.body.toString()), this.url); } @@ -48,14 +55,14 @@ class Webhook { * @param {String} base64Avatar [Base64](https://en.wikipedia.org/wiki/Base64)-String of your image. If you don't know how to use this, please google Image to Base64 nodejs or File to Base 64 if you have an image file. * @return {Promise} */ - async edit(name = null, base64Avatar = null) { + async edit(name?: String, base64Avatar?: String) { const res = await centra(this.url, 'PATCH').body({ name, avatar: base64Avatar ? `data:image/jpeg;base64,${base64Avatar}` : null }) .header('Content-Type', 'application/json') - .send('form'); - if (res.statusCode !== 200) throw new Error(`Something went wrong while sending while sending the webhook ${this.url}. Here is the answer from discord: `+ res.body.toString()); + .send(); + if (res.statusCode !== 200) throw new WebhookError(JSON.parse(res.body.toString())); } /** @@ -63,8 +70,8 @@ class Webhook { * @return {Promise} */ async delete() { - const res = await centra(this.url, 'DELETE').body().send('form'); - if (res.statusCode !== 204) throw new Error(`Something went wrong while sending while sending the webhook ${this.url}. Here is the answer from discord: ` + res.body.toString()); + const res = await centra(this.url, 'DELETE').send(); + if (res.statusCode !== 204) throw new WebhookError(JSON.parse(res.body.toString())); } /** @@ -74,19 +81,17 @@ class Webhook { */ async fetchMessage(id) { const res = await centra(this.url + `/messages/${id}`, 'GET') - .send('form'); - if (res.statusCode !== 200) throw new Error(`Something went wrong while sending while sending the webhook ${this.url}. Here is the answer from discord: ` + res.body.toString()); + .send(); + if (res.statusCode !== 200) throw new WebhookError(JSON.parse(res.body.toString())); return new Message(JSON.parse(res.body.toString()), this.url) } /** - * Resolves a message by ID (will not include any content of this message. Only use this if you want to edit or delate already send messages without having to fetch the message again + * Resolves a message by ID (will not include any content of this message. Only use this if you want to edit or delete already send messages without having to fetch the message again * @param messageID * @return {Message} */ resolveMessageID(messageID) { return new Message({id: messageID}, this.url); } -} - -module.exports.Webhook = Webhook; \ No newline at end of file +} \ No newline at end of file diff --git a/src/WebhookError.ts b/src/WebhookError.ts new file mode 100644 index 0000000..02e3ce6 --- /dev/null +++ b/src/WebhookError.ts @@ -0,0 +1,19 @@ +import {RESTError} from "discord-api-types/v10"; +import {Webhook} from "./Webhook"; + +interface APIError { + code: number, + message: string +} + +export class WebhookError extends Error implements RESTError { + code: number; + message: string; + + constructor(data: APIError) { + super('Error:' + data.message); + this.code = data.code; + this.message = data.message; + Object.setPrototypeOf(this, Webhook.prototype); + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..ae60ecf --- /dev/null +++ b/src/index.ts @@ -0,0 +1,2 @@ +export {Webhook} from './Webhook' +export {Message} from './Message' \ No newline at end of file diff --git a/test/test.js b/test/test.ts similarity index 97% rename from test/test.js rename to test/test.ts index 8ee5a09..253b078 100644 --- a/test/test.js +++ b/test/test.ts @@ -1,8 +1,8 @@ -const {Webhook} = require('../Webhook'); +const {Webhook} = require('../src/index'); const webhook = new Webhook(process.env.WEBHOOK_URL); -webhook.fetchMessage('895006405122224169').then(m => { +webhook.fetchMessage('1016274891483390025').then(m => { console.log(m.content) }) @@ -10,7 +10,7 @@ webhook.send('This message should get edited (hopefully) soon').then(async (resu // return webhook.delete(); console.log('Successfully send new message!'); - await webhook.resolveMessageID('820311219432194068').edit(`I got edited!`); + await webhook.resolveMessageID('8201311219432194068').edit(`I got edited!`); console.log('Successfully resolved messageID and edited message!'); // Use https://base64.guru/converter/encode/image or https://www.npmjs.com/package/filetobase64 (not tested) to generate a base64 string of a image diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4a4bbe0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es2015", + "declaration": true, + "outDir": "./dist", + "allowSyntheticDefaultImports": true + }, + "include": [ + "src/**/*" + ] +} \ No newline at end of file