From 44da16bbc6417d694108126a5c41faffe1444958 Mon Sep 17 00:00:00 2001 From: Cat++ <69035887+NotGhex@users.noreply.github.com> Date: Sat, 9 Dec 2023 09:38:05 +0800 Subject: [PATCH] document idk --- packages/utils/src/classes/MessageURLData.ts | 61 ++++++++++++++++++++ packages/utils/src/helpers/mentions.ts | 1 - 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/utils/src/classes/MessageURLData.ts b/packages/utils/src/classes/MessageURLData.ts index ff659d38..a7fe6f19 100644 --- a/packages/utils/src/classes/MessageURLData.ts +++ b/packages/utils/src/classes/MessageURLData.ts @@ -1,23 +1,50 @@ import { BaseFetchOptions, Client, DMChannel, Guild, GuildTextBasedChannel, If, Message, PartialDMChannel, PartialGroupDMChannel, TextBasedChannel } from 'discord.js'; +/** + * Message URL parse data + */ export interface ParseMessageURLData { guildId: string|null; channelId: string; messageId: string; } +/** + * Message URL object + */ export class MessageURLData implements ParseMessageURLData { private _guildId: string|null = null; private _guild?: Guild|null; private _channel?: TextBasedChannel|null; private _message?: Message|null; + /** + * The message channel id + */ readonly channelId: string; + /** + * The message id + */ readonly messageId: string; + /** + * The message guild id + */ get guildId() { return this._guildId as If; } + + /** + * The message guild object + */ get guild() { return this._guild as If, undefined>; } + + /** + * THe message channel object + */ get channel() { return this._channel as If, undefined>; } + + /** + * The message object + */ get message() { return this._message as If, undefined>; } constructor(data: ParseMessageURLData, public client?: Client) { @@ -26,6 +53,10 @@ export class MessageURLData> { this.client = options?.client ?? this.client; if (!this.client) throw new Error('Discord.js client is not defined'); @@ -39,22 +70,37 @@ export class MessageURLData; } + /** + * Check if the objects are fetched + */ public isFetched(): this is MessageURLData { return this._guild !== undefined && this._channel !== undefined && this._message !== undefined; } + /** + * Check if message URL is in DM + */ public isDMBased(): this is MessageURLData { return this.guildId === null; } + /** + * Check if message URL is in guild + */ public inGuild(): this is MessageURLData { return !this.isDMBased(); } + /** + * Get URL string + */ public toString(): string { return `https://discord.com/channels/${this.guildId ?? '@me'}/${this.channelId}/${this.messageId}`; } + /** + * Flatten object data + */ public toJSON(): ParseMessageURLData { return { guildId: this.guildId, @@ -63,11 +109,22 @@ export class MessageURLData(url: string|URL, client: Client, fetchOptions?: BaseFetchOptions): Promise> { const data = this.parse(url); return data.fetch({ ...fetchOptions, client }); } + /** + * Parse message URL data without fetching objects + * @param url The message URL + * @param client Discord.js client object + */ public static parse(url: string|URL, client?: Client): MessageURLData { const structure = url instanceof URL ? url : new URL(url); const path = structure.pathname.split('/').filter(Boolean); @@ -84,6 +141,10 @@ export class MessageURLData`): string|null { const id = typeof user === 'string'