mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
fix(Webhook): throw an error if no token is available when it's required (#5798)
This commit is contained in:
@@ -290,7 +290,7 @@ class Client extends BaseClient {
|
|||||||
return this.api
|
return this.api
|
||||||
.webhooks(id, token)
|
.webhooks(id, token)
|
||||||
.get()
|
.get()
|
||||||
.then(data => new Webhook(this, data));
|
.then(data => new Webhook(this, { token, ...data }));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ const Messages = {
|
|||||||
INVALID_ELEMENT: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`,
|
INVALID_ELEMENT: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`,
|
||||||
|
|
||||||
WEBHOOK_MESSAGE: 'The message was not sent by a webhook.',
|
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',
|
MESSAGE_REFERENCE_MISSING: 'The message does not reference another message',
|
||||||
|
|
||||||
EMOJI_TYPE: 'Emoji must be a string or GuildEmoji/ReactionEmoji',
|
EMOJI_TYPE: 'Emoji must be a string or GuildEmoji/ReactionEmoji',
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const APIMessage = require('./APIMessage');
|
const APIMessage = require('./APIMessage');
|
||||||
const Channel = require('./Channel');
|
const Channel = require('./Channel');
|
||||||
|
const { Error } = require('../errors');
|
||||||
const { WebhookTypes } = require('../util/Constants');
|
const { WebhookTypes } = require('../util/Constants');
|
||||||
const DataResolver = require('../util/DataResolver');
|
const DataResolver = require('../util/DataResolver');
|
||||||
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
const SnowflakeUtil = require('../util/SnowflakeUtil');
|
||||||
@@ -29,7 +30,7 @@ class Webhook {
|
|||||||
this.name = data.name;
|
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
|
* @name Webhook#token
|
||||||
* @type {?string}
|
* @type {?string}
|
||||||
*/
|
*/
|
||||||
@@ -149,6 +150,8 @@ class Webhook {
|
|||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
async send(options) {
|
async send(options) {
|
||||||
|
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
|
||||||
|
|
||||||
let apiMessage;
|
let apiMessage;
|
||||||
|
|
||||||
if (options instanceof APIMessage) {
|
if (options instanceof APIMessage) {
|
||||||
@@ -194,6 +197,8 @@ class Webhook {
|
|||||||
* }).catch(console.error);
|
* }).catch(console.error);
|
||||||
*/
|
*/
|
||||||
sendSlackMessage(body) {
|
sendSlackMessage(body) {
|
||||||
|
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
|
||||||
|
|
||||||
return this.client.api
|
return this.client.api
|
||||||
.webhooks(this.id, this.token)
|
.webhooks(this.id, this.token)
|
||||||
.slack.post({
|
.slack.post({
|
||||||
@@ -237,6 +242,8 @@ class Webhook {
|
|||||||
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
|
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
|
||||||
*/
|
*/
|
||||||
async fetchMessage(message, cache = true) {
|
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();
|
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;
|
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
|
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
|
||||||
*/
|
*/
|
||||||
async editMessage(message, options) {
|
async editMessage(message, options) {
|
||||||
|
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
|
||||||
|
|
||||||
let apiMessage;
|
let apiMessage;
|
||||||
|
|
||||||
if (options instanceof APIMessage) apiMessage = options;
|
if (options instanceof APIMessage) apiMessage = options;
|
||||||
@@ -287,6 +296,8 @@ class Webhook {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async deleteMessage(message) {
|
async deleteMessage(message) {
|
||||||
|
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
|
||||||
|
|
||||||
await this.client.api
|
await this.client.api
|
||||||
.webhooks(this.id, this.token)
|
.webhooks(this.id, this.token)
|
||||||
.messages(typeof message === 'string' ? message : message.id)
|
.messages(typeof message === 'string' ? message : message.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user