fix: exports & webhooks tsdocs

This commit is contained in:
MARCROCK22 2024-03-28 20:31:52 -04:00
parent 2bf75715c8
commit 1355ebf50a
2 changed files with 59 additions and 18 deletions

View File

@ -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"
}
]
}

View File

@ -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<Omit<APIWebhook, 'user' | 'source_guild'>> {}
/**
* 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<AnonymousGuild>;
/** Methods related to interacting with messages through the webhook. */
messages!: ReturnType<typeof Webhook.messages>;
/**
* 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 }