Skip to content

Util

classe · Source

Contains various general-purpose utility methods.

Methods

flatten

static

flatten(obj: Object, props?: Object<string, (boolean|string)>): Object

Flatten an object. Any properties that are collections will get converted to an array of keys. Parameters

NameTypeDescription
objObjectThe object to flatten.
props?Object<string, (boolean|string)>Specific properties to include/exclude.

Returns: ObjectSource

splitMessage

static · deprecated

splitMessage(text: string, options?: SplitOptions): Array<string>

Splits a string into multiple chunks at a designated character that do not exceed a specific length. Parameters

NameTypeDescription
textstringContent to split
options?SplitOptionsOptions controlling the behavior of the split

Returns: Array<string>Source

escapeMarkdown

static

escapeMarkdown(text: string, options?: EscapeMarkdownOptions): string

Escapes any Discord-flavour markdown in a string. Parameters

NameTypeDescription
textstringContent to escape
options?EscapeMarkdownOptionsOptions for escaping the markdown Default: {}.

Returns: stringSource

escapeCodeBlock

static

escapeCodeBlock(text: string): string

Escapes code block markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeInlineCode

static

escapeInlineCode(text: string): string

Escapes inline code markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeItalic

static

escapeItalic(text: string): string

Escapes italic markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeBold

static

escapeBold(text: string): string

Escapes bold markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeUnderline

static

escapeUnderline(text: string): string

Escapes underline markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeStrikethrough

static

escapeStrikethrough(text: string): string

Escapes strikethrough markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeSpoiler

static

escapeSpoiler(text: string): string

Escapes spoiler markdown in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeEscape

static

escapeEscape(text: string): string

Escapes escape characters in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeHeading

static

escapeHeading(text: string): string

Escapes heading characters in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeBulletedList

static

escapeBulletedList(text: string): string

Escapes bulleted list characters in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

escapeNumberedList

static

escapeNumberedList(text: string): string

Escapes numbered list characters in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

static

escapeMaskedLink(text: string): string

Escapes masked link characters in a string. Parameters

NameTypeDescription
textstringContent to escape

Returns: stringSource

parseEmoji

static · private

parseEmoji(text: string): APIEmoji

Parses emoji info out of a string. The string must be one of:

  • A UTF-8 emoji (no id)
  • A URL-encoded UTF-8 emoji (no id)
  • A Discord custom emoji (&lt;:name:id&gt; or &lt;a:name:id&gt;) Parameters
NameTypeDescription
textstringEmoji string to parse

Returns: APIEmoji — Object with animated, name, and id properties Source

resolvePartialEmoji

static · private

resolvePartialEmoji(emoji: EmojiIdentifierResolvable): RawEmoji

Resolves a partial emoji object from an EmojiIdentifierResolvable, without checking a Client. Parameters

NameTypeDescription
emojiEmojiIdentifierResolvableEmoji identifier to resolve

Returns: RawEmojiSource

cloneObject

static · private

cloneObject(obj: Object): Object

Shallow-copies an object with its class/prototype intact. Parameters

NameTypeDescription
objObjectObject to clone

Returns: ObjectSource

mergeDefault

static · private

mergeDefault(def: Object, given: Object): Object

Sets default properties on an object that aren't already specified. Parameters

NameTypeDescription
defObjectDefault properties
givenObjectObject to assign defaults to

Returns: ObjectSource

makeError

static · private

makeError(obj: MakeErrorOptions): Error

Makes an Error from a plain info object. Parameters

NameTypeDescription
objMakeErrorOptionsError info

Returns: ErrorSource

makePlainError

static · private

makePlainError(err: Error): MakeErrorOptions

Makes a plain error info object from an Error. Parameters

NameTypeDescription
errErrorError to get info from

Returns: MakeErrorOptionsSource

moveElementInArray

static · private

moveElementInArray(array: Array<*>, element: *, newIndex: number, offset?: boolean): number

Moves an element in an array in place. Parameters

NameTypeDescription
arrayArray<*>Array to modify
element*Element to move
newIndexnumberIndex or offset to move the element to
offset?booleanMove the element by an offset amount rather than to a set index Default: false.

Returns: numberSource

verifyString

static

verifyString(data: string, error?: function, errorMessage?: string, allowEmpty?: boolean): string

Verifies the provided data is a string, otherwise throws provided error. Parameters

NameTypeDescription
datastringThe string resolvable to resolve
error?functionThe Error constructor to instantiate. Defaults to Error
errorMessage?stringThe error message to throw with. Defaults to "Expected string, got <data> instead."
allowEmpty?booleanWhether an empty string should be allowed Default: true.

Returns: stringSource

resolveColor

static

resolveColor(color: ColorResolvable): number

Resolves a ColorResolvable into a color number. Parameters

NameTypeDescription
colorColorResolvableColor to resolve

Returns: number — A color Source

discordSort

static

discordSort(collection: Collection): Collection

Sorts by Discord's position and id. Parameters

NameTypeDescription
collectionCollectionCollection of objects to sort

Returns: CollectionSource

setPosition

static · async · private

async setPosition(item: Channel | Role, position: number, relative: boolean, sorted: Collection<string, (Channel|Role)>, route: APIRouter, reason?: string): Promise<(Array<Channel>|Array<Role>)>

Sets the position of a Channel or Role. Parameters

NameTypeDescription
itemChannel | RoleObject to set the position of
positionnumberNew position for the object
relativebooleanWhether position is relative to its current position
sortedCollection<string, (Channel|Role)>A collection of the objects sorted properly
routeAPIRouterRoute to call PATCH on
reason?stringReason for the change

Returns: Promise<(Array<Channel>\|Array<Role>)> — Updated item list, with id and position properties Source

basename

static · private

basename(path: string, ext?: string): string

Alternative to Node's path.basename, removing query string after the extension if it exists. Parameters

NameTypeDescription
pathstringPath to get the basename of
ext?stringFile extension to remove

Returns: string — Basename of the path Source

removeMentions

static · deprecated

removeMentions(str: string): string

Breaks user, role and everyone/here mentions by adding a zero width space after every @ character Parameters

NameTypeDescription
strstringThe string to sanitize

Returns: stringSource

cleanContent

static

cleanContent(str: string, channel: TextBasedChannels): string

The content to have all mentions replaced by the equivalent text. When Util.removeMentions is removed, this method will no longer sanitize mentions. Use BaseMessageOptions#allowedMentions instead to prevent mentions when sending a message. Parameters

NameTypeDescription
strstringThe string to be converted
channelTextBasedChannelsThe channel the string was sent in

Returns: stringSource

cleanCodeBlockContent

static

cleanCodeBlockContent(text: string): string

The content to put in a code block with all code block fences replaced by the equivalent backticks. Parameters

NameTypeDescription
textstringThe string to be converted

Returns: stringSource

archivedThreadSweepFilter

static · deprecated

archivedThreadSweepFilter(lifetime?: number): SweepFilter

Creates a sweep filter that sweeps archived threads Parameters

NameTypeDescription
lifetime?numberHow long a thread has to be archived to be valid for sweeping Default: 14400.

Returns: SweepFilterSource

resolveAutoArchiveMaxLimit

static · deprecated

resolveAutoArchiveMaxLimit(guild: Guild): number

Resolves the maximum time a guild's thread channels should automatically archive in case of no recent activity. Parameters

NameTypeDescription
guildGuildThe guild to resolve this limit from.

Returns: numberSource

calculateUserDefaultAvatarIndex

static

calculateUserDefaultAvatarIndex(userId: Snowflake): number

Calculates the default avatar index for a given user id. Parameters

NameTypeDescription
userIdSnowflakeThe user id to calculate the default avatar index for

Returns: numberSource

lazy

static

lazy(cb: function): function

Lazily evaluates a callback function (yea it's v14 :yay:) Parameters

NameTypeDescription
cbfunctionThe callback to lazily evaluate

Returns: function

js
const User = lazy(() => require('../structures/User'));
const user = new (User())(client, data);

Source

verifyProxyAgent

static

verifyProxyAgent(object: Object): boolean

Hacking check object instanceof Proxy-agent Parameters

NameTypeDescription
objectObjectany

Returns: booleanSource

createTLSOptions

static · private

createTLSOptions(options?: Object | Agent): Object

Creates the TLS options shared by REST and WebSocket connections. User options intentionally take precedence over the browser-like defaults. Parameters

NameTypeDescription
options?Object | AgentTLS or HTTPS agent options

Returns: ObjectSource

resolveWebSocketTLS

static · private

resolveWebSocketTLS(options?: WebsocketOptions): Object

Resolves the TLS and agent options accepted by ws. Parameters

NameTypeDescription
options?WebsocketOptionsClient WebSocket options

Returns: ObjectSource

getPayloadType

static

getPayloadType(codecName: 'opus' | 'H264' | 'H265' | 'VP8' | 'VP9' | 'AV1'): number

Get the payload type of the codec Parameters

NameTypeDescription
codecName'opus' | 'H264' | 'H265' | 'VP8' | 'VP9' | 'AV1'Codec name

Returns: numberSource

Unofficial software. Not affiliated with or supported by Discord.