From 21af6d298890a399c3d173bbacff7cf02b70ebed Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Thu, 10 Jun 2021 13:29:13 +0200 Subject: [PATCH] fix(Webhook): throw an error if no token is available when it's required --- src/client/Client.js | 2 +- src/errors/Messages.js | 1 + src/structures/Webhook.js | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/client/Client.js b/src/client/Client.js index f20a1eded44b..7e291ef7815c 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -290,7 +290,7 @@ class Client extends BaseClient { return this.api .webhooks(id, token) .get() - .then(data => new Webhook(this, data)); + .then(data => new Webhook(this, { token, ...data })); } /** diff --git a/src/errors/Messages.js b/src/errors/Messages.js index bb08c34ff83f..70f713ddd947 100644 --- a/src/errors/Messages.js +++ b/src/errors/Messages.js @@ -92,6 +92,7 @@ const Messages = { INVALID_ELEMENT: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`, WEBHOOK_MESSAGE: 'The message was not sent by a webhook.', + WEBHOOK_TOKEN_UNAVAILABLE: 'This action requires a webhook token, but none is available.', MESSAGE_REFERENCE_MISSING: 'The message does not reference another message', EMOJI_TYPE: 'Emoji must be a string or GuildEmoji/ReactionEmoji', diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 96ecbd95a032..6c945fb5e2fe 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -2,6 +2,7 @@ const APIMessage = require('./APIMessage'); const Channel = require('./Channel'); +const { Error } = require('../errors'); const { WebhookTypes } = require('../util/Constants'); const DataResolver = require('../util/DataResolver'); const SnowflakeUtil = require('../util/SnowflakeUtil'); @@ -29,7 +30,7 @@ class Webhook { this.name = data.name; /** - * The token for the webhook + * The token for the webhook, unavailable for follower webhooks and webhooks owned by another application. * @name Webhook#token * @type {?string} */ @@ -149,6 +150,8 @@ class Webhook { * .catch(console.error); */ async send(options) { + if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); + let apiMessage; if (options instanceof APIMessage) { @@ -194,6 +197,8 @@ class Webhook { * }).catch(console.error); */ sendSlackMessage(body) { + if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); + return this.client.api .webhooks(this.id, this.token) .slack.post({ @@ -237,6 +242,8 @@ class Webhook { * {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned */ async fetchMessage(message, cache = true) { + if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); + const data = await this.client.api.webhooks(this.id, this.token).messages(message).get(); return this.client.channels?.cache.get(data.channel_id)?.messages.add(data, cache) ?? data; } @@ -249,6 +256,8 @@ class Webhook { * {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned */ async editMessage(message, options) { + if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); + let apiMessage; if (options instanceof APIMessage) apiMessage = options; @@ -287,6 +296,8 @@ class Webhook { * @returns {Promise} */ async deleteMessage(message) { + if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); + await this.client.api .webhooks(this.id, this.token) .messages(typeof message === 'string' ? message : message.id)