aboutsummaryrefslogtreecommitdiff
path: root/node_modules/discord.js/src/structures/Webhook.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/discord.js/src/structures/Webhook.js')
-rw-r--r--node_modules/discord.js/src/structures/Webhook.js200
1 files changed, 200 insertions, 0 deletions
diff --git a/node_modules/discord.js/src/structures/Webhook.js b/node_modules/discord.js/src/structures/Webhook.js
new file mode 100644
index 0000000..96984ff
--- /dev/null
+++ b/node_modules/discord.js/src/structures/Webhook.js
@@ -0,0 +1,200 @@
+const path = require('path');
+const escapeMarkdown = require('../util/EscapeMarkdown');
+
+/**
+ * Represents a webhook
+ */
+class Webhook {
+ constructor(client, dataOrID, token) {
+ if (client) {
+ /**
+ * The Client that instantiated the Webhook
+ * @name Webhook#client
+ * @type {Client}
+ * @readonly
+ */
+ Object.defineProperty(this, 'client', { value: client });
+ if (dataOrID) this.setup(dataOrID);
+ } else {
+ this.id = dataOrID;
+ this.token = token;
+ Object.defineProperty(this, 'client', { value: this });
+ }
+ }
+
+ setup(data) {
+ /**
+ * The name of the webhook
+ * @type {string}
+ */
+ this.name = data.name;
+
+ /**
+ * The token for the webhook
+ * @type {string}
+ */
+ this.token = data.token;
+
+ /**
+ * The avatar for the webhook
+ * @type {string}
+ */
+ this.avatar = data.avatar;
+
+ /**
+ * The ID of the webhook
+ * @type {string}
+ */
+ this.id = data.id;
+
+ /**
+ * The guild the webhook belongs to
+ * @type {string}
+ */
+ this.guildID = data.guild_id;
+
+ /**
+ * The channel the webhook belongs to
+ * @type {string}
+ */
+ this.channelID = data.channel_id;
+
+ /**
+ * The owner of the webhook
+ * @type {User}
+ */
+ if (data.user) this.owner = data.user;
+ }
+
+ /**
+ * Options that can be passed into sendMessage, sendTTSMessage, sendFile, sendCode
+ * @typedef {Object} WebhookMessageOptions
+ * @property {boolean} [tts=false] Whether or not the message should be spoken aloud
+ * @property {boolean} [disableEveryone=this.options.disableEveryone] Whether or not @everyone and @here
+ * should be replaced with plain-text
+ */
+
+ /**
+ * Send a message with this webhook
+ * @param {StringResolvable} content The content to send.
+ * @param {WebhookMessageOptions} [options={}] The options to provide.
+ * @returns {Promise<Message|Message[]>}
+ * @example
+ * // send a message
+ * webhook.sendMessage('hello!')
+ * .then(message => console.log(`Sent message: ${message.content}`))
+ * .catch(console.error);
+ */
+ sendMessage(content, options = {}) {
+ return this.client.rest.methods.sendWebhookMessage(this, content, options);
+ }
+
+ /**
+ * Send a raw slack message with this webhook
+ * @param {Object} body The raw body to send.
+ * @returns {Promise}
+ * @example
+ * // send a slack message
+ * webhook.sendSlackMessage({
+ * 'username': 'Wumpus',
+ * 'attachments': [{
+ * 'pretext': 'this looks pretty cool',
+ * 'color': '#F0F',
+ * 'footer_icon': 'http://snek.s3.amazonaws.com/topSnek.png',
+ * 'footer': 'Powered by sneks',
+ * 'ts': Date.now() / 1000
+ * }]
+ * }).catch(console.error);
+ */
+ sendSlackMessage(body) {
+ return this.client.rest.methods.sendSlackWebhookMessage(this, body);
+ }
+
+ /**
+ * Send a text-to-speech message with this webhook
+ * @param {StringResolvable} content The content to send
+ * @param {WebhookMessageOptions} [options={}] The options to provide
+ * @returns {Promise<Message|Message[]>}
+ * @example
+ * // send a TTS message
+ * webhook.sendTTSMessage('hello!')
+ * .then(message => console.log(`Sent tts message: ${message.content}`))
+ * .catch(console.error);
+ */
+ sendTTSMessage(content, options = {}) {
+ Object.assign(options, { tts: true });
+ return this.client.rest.methods.sendWebhookMessage(this, content, options);
+ }
+
+ /**
+ * Send a file with this webhook
+ * @param {BufferResolvable} attachment The file to send
+ * @param {string} [fileName="file.jpg"] The name and extension of the file
+ * @param {StringResolvable} [content] Text message to send with the attachment
+ * @param {WebhookMessageOptions} [options] The options to provide
+ * @returns {Promise<Message>}
+ */
+ sendFile(attachment, fileName, content, options = {}) {
+ if (!fileName) {
+ if (typeof attachment === 'string') {
+ fileName = path.basename(attachment);
+ } else if (attachment && attachment.path) {
+ fileName = path.basename(attachment.path);
+ } else {
+ fileName = 'file.jpg';
+ }
+ }
+ return this.client.resolver.resolveBuffer(attachment).then(file =>
+ this.client.rest.methods.sendWebhookMessage(this, content, options, {
+ file,
+ name: fileName,
+ })
+ );
+ }
+
+ /**
+ * Send a code block with this webhook
+ * @param {string} lang Language for the code block
+ * @param {StringResolvable} content Content of the code block
+ * @param {WebhookMessageOptions} options The options to provide
+ * @returns {Promise<Message|Message[]>}
+ */
+ sendCode(lang, content, options = {}) {
+ if (options.split) {
+ if (typeof options.split !== 'object') options.split = {};
+ if (!options.split.prepend) options.split.prepend = `\`\`\`${lang || ''}\n`;
+ if (!options.split.append) options.split.append = '\n```';
+ }
+ content = escapeMarkdown(this.client.resolver.resolveString(content), true);
+ return this.sendMessage(`\`\`\`${lang || ''}\n${content}\n\`\`\``, options);
+ }
+
+ /**
+ * Edit the webhook.
+ * @param {string} name The new name for the Webhook
+ * @param {BufferResolvable} avatar The new avatar for the Webhook.
+ * @returns {Promise<Webhook>}
+ */
+ edit(name = this.name, avatar) {
+ if (avatar) {
+ return this.client.resolver.resolveBuffer(avatar).then(file => {
+ const dataURI = this.client.resolver.resolveBase64(file);
+ return this.client.rest.methods.editWebhook(this, name, dataURI);
+ });
+ }
+ return this.client.rest.methods.editWebhook(this, name).then(data => {
+ this.setup(data);
+ return this;
+ });
+ }
+
+ /**
+ * Delete the webhook
+ * @returns {Promise}
+ */
+ delete() {
+ return this.client.rest.methods.deleteWebhook(this);
+ }
+}
+
+module.exports = Webhook;