From 20bb778722a8512378e28c5778da8416d6aa9922 Mon Sep 17 00:00:00 2001 From: Xeno Date: Wed, 12 Jun 2024 17:19:42 +0530 Subject: [PATCH] chore: improve toString output with newly added formatters (#209) * chore: improve toString output with newly added formatters * fix(missingParameter): added emoji name as a parameter --------- Co-authored-by: NotAditya69 <90441096+NotAditya69@users.noreply.github.com> --- src/common/it/formatter.ts | 37 +++++++++++++++++++++++++++++++++++ src/events/hooks/guild.ts | 2 +- src/structures/GuildBan.ts | 4 ++-- src/structures/GuildEmoji.ts | 4 ++-- src/structures/GuildMember.ts | 4 ++-- src/structures/GuildRole.ts | 6 +++++- src/structures/User.ts | 4 ++-- src/structures/channels.ts | 4 ++-- 8 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/common/it/formatter.ts b/src/common/it/formatter.ts index d24d1d9..598f2a2 100644 --- a/src/common/it/formatter.ts +++ b/src/common/it/formatter.ts @@ -200,4 +200,41 @@ export class Formatter { static timestamp(timestamp: Date, style: TimestampStyle = TimestampStyle.RelativeTime): Timestamp { return ``; } + + /** + * Formats a user mention. + * @param userId The ID of the user to mention. + * @returns The formatted user mention. + */ + static userMention(userId: string): `<@${string}>` { + return `<@${userId}>`; + } + + /** + * Formats a role mention. + * @param roleId The ID of the role to mention. + * @returns The formatted role mention. + */ + static roleMention(roleId: string): `<@&${string}>` { + return `<@&${roleId}>`; + } + + /** + * Formats a channel mention. + * @param channelId The ID of the channel to mention. + * @returns The formatted channel mention. + */ + static channelMention(channelId: string): `<#${string}>` { + return `<#${channelId}>`; + } + + /** + * Formats an emoji. + * @param emojiId The ID of the emoji. + * @param animated Whether the emoji is animated. Defaults to false. + * @returns The formatted emoji. + */ + static emojiMention(emojiId: string, name: string | null, animated = false): string { + return `<${animated ? 'a' : ''}:${name ?? '_'}:${emojiId}>`; + } } diff --git a/src/events/hooks/guild.ts b/src/events/hooks/guild.ts index a8c5ab2..0538c37 100644 --- a/src/events/hooks/guild.ts +++ b/src/events/hooks/guild.ts @@ -57,7 +57,7 @@ export const GUILD_INTEGRATIONS_UPDATE = (_self: UsingClient, data: GatewayGuild }; export const GUILD_MEMBER_ADD = (self: UsingClient, data: GatewayGuildMemberAddDispatchData) => { - return new GuildMember(self, data, data.user, data.guild_id); + return new GuildMember(self, data, data.user!, data.guild_id); }; export const GUILD_MEMBER_REMOVE = (self: UsingClient, data: GatewayGuildMemberRemoveDispatchData) => { diff --git a/src/structures/GuildBan.ts b/src/structures/GuildBan.ts index 993fe48..5f7690c 100644 --- a/src/structures/GuildBan.ts +++ b/src/structures/GuildBan.ts @@ -1,6 +1,6 @@ import type { APIBan, RESTGetAPIGuildBansQuery } from 'discord-api-types/v10'; import type { UsingClient } from '../commands'; -import type { MethodContext, ObjectToLower } from '../common'; +import { Formatter, type MethodContext, type ObjectToLower } from '../common'; import { DiscordBase } from './extra/DiscordBase'; import type { BanShorter } from '../common/shorters/bans'; @@ -32,7 +32,7 @@ export class GuildBan extends DiscordBase { } toString() { - return `<@${this.id}>`; + return Formatter.userMention(this.id); } static methods({ client, guildId }: MethodContext<{ guildId: string }>) { diff --git a/src/structures/GuildEmoji.ts b/src/structures/GuildEmoji.ts index d2dd2e0..1f9f36c 100644 --- a/src/structures/GuildEmoji.ts +++ b/src/structures/GuildEmoji.ts @@ -1,7 +1,7 @@ import type { APIEmoji, RESTPatchAPIChannelJSONBody, RESTPatchAPIGuildEmojiJSONBody } from 'discord-api-types/v10'; import type { BaseCDNUrlOptions } from '../api'; import type { UsingClient } from '../commands'; -import type { EmojiShorter, MethodContext, ObjectToLower } from '../common'; +import { Formatter, type EmojiShorter, type MethodContext, type ObjectToLower } from '../common'; import { DiscordBase } from './extra/DiscordBase'; export interface GuildEmoji extends DiscordBase, ObjectToLower> {} @@ -37,7 +37,7 @@ export class GuildEmoji extends DiscordBase { } toString() { - return `<${this.animated ? 'a' : ''}:${this.name}:${this.id}>`; + return Formatter.emojiMention(this.id, this.name, this.animated); } toJSON() { diff --git a/src/structures/GuildMember.ts b/src/structures/GuildMember.ts index 4e83103..7a247b7 100644 --- a/src/structures/GuildMember.ts +++ b/src/structures/GuildMember.ts @@ -20,7 +20,7 @@ import type { RESTPutAPIGuildMemberJSONBody, } from 'discord-api-types/v10'; import type { UsingClient } from '../commands'; -import type { MessageCreateBodyRequest, ObjectToLower, ToClass } from '../common'; +import { Formatter, type MessageCreateBodyRequest, type ObjectToLower, type ToClass } from '../common'; import type { ImageOptions, MethodContext } from '../common/types/options'; import type { GuildMemberResolvable } from '../common/types/resolvables'; import { User } from './User'; @@ -73,7 +73,7 @@ export class BaseGuildMember extends DiscordBase { } toString() { - return `<@${this.id}>`; + return Formatter.userMention(this.id); } private patch(data: GuildMemberData) { diff --git a/src/structures/GuildRole.ts b/src/structures/GuildRole.ts index 4a1b27a..9d341ee 100644 --- a/src/structures/GuildRole.ts +++ b/src/structures/GuildRole.ts @@ -5,7 +5,7 @@ import type { RESTPostAPIGuildRoleJSONBody, } from 'discord-api-types/v10'; import type { UsingClient } from '../commands'; -import type { MethodContext, ObjectToLower } from '../common'; +import { Formatter, type MethodContext, type ObjectToLower } from '../common'; import { DiscordBase } from './extra/DiscordBase'; import { PermissionsBitField } from './extra/Permissions'; @@ -35,6 +35,10 @@ export class GuildRole extends DiscordBase { return this.client.roles.delete(this.guildId, this.id, reason); } + toString() { + return Formatter.roleMention(this.id); + } + static methods(ctx: MethodContext<{ guildId: string }>) { return { create: (body: RESTPostAPIGuildRoleJSONBody) => ctx.client.roles.create(ctx.guildId, body), diff --git a/src/structures/User.ts b/src/structures/User.ts index a0067f8..2eceeb3 100644 --- a/src/structures/User.ts +++ b/src/structures/User.ts @@ -1,6 +1,6 @@ import type { APIUser } from 'discord-api-types/v10'; import { calculateUserDefaultAvatarIndex } from '../api'; -import type { MessageCreateBodyRequest, ObjectToLower } from '../common'; +import { Formatter, type MessageCreateBodyRequest, type ObjectToLower } from '../common'; import type { ImageOptions } from '../common/types/options'; import { DiscordBase } from './extra/DiscordBase'; @@ -60,6 +60,6 @@ export class User extends DiscordBase { } toString() { - return `<@${this.id}>`; + return Formatter.userMention(this.id); } } diff --git a/src/structures/channels.ts b/src/structures/channels.ts index 40a20ab..aab1837 100644 --- a/src/structures/channels.ts +++ b/src/structures/channels.ts @@ -41,7 +41,7 @@ import type { GuildMember } from './GuildMember'; import type { GuildRole } from './GuildRole'; import { DiscordBase } from './extra/DiscordBase'; import { channelLink } from './extra/functions'; -import { Collection, type RawFile } from '..'; +import { Collection, Formatter, type RawFile } from '..'; export class BaseChannel extends DiscordBase> { declare type: T; @@ -77,7 +77,7 @@ export class BaseChannel extends DiscordBase`; + return Formatter.channelMention(this.id); } isStage(): this is StageChannel {