Skip to content

Commit 9f18cb2

Browse files
ckohenJiralite
andauthored
refactor(Webhooks)!: remove WebhookClient (#11266)
BREAKING CHANGE: WebhookClient has been removed, use @discordjs/core instead or fetch webhooks. Alternative solutions are in the works BREAKING CHANGE: `WebhookURLInvalid` is no longer an error code (obsolete). Co-authored-by: Jiralite <[email protected]>
1 parent 837af56 commit 9f18cb2

File tree

10 files changed

+32
-189
lines changed

10 files changed

+32
-189
lines changed

packages/discord.js/src/client/WebhookClient.js

Lines changed: 0 additions & 119 deletions
This file was deleted.

packages/discord.js/src/errors/ErrorCodes.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
*
7878
* @property {'WebhookMessage'} WebhookMessage
7979
* @property {'WebhookTokenUnavailable'} WebhookTokenUnavailable
80-
* @property {'WebhookURLInvalid'} WebhookURLInvalid
8180
* @property {'WebhookApplication'} WebhookApplication
8281
*
8382
* @property {'MessageReferenceMissing'} MessageReferenceMissing
@@ -212,7 +211,6 @@ const keys = [
212211

213212
'WebhookMessage',
214213
'WebhookTokenUnavailable',
215-
'WebhookURLInvalid',
216214
'WebhookApplication',
217215

218216
'MessageReferenceMissing',

packages/discord.js/src/errors/Messages.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const Messages = {
8282

8383
[ErrorCodes.WebhookMessage]: 'The message was not sent by a webhook.',
8484
[ErrorCodes.WebhookTokenUnavailable]: 'This action requires a webhook token, but none is available.',
85-
[ErrorCodes.WebhookURLInvalid]: 'The provided webhook URL is not valid.',
8685
[ErrorCodes.WebhookApplication]: 'This message webhook belongs to an application and cannot be fetched.',
8786

8887
[ErrorCodes.MessageReferenceMissing]: 'The message does not reference another message',

packages/discord.js/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ exports.Client = require('./client/Client.js').Client;
88
exports.Shard = require('./sharding/Shard.js').Shard;
99
exports.ShardClientUtil = require('./sharding/ShardClientUtil.js').ShardClientUtil;
1010
exports.ShardingManager = require('./sharding/ShardingManager.js').ShardingManager;
11-
exports.WebhookClient = require('./client/WebhookClient.js').WebhookClient;
1211

1312
// Errors
1413
exports.DiscordjsError = require('./errors/DJSError.js').DiscordjsError;

packages/discord.js/src/structures/MessagePayload.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ class MessagePayload {
4747
}
4848

4949
/**
50-
* Whether or not the target is a {@link Webhook} or a {@link WebhookClient}
50+
* Whether or not the target is a {@link Webhook}
5151
*
5252
* @type {boolean}
5353
* @readonly
5454
*/
5555
get isWebhook() {
5656
const { Webhook } = require('./Webhook.js');
57-
const { WebhookClient } = require('../client/WebhookClient.js');
58-
return this.target instanceof Webhook || this.target instanceof WebhookClient;
57+
return this.target instanceof Webhook;
5958
}
6059

6160
/**
@@ -302,7 +301,7 @@ exports.MessagePayload = MessagePayload;
302301
/**
303302
* A target for a message.
304303
*
305-
* @typedef {TextBasedChannels|ChannelManager|Webhook|WebhookClient|BaseInteraction|InteractionWebhook|
304+
* @typedef {TextBasedChannels|ChannelManager|Webhook|BaseInteraction|InteractionWebhook|
306305
* Message|MessageManager} MessageTarget
307306
*/
308307

packages/discord.js/src/structures/Webhook.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ class Webhook {
9595
/**
9696
* The owner of the webhook
9797
*
98-
* @type {?(User|APIUser)}
98+
* @type {?User}
9999
*/
100-
this.owner = this.client.users?._add(data.user) ?? data.user;
100+
this.owner = this.client.users._add(data.user);
101101
} else {
102102
this.owner ??= null;
103103
}
@@ -119,7 +119,7 @@ class Webhook {
119119
*
120120
* @type {?(Guild|APIGuild)}
121121
*/
122-
this.sourceGuild = this.client.guilds?.cache.get(data.source_guild.id) ?? data.source_guild;
122+
this.sourceGuild = this.client.guilds.cache.get(data.source_guild.id) ?? data.source_guild;
123123
} else {
124124
this.sourceGuild ??= null;
125125
}
@@ -130,7 +130,7 @@ class Webhook {
130130
*
131131
* @type {?(AnnouncementChannel|APIChannel)}
132132
*/
133-
this.sourceChannel = this.client.channels?.cache.get(data.source_channel?.id) ?? data.source_channel;
133+
this.sourceChannel = this.client.channels.cache.get(data.source_channel?.id) ?? data.source_channel;
134134
} else {
135135
this.sourceChannel ??= null;
136136
}
@@ -248,7 +248,6 @@ class Webhook {
248248
auth: false,
249249
});
250250

251-
if (!this.client.channels) return data;
252251
return (
253252
this.client.channels.cache.get(data.channel_id)?.messages._add(data, false) ??
254253
new (getMessage())(this.client, data)
@@ -345,7 +344,6 @@ class Webhook {
345344
auth: false,
346345
});
347346

348-
if (!this.client.channels) return data;
349347
return (
350348
this.client.channels.cache.get(data.channel_id)?.messages._add(data, false) ??
351349
new (getMessage())(this.client, data)
@@ -384,10 +382,7 @@ class Webhook {
384382
},
385383
);
386384

387-
const channelManager = this.client.channels;
388-
if (!channelManager) return data;
389-
390-
const messageManager = channelManager.cache.get(data.channel_id)?.messages;
385+
const messageManager = this.client.channels.cache.get(data.channel_id)?.messages;
391386
if (!messageManager) return new (getMessage())(this.client, data);
392387

393388
const existing = messageManager.cache.get(data.id);

packages/discord.js/src/util/Util.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,19 @@ function cleanCodeBlockContent(text) {
469469
return text.replaceAll('```', '`\u200B``');
470470
}
471471

472+
/**
473+
* Represents the credentials used for a webhook in the form of its id and token.
474+
*
475+
* @typedef {Object} WebhookDataIdWithToken
476+
* @property {Snowflake} id The webhook's id
477+
* @property {string} token The webhook's token
478+
*/
479+
472480
/**
473481
* Parses a webhook URL for the id and token.
474482
*
475483
* @param {string} url The URL to parse
476-
* @returns {?WebhookClientDataIdWithToken} `null` if the URL is invalid, otherwise the id and the token
484+
* @returns {?WebhookDataIdWithToken} `null` if the URL is invalid, otherwise the id and the token
477485
*/
478486
function parseWebhookURL(url) {
479487
const matches =

packages/discord.js/test/webhooktest.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const { setTimeout: sleep } = require('node:timers/promises');
77
const util = require('node:util');
88
const { GatewayIntentBits } = require('discord-api-types/v10');
99
const { fetch } = require('undici');
10+
const { Client, MessageAttachment, Embed } = require('../src/index.js');
1011
const { owner, token, webhookChannel, webhookToken } = require('./auth.js');
11-
const { Client, MessageAttachment, Embed, WebhookClient } = require('../src/index.js');
1212

1313
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
1414

@@ -84,30 +84,26 @@ client.on('messageCreate', async message => {
8484
if (message.author.id !== owner) return;
8585
const match = message.content.match(/^do (.+)$/);
8686
const hooks = [
87-
{ type: 'WebhookClient', hook: new WebhookClient({ id: webhookChannel, token: webhookToken }) },
8887
{ type: 'TextChannel#fetchWebhooks', hook: await message.channel.fetchWebhooks().then(x => x.first()) },
8988
{ type: 'Guild#fetchWebhooks', hook: await message.guild.fetchWebhooks().then(x => x.first()) },
9089
];
9190
if (match?.[1] === 'it') {
92-
/* eslint-disable no-await-in-loop */
9391
for (const { type, hook } of hooks) {
9492
for (const [i, test] of tests.entries()) {
9593
await message.channel.send(`**#${i}-Hook: ${type}**\n\`\`\`js\n${test.toString()}\`\`\``);
96-
await test(message, hook).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
94+
await test(message, hook).catch(error => message.channel.send(`Error!\n\`\`\`\n${error}\`\`\``));
9795
await sleep(1_000);
9896
}
9997
}
100-
/* eslint-enable no-await-in-loop */
10198
} else if (match) {
102-
const n = parseInt(match[1]) || 0;
99+
const n = Number.parseInt(match[1]) || 0;
103100
const test = tests.slice(n)[0];
104101
const i = tests.indexOf(test);
105-
/* eslint-disable no-await-in-loop */
102+
106103
for (const { type, hook } of hooks) {
107104
await message.channel.send(`**#${i}-Hook: ${type}**\n\`\`\`js\n${test.toString()}\`\`\``);
108-
await test(message, hook).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
105+
await test(message, hook).catch(error => message.channel.send(`Error!\n\`\`\`\n${error}\`\`\``));
109106
}
110-
/* eslint-enable no-await-in-loop */
111107
}
112108
});
113109

0 commit comments

Comments
 (0)