mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: add typeguards to webhooks (#6850)
This commit is contained in:
@@ -395,6 +395,22 @@ class Webhook {
|
||||
return this.client.rest.cdn.Avatar(this.id, this.avatar, format, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not this webhook is a channel follower webhook.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isChannelFollower() {
|
||||
return this.type === WebhookTypes['Channel Follower'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not this webhook is an incoming webhook.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isIncoming() {
|
||||
return this.type === WebhookTypes.Incoming;
|
||||
}
|
||||
|
||||
static applyToClass(structure, ignore = []) {
|
||||
for (const prop of [
|
||||
'send',
|
||||
|
||||
5
typings/index.d.ts
vendored
5
typings/index.d.ts
vendored
@@ -2218,6 +2218,11 @@ export class Webhook extends WebhookMixin() {
|
||||
public sourceChannel: NewsChannel | APIPartialChannel | null;
|
||||
public token: string | null;
|
||||
public type: WebhookType;
|
||||
public isIncoming(): this is this & { token: string };
|
||||
public isChannelFollower(): this is this & {
|
||||
sourceGuild: Guild | APIPartialGuild;
|
||||
sourceChannel: NewsChannel | APIPartialChannel;
|
||||
};
|
||||
}
|
||||
|
||||
export class WebhookClient extends WebhookMixin(BaseClient) {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import {
|
||||
APIGuildMember,
|
||||
APIInteractionDataResolvedGuildMember,
|
||||
APIInteractionGuildMember,
|
||||
APIMessage,
|
||||
APIPartialChannel,
|
||||
APIPartialGuild,
|
||||
APIInteractionDataResolvedGuildMember,
|
||||
} from 'discord-api-types/v9';
|
||||
import {
|
||||
ApplicationCommand,
|
||||
@@ -478,7 +480,7 @@ client.on('messageReactionRemoveAll', async message => {
|
||||
// This is to check that stuff is the right type
|
||||
declare const assertIsMessage: (m: Promise<Message>) => void;
|
||||
|
||||
client.on('messageCreate', message => {
|
||||
client.on('messageCreate', async message => {
|
||||
const { channel } = message;
|
||||
assertIsMessage(channel.send('string'));
|
||||
assertIsMessage(channel.send({}));
|
||||
@@ -573,6 +575,22 @@ client.on('messageCreate', message => {
|
||||
},
|
||||
});
|
||||
|
||||
const webhook = await message.fetchWebhook();
|
||||
|
||||
if (webhook.isChannelFollower()) {
|
||||
assertType<Guild | APIPartialGuild>(webhook.sourceGuild);
|
||||
assertType<NewsChannel | APIPartialChannel>(webhook.sourceChannel);
|
||||
} else if (webhook.isIncoming()) {
|
||||
assertType<string>(webhook.token);
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
assertType<Guild | APIPartialGuild>(webhook.sourceGuild);
|
||||
// @ts-expect-error
|
||||
assertType<NewsChannel | APIPartialChannel>(webhook.sourceChannel);
|
||||
// @ts-expect-error
|
||||
assertType<string>(webhook.token);
|
||||
|
||||
channel.awaitMessageComponent({
|
||||
filter: i => {
|
||||
assertType<MessageComponentInteraction>(i);
|
||||
|
||||
Reference in New Issue
Block a user