Skip to content

Commit

Permalink
fix(Webhook): throw an error if no token is available when it's requi…
Browse files Browse the repository at this point in the history
…red (#5798)
  • Loading branch information
SpaceEEC committed Jun 10, 2021
1 parent e0ab836 commit eb98e33
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/client/Client.js
Expand Up @@ -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 }));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/errors/Messages.js
Expand Up @@ -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',
Expand Down
13 changes: 12 additions & 1 deletion src/structures/Webhook.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -287,6 +296,8 @@ class Webhook {
* @returns {Promise<void>}
*/
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)
Expand Down

0 comments on commit eb98e33

Please sign in to comment.