Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Webhook): throw an error if no token is available when it's required #5798

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/Client.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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