Skip to content

Commit 980a2b7

Browse files
Qjuhkodiakhq[bot]
andauthored
types: generic for Webhook type (#10188)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 654f1a4 commit 980a2b7

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

packages/discord.js/typings/index.d.ts

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ export class Guild extends AnonymousGuild {
14341434
public fetchPreview(): Promise<GuildPreview>;
14351435
public fetchTemplates(): Promise<Collection<GuildTemplate['code'], GuildTemplate>>;
14361436
public fetchVanityData(): Promise<Vanity>;
1437-
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
1437+
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>>;
14381438
public fetchWelcomeScreen(): Promise<WelcomeScreen>;
14391439
public fetchWidget(): Promise<Widget>;
14401440
public fetchWidgetSettings(): Promise<GuildWidgetSettings>;
@@ -1476,7 +1476,7 @@ export class Guild extends AnonymousGuild {
14761476
export class GuildAuditLogs<Event extends GuildAuditLogsResolvable = AuditLogEvent> {
14771477
private constructor(guild: Guild, data: RawGuildAuditLogData);
14781478
private applicationCommands: Collection<Snowflake, ApplicationCommand>;
1479-
private webhooks: Collection<Snowflake, Webhook>;
1479+
private webhooks: Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>;
14801480
private integrations: Collection<Snowflake | string, Integration>;
14811481
private guildScheduledEvents: Collection<Snowflake, GuildScheduledEvent>;
14821482
private autoModerationRules: Collection<Snowflake, AutoModerationRule>;
@@ -3529,44 +3529,32 @@ export class VoiceState extends Base {
35293529
}
35303530

35313531
// tslint:disable-next-line no-empty-interface
3532-
export interface Webhook extends WebhookFields {}
3533-
export class Webhook {
3532+
export interface Webhook<Type extends WebhookType = WebhookType> extends WebhookFields {}
3533+
export class Webhook<Type extends WebhookType = WebhookType> {
35343534
private constructor(client: Client<true>, data?: RawWebhookData);
35353535
public avatar: string | null;
35363536
public avatarURL(options?: ImageURLOptions): string | null;
35373537
public channelId: Snowflake;
35383538
public readonly client: Client;
35393539
public guildId: Snowflake;
35403540
public name: string;
3541-
public owner: User | APIUser | null;
3542-
public sourceGuild: Guild | APIPartialGuild | null;
3543-
public sourceChannel: NewsChannel | APIPartialChannel | null;
3544-
public token: string | null;
3545-
public type: WebhookType;
3546-
public applicationId: Snowflake | null;
3541+
public owner: Type extends WebhookType.Incoming ? User | APIUser | null : User | APIUser;
3542+
public sourceGuild: Type extends WebhookType.ChannelFollower ? Guild | APIPartialGuild : null;
3543+
public sourceChannel: Type extends WebhookType.ChannelFollower ? NewsChannel | APIPartialChannel : null;
3544+
public token: Type extends WebhookType.Incoming
3545+
? string
3546+
: Type extends WebhookType.ChannelFollower
3547+
? null
3548+
: string | null;
3549+
public type: Type;
3550+
public applicationId: Type extends WebhookType.Application ? Snowflake : null;
35473551
public get channel(): TextChannel | VoiceChannel | NewsChannel | StageChannel | ForumChannel | MediaChannel | null;
3548-
public isUserCreated(): this is this & {
3549-
type: WebhookType.Incoming;
3550-
applicationId: null;
3551-
owner: User | APIUser;
3552-
};
3553-
public isApplicationCreated(): this is this & {
3554-
type: WebhookType.Application;
3555-
applicationId: Snowflake;
3556-
owner: User | APIUser;
3557-
};
3558-
public isIncoming(): this is this & {
3559-
type: WebhookType.Incoming;
3560-
token: string;
3561-
};
3562-
public isChannelFollower(): this is this & {
3563-
type: WebhookType.ChannelFollower;
3564-
sourceGuild: Guild | APIPartialGuild;
3565-
sourceChannel: NewsChannel | APIPartialChannel;
3566-
token: null;
3567-
applicationId: null;
3552+
public isUserCreated(): this is Webhook<WebhookType.Incoming> & {
35683553
owner: User | APIUser;
35693554
};
3555+
public isApplicationCreated(): this is Webhook<WebhookType.Application>;
3556+
public isIncoming(): this is Webhook<WebhookType.Incoming>;
3557+
public isChannelFollower(): this is Webhook<WebhookType.ChannelFollower>;
35703558

35713559
public editMessage(
35723560
message: MessageResolvable,
@@ -4192,14 +4180,16 @@ export class GuildChannelManager extends CachedManager<Snowflake, GuildBasedChan
41924180
options: GuildChannelCreateOptions & { type: Type },
41934181
): Promise<MappedGuildChannelTypes[Type]>;
41944182
public create(options: GuildChannelCreateOptions): Promise<TextChannel>;
4195-
public createWebhook(options: WebhookCreateOptions): Promise<Webhook>;
4183+
public createWebhook(options: WebhookCreateOptions): Promise<Webhook<WebhookType.Incoming>>;
41964184
public edit(channel: GuildChannelResolvable, data: GuildChannelEditOptions): Promise<GuildChannel>;
41974185
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<GuildBasedChannel | null>;
41984186
public fetch(
41994187
id?: undefined,
42004188
options?: BaseFetchOptions,
42014189
): Promise<Collection<Snowflake, NonThreadGuildBasedChannel | null>>;
4202-
public fetchWebhooks(channel: GuildChannelResolvable): Promise<Collection<Snowflake, Webhook>>;
4190+
public fetchWebhooks(
4191+
channel: GuildChannelResolvable,
4192+
): Promise<Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>>;
42034193
public setPosition(
42044194
channel: GuildChannelResolvable,
42054195
position: number,
@@ -4553,8 +4543,8 @@ export interface TextBasedChannelFields<InGuild extends boolean = boolean>
45534543
options?: MessageChannelCollectorOptionsParams<ComponentType, true>,
45544544
): InteractionCollector<MappedInteractionTypes[ComponentType]>;
45554545
createMessageCollector(options?: MessageCollectorOptions): MessageCollector;
4556-
createWebhook(options: ChannelWebhookCreateOptions): Promise<Webhook>;
4557-
fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
4546+
createWebhook(options: ChannelWebhookCreateOptions): Promise<Webhook<WebhookType.Incoming>>;
4547+
fetchWebhooks(): Promise<Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>>;
45584548
sendTyping(): Promise<void>;
45594549
setRateLimitPerUser(rateLimitPerUser: number, reason?: string): Promise<this>;
45604550
setNSFW(nsfw?: boolean, reason?: string): Promise<this>;
@@ -4580,7 +4570,7 @@ export interface WebhookFields extends PartialWebhookFields {
45804570
get createdAt(): Date;
45814571
get createdTimestamp(): number;
45824572
delete(reason?: string): Promise<void>;
4583-
edit(options: WebhookEditOptions): Promise<Webhook>;
4573+
edit(options: WebhookEditOptions): Promise<this>;
45844574
sendSlackMessage(body: unknown): Promise<boolean>;
45854575
}
45864576

@@ -5736,7 +5726,7 @@ export interface GuildAuditLogsEntryExtraField {
57365726
export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLogsActionType> {
57375727
User: User | null;
57385728
Guild: Guild;
5739-
Webhook: Webhook;
5729+
Webhook: Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>;
57405730
Invite: Invite;
57415731
Message: TActionType extends AuditLogEvent.MessageBulkDelete ? Guild | { id: Snowflake } : User;
57425732
Integration: Integration;
@@ -6337,7 +6327,7 @@ export type MessageTarget =
63376327
| TextBasedChannel
63386328
| User
63396329
| GuildMember
6340-
| Webhook
6330+
| Webhook<WebhookType.Incoming>
63416331
| WebhookClient
63426332
| Message
63436333
| MessageManager;

packages/discord.js/typings/index.test-d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
APIChannelSelectComponent,
3333
APIMentionableSelectComponent,
3434
APIModalInteractionResponseCallbackData,
35+
WebhookType,
3536
} from 'discord-api-types/v10';
3637
import {
3738
ApplicationCommand,
@@ -538,8 +539,10 @@ client.on('messageCreate', async message => {
538539
if (webhook.isChannelFollower()) {
539540
expectAssignable<Guild | APIPartialGuild>(webhook.sourceGuild);
540541
expectAssignable<NewsChannel | APIPartialChannel>(webhook.sourceChannel);
542+
expectType<Webhook<WebhookType.ChannelFollower>>(webhook);
541543
} else if (webhook.isIncoming()) {
542544
expectType<string>(webhook.token);
545+
expectType<Webhook<WebhookType.Incoming>>(webhook);
543546
}
544547

545548
expectNotType<Guild | APIPartialGuild>(webhook.sourceGuild);
@@ -2344,6 +2347,7 @@ declare const snowflake: Snowflake;
23442347
expectType<Promise<Message>>(webhook.send('content'));
23452348
expectType<Promise<Message>>(webhook.editMessage(snowflake, 'content'));
23462349
expectType<Promise<Message>>(webhook.fetchMessage(snowflake));
2350+
expectType<Promise<Webhook>>(webhook.edit({ name: 'name' }));
23472351

23482352
expectType<Promise<APIMessage>>(webhookClient.send('content'));
23492353
expectType<Promise<APIMessage>>(webhookClient.editMessage(snowflake, 'content'));

0 commit comments

Comments
 (0)