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

types: generic for Webhook type #10188

Merged
merged 4 commits into from
Mar 31, 2024
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
64 changes: 27 additions & 37 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ export class Guild extends AnonymousGuild {
public fetchPreview(): Promise<GuildPreview>;
public fetchTemplates(): Promise<Collection<GuildTemplate['code'], GuildTemplate>>;
public fetchVanityData(): Promise<Vanity>;
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>>;
public fetchWelcomeScreen(): Promise<WelcomeScreen>;
public fetchWidget(): Promise<Widget>;
public fetchWidgetSettings(): Promise<GuildWidgetSettings>;
Expand Down Expand Up @@ -1476,7 +1476,7 @@ export class Guild extends AnonymousGuild {
export class GuildAuditLogs<Event extends GuildAuditLogsResolvable = AuditLogEvent> {
private constructor(guild: Guild, data: RawGuildAuditLogData);
private applicationCommands: Collection<Snowflake, ApplicationCommand>;
private webhooks: Collection<Snowflake, Webhook>;
private webhooks: Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>;
private integrations: Collection<Snowflake | string, Integration>;
private guildScheduledEvents: Collection<Snowflake, GuildScheduledEvent>;
private autoModerationRules: Collection<Snowflake, AutoModerationRule>;
Expand Down Expand Up @@ -3529,44 +3529,32 @@ export class VoiceState extends Base {
}

// tslint:disable-next-line no-empty-interface
export interface Webhook extends WebhookFields {}
export class Webhook {
export interface Webhook<Type extends WebhookType = WebhookType> extends WebhookFields {}
export class Webhook<Type extends WebhookType = WebhookType> {
private constructor(client: Client<true>, data?: RawWebhookData);
public avatar: string | null;
public avatarURL(options?: ImageURLOptions): string | null;
public channelId: Snowflake;
public readonly client: Client;
public guildId: Snowflake;
public name: string;
public owner: User | APIUser | null;
public sourceGuild: Guild | APIPartialGuild | null;
public sourceChannel: NewsChannel | APIPartialChannel | null;
public token: string | null;
public type: WebhookType;
public applicationId: Snowflake | null;
public owner: Type extends WebhookType.Incoming ? User | APIUser | null : User | APIUser;
public sourceGuild: Type extends WebhookType.ChannelFollower ? Guild | APIPartialGuild : null;
public sourceChannel: Type extends WebhookType.ChannelFollower ? NewsChannel | APIPartialChannel : null;
public token: Type extends WebhookType.Incoming
? string
: Type extends WebhookType.ChannelFollower
? null
: string | null;
public type: Type;
public applicationId: Type extends WebhookType.Application ? Snowflake : null;
public get channel(): TextChannel | VoiceChannel | NewsChannel | StageChannel | ForumChannel | MediaChannel | null;
public isUserCreated(): this is this & {
type: WebhookType.Incoming;
applicationId: null;
owner: User | APIUser;
};
public isApplicationCreated(): this is this & {
type: WebhookType.Application;
applicationId: Snowflake;
owner: User | APIUser;
};
public isIncoming(): this is this & {
type: WebhookType.Incoming;
token: string;
};
public isChannelFollower(): this is this & {
type: WebhookType.ChannelFollower;
sourceGuild: Guild | APIPartialGuild;
sourceChannel: NewsChannel | APIPartialChannel;
token: null;
applicationId: null;
public isUserCreated(): this is Webhook<WebhookType.Incoming> & {
owner: User | APIUser;
};
public isApplicationCreated(): this is Webhook<WebhookType.Application>;
public isIncoming(): this is Webhook<WebhookType.Incoming>;
public isChannelFollower(): this is Webhook<WebhookType.ChannelFollower>;

public editMessage(
message: MessageResolvable,
Expand Down Expand Up @@ -4192,14 +4180,16 @@ export class GuildChannelManager extends CachedManager<Snowflake, GuildBasedChan
options: GuildChannelCreateOptions & { type: Type },
): Promise<MappedGuildChannelTypes[Type]>;
public create(options: GuildChannelCreateOptions): Promise<TextChannel>;
public createWebhook(options: WebhookCreateOptions): Promise<Webhook>;
public createWebhook(options: WebhookCreateOptions): Promise<Webhook<WebhookType.Incoming>>;
public edit(channel: GuildChannelResolvable, data: GuildChannelEditOptions): Promise<GuildChannel>;
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<GuildBasedChannel | null>;
public fetch(
id?: undefined,
options?: BaseFetchOptions,
): Promise<Collection<Snowflake, NonThreadGuildBasedChannel | null>>;
public fetchWebhooks(channel: GuildChannelResolvable): Promise<Collection<Snowflake, Webhook>>;
public fetchWebhooks(
channel: GuildChannelResolvable,
): Promise<Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>>;
public setPosition(
channel: GuildChannelResolvable,
position: number,
Expand Down Expand Up @@ -4553,8 +4543,8 @@ export interface TextBasedChannelFields<InGuild extends boolean = boolean>
options?: MessageChannelCollectorOptionsParams<ComponentType, true>,
): InteractionCollector<MappedInteractionTypes[ComponentType]>;
createMessageCollector(options?: MessageCollectorOptions): MessageCollector;
createWebhook(options: ChannelWebhookCreateOptions): Promise<Webhook>;
fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
createWebhook(options: ChannelWebhookCreateOptions): Promise<Webhook<WebhookType.Incoming>>;
fetchWebhooks(): Promise<Collection<Snowflake, Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>>>;
sendTyping(): Promise<void>;
setRateLimitPerUser(rateLimitPerUser: number, reason?: string): Promise<this>;
setNSFW(nsfw?: boolean, reason?: string): Promise<this>;
Expand All @@ -4580,7 +4570,7 @@ export interface WebhookFields extends PartialWebhookFields {
get createdAt(): Date;
get createdTimestamp(): number;
delete(reason?: string): Promise<void>;
edit(options: WebhookEditOptions): Promise<Webhook>;
edit(options: WebhookEditOptions): Promise<this>;
sendSlackMessage(body: unknown): Promise<boolean>;
}

Expand Down Expand Up @@ -5736,7 +5726,7 @@ export interface GuildAuditLogsEntryExtraField {
export interface GuildAuditLogsEntryTargetField<TActionType extends GuildAuditLogsActionType> {
User: User | null;
Guild: Guild;
Webhook: Webhook;
Webhook: Webhook<WebhookType.ChannelFollower | WebhookType.Incoming>;
Invite: Invite;
Message: TActionType extends AuditLogEvent.MessageBulkDelete ? Guild | { id: Snowflake } : User;
Integration: Integration;
Expand Down Expand Up @@ -6337,7 +6327,7 @@ export type MessageTarget =
| TextBasedChannel
| User
| GuildMember
| Webhook
| Webhook<WebhookType.Incoming>
| WebhookClient
| Message
| MessageManager;
Expand Down
4 changes: 4 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
APIChannelSelectComponent,
APIMentionableSelectComponent,
APIModalInteractionResponseCallbackData,
WebhookType,
} from 'discord-api-types/v10';
import {
ApplicationCommand,
Expand Down Expand Up @@ -538,8 +539,10 @@ client.on('messageCreate', async message => {
if (webhook.isChannelFollower()) {
expectAssignable<Guild | APIPartialGuild>(webhook.sourceGuild);
expectAssignable<NewsChannel | APIPartialChannel>(webhook.sourceChannel);
expectType<Webhook<WebhookType.ChannelFollower>>(webhook);
} else if (webhook.isIncoming()) {
expectType<string>(webhook.token);
expectType<Webhook<WebhookType.Incoming>>(webhook);
}

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

expectType<Promise<APIMessage>>(webhookClient.send('content'));
expectType<Promise<APIMessage>>(webhookClient.editMessage(snowflake, 'content'));
Expand Down
Loading