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>
This commit is contained in:
Xeno 2024-06-12 17:19:42 +05:30 committed by GitHub
parent 88c800d9fd
commit 20bb778722
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 53 additions and 12 deletions

View File

@ -200,4 +200,41 @@ export class Formatter {
static timestamp(timestamp: Date, style: TimestampStyle = TimestampStyle.RelativeTime): Timestamp {
return `<t:${Math.floor(timestamp.getTime() / 1000)}:${style}>`;
}
/**
* 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}>`;
}
}

View File

@ -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) => {

View File

@ -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 }>) {

View File

@ -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<Omit<APIEmoji, 'id'>> {}
@ -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() {

View File

@ -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) {

View File

@ -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),

View File

@ -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<APIUser> {
}
toString() {
return `<@${this.id}>`;
return Formatter.userMention(this.id);
}
}

View File

@ -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<T extends ChannelType> extends DiscordBase<APIChannelBase<ChannelType>> {
declare type: T;
@ -77,7 +77,7 @@ export class BaseChannel<T extends ChannelType> extends DiscordBase<APIChannelBa
}
toString() {
return `<#${this.id}>`;
return Formatter.channelMention(this.id);
}
isStage(): this is StageChannel {