Skip to content

Commit

Permalink
document idk
Browse files Browse the repository at this point in the history
  • Loading branch information
catplvsplus committed Dec 9, 2023
1 parent b67a966 commit 44da16b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
61 changes: 61 additions & 0 deletions packages/utils/src/classes/MessageURLData.ts
Original file line number Diff line number Diff line change
@@ -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<InGuild extends boolean = boolean, Fetched extends boolean = boolean> 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<InGuild, string>; }

/**
* The message guild object
*/
get guild() { return this._guild as If<Fetched, If<InGuild, Guild, null>, undefined>; }

/**
* THe message channel object
*/
get channel() { return this._channel as If<Fetched, If<InGuild, GuildTextBasedChannel, PartialGroupDMChannel|DMChannel|PartialDMChannel>, undefined>; }

/**
* The message object
*/
get message() { return this._message as If<Fetched, Message<InGuild>, undefined>; }

constructor(data: ParseMessageURLData, public client?: Client) {
Expand All @@ -26,6 +53,10 @@ export class MessageURLData<InGuild extends boolean = boolean, Fetched extends b
this.messageId = data.messageId;
}

/**
* Fetch guild, channel, and message objects
* @param options Fetch options
*/
public async fetch(options?: BaseFetchOptions & { client?: Client }): Promise<MessageURLData<InGuild, true>> {
this.client = options?.client ?? this.client;
if (!this.client) throw new Error('Discord.js client is not defined');
Expand All @@ -39,22 +70,37 @@ export class MessageURLData<InGuild extends boolean = boolean, Fetched extends b
return this as MessageURLData<InGuild, true>;
}

/**
* Check if the objects are fetched
*/
public isFetched(): this is MessageURLData<InGuild, true> {
return this._guild !== undefined && this._channel !== undefined && this._message !== undefined;
}

/**
* Check if message URL is in DM
*/
public isDMBased(): this is MessageURLData<false, Fetched> {
return this.guildId === null;
}

/**
* Check if message URL is in guild
*/
public inGuild(): this is MessageURLData<true, Fetched> {
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,
Expand All @@ -63,11 +109,22 @@ export class MessageURLData<InGuild extends boolean = boolean, Fetched extends b
};
}

/**
* Fetch message URL data
* @param url The message URL
* @param client Discord.js client object
* @param fetchOptions Fetch options
*/
public static async fetch<InGuild extends boolean = boolean>(url: string|URL, client: Client, fetchOptions?: BaseFetchOptions): Promise<MessageURLData<InGuild, true>> {
const data = this.parse<InGuild>(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<InGuild extends boolean = boolean>(url: string|URL, client?: Client): MessageURLData<InGuild, false> {
const structure = url instanceof URL ? url : new URL(url);
const path = structure.pathname.split('/').filter(Boolean);
Expand All @@ -84,6 +141,10 @@ export class MessageURLData<InGuild extends boolean = boolean, Fetched extends b
}, client);
}

/**
* Check if string is valid message URL
* @param url url string
*/
public static isValidMessageURL(url: string|URL): boolean {
try {
const structure = url instanceof URL ? url : new URL(url);
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/helpers/mentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface FetchMentionOrIdOptions {
/**
* Get user id from mention or user object
* @param user User resolvable
* @returns
*/
export function getMentionId(user: UserResolvable|APIUser|APIMessage|`<@${string}>`): string|null {
const id = typeof user === 'string'
Expand Down

0 comments on commit 44da16b

Please sign in to comment.