From 1355ebf50ae01969307852ae991aea28126a4c5e Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Thu, 28 Mar 2024 20:31:52 -0400 Subject: [PATCH] fix: exports & webhooks tsdocs --- package.json | 19 +------------ src/structures/Webhook.ts | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 2c58dbe..cac511c 100644 --- a/package.json +++ b/package.json @@ -70,22 +70,5 @@ "name": "socram03", "url": "https://github.com/socram03" } - ], - "exports": { - ".": "./lib/index.js", - "./api": "./lib/api/index.js", - "./builders": "./lib/builders/index.js", - "./cache": "./lib/cache/index.js", - "./client": "./lib/client/index.js", - "./commands": "./lib/commands/index.js", - "./common": "./lib/common/index.js", - "./components": "./lib/components/index.js", - "./events": "./lib/events/index.js", - "./langs": "./lib/langs/index.js", - "./structures": "./lib/structures/index.js", - "./websocket": "./lib/websocket/index.js", - "./collection": "./lib/collection.js", - "./package.json": "./package.json", - "./dapi": "./lib/types/index.js" - } + ] } diff --git a/src/structures/Webhook.ts b/src/structures/Webhook.ts index fd4f777..246f70e 100644 --- a/src/structures/Webhook.ts +++ b/src/structures/Webhook.ts @@ -1,3 +1,6 @@ +/** + * Represents a Discord webhook. + */ import type { APIWebhook, RESTGetAPIWebhookWithTokenMessageQuery, @@ -20,10 +23,21 @@ import { DiscordBase } from './extra/DiscordBase'; export interface Webhook extends DiscordBase, ObjectToLower> {} +/** + * Represents a Discord webhook. + */ export class Webhook extends DiscordBase { + /** The user associated with the webhook, if applicable. */ user?: User; + /** The source guild of the webhook, if applicable. */ sourceGuild?: Partial; + /** Methods related to interacting with messages through the webhook. */ messages!: ReturnType; + /** + * Constructs a new Webhook instance. + * @param client The Discord client instance. + * @param data The data representing the webhook. + */ constructor(client: UsingClient, data: APIWebhook) { super(client, data); @@ -40,16 +54,31 @@ export class Webhook extends DiscordBase { }); } + /** + * Fetches the guild associated with the webhook. + * @param force Whether to force fetching the guild even if it's already cached. + * @returns A promise that resolves to the guild associated with the webhook, or undefined if not applicable. + */ guild(force = false) { if (!this.sourceGuild?.id) return; return this.client.guilds.fetch(this.sourceGuild.id, force); } + /** + * Fetches the channel associated with the webhook. + * @param force Whether to force fetching the channel even if it's already cached. + * @returns A promise that resolves to the channel associated with the webhook, or undefined if not applicable. + */ async channel(force = false) { if (!this.sourceChannel?.id) return; return this.client.channels.fetch(this.sourceChannel.id, force); } + /** + * Retrieves the avatar URL of the webhook. + * @param options The image options for the avatar. + * @returns The avatar URL of the webhook, or null if no avatar is set. + */ avatarURL(options?: ImageOptions): string | null { if (!this.avatar) { return null; @@ -58,42 +87,71 @@ export class Webhook extends DiscordBase { return this.rest.cdn.avatar(this.id, this.avatar, options); } + /** + * Fetches the webhook data from the Discord API. + * @returns A promise that resolves to the fetched webhook data. + */ async fetch() { return this.client.webhooks.fetch(this.id, this.token); } + /** + * Edits the webhook. + * @param body The new webhook data. + * @param reason The reason for editing the webhook. + * @returns A promise that resolves when the webhook is successfully edited. + */ async edit(body: RESTPatchAPIWebhookJSONBody | RESTPatchAPIWebhookWithTokenJSONBody, reason?: string) { return this.client.webhooks.edit(this.id, body, { reason, token: this.token }); } + /** + * Deletes the webhook. + * @param reason The reason for deleting the webhook. + * @returns A promise that resolves when the webhook is successfully deleted. + */ delete(reason?: string) { return this.client.webhooks.delete(this.id, { token: this.token, reason }); } + /** + * Static methods related to interacting with messages through webhooks. + */ static messages({ client, webhookId, webhookToken }: MethodContext<{ webhookId: string; webhookToken: string }>) { return { + /** Writes a message through the webhook. */ write: (payload: MessageWebhookMethodWriteParams) => client.webhooks.writeMessage(webhookId, webhookToken, payload), + /** Edits a message sent through the webhook. */ edit: (payload: MessageWebhookMethodEditParams) => client.webhooks.editMessage(webhookId, webhookToken, payload), + /** Deletes a message sent through the webhook. */ delete: (messageId: string, reason?: string) => client.webhooks.deleteMessage(webhookId, webhookToken, messageId, reason), }; } + /** + * Static methods related to managing webhooks. + */ static methods({ client, webhookId, webhookToken }: MethodContext<{ webhookId: string; webhookToken?: string }>) { return { + /** Deletes the webhook. */ delete: (reason?: string) => client.webhooks.delete(webhookId, { reason, token: webhookToken }), + /** Edits the webhook. */ edit: (body: RESTPatchAPIWebhookWithTokenJSONBody | RESTPatchAPIWebhookJSONBody, reason?: string) => client.webhooks.edit(webhookId, body, { token: webhookToken, reason }), + /** Fetches the webhook data from the Discord API. */ fetch: () => client.webhooks.fetch(webhookId, webhookToken), }; } } +/** Type definition for parameters of editing a message through a webhook. */ export type MessageWebhookMethodEditParams = MessageWebhookPayload< MessageWebhookUpdateBodyRequest, { messageId: string; query?: RESTGetAPIWebhookWithTokenMessageQuery } >; +/** Type definition for parameters of writing a message through a webhook. */ export type MessageWebhookMethodWriteParams = MessageWebhookPayload< MessageWebhookCreateBodyRequest, { query?: RESTPostAPIWebhookWithTokenQuery }