Util
classe · Source
Contains various general-purpose utility methods.
Methods
flatten
Flatten an object. Any properties that are collections will get converted to an array of keys. Parameters
| Name | Type | Description |
|---|---|---|
obj | Object | The object to flatten. |
props? | Object<string, (boolean|string)> | Specific properties to include/exclude. |
Returns: ObjectSource
splitMessage
Splits a string into multiple chunks at a designated character that do not exceed a specific length. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to split |
options? | SplitOptions | Options controlling the behavior of the split |
Returns: Array<string>Source
escapeMarkdown
Escapes any Discord-flavour markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
options? | EscapeMarkdownOptions | Options for escaping the markdown Default: {}. |
Returns: stringSource
escapeCodeBlock
Escapes code block markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeInlineCode
Escapes inline code markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeItalic
Escapes italic markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeBold
Escapes bold markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeUnderline
Escapes underline markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeStrikethrough
Escapes strikethrough markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeSpoiler
Escapes spoiler markdown in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeEscape
Escapes escape characters in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeHeading
Escapes heading characters in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeBulletedList
Escapes bulleted list characters in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeNumberedList
Escapes numbered list characters in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
escapeMaskedLink
Escapes masked link characters in a string. Parameters
| Name | Type | Description |
|---|---|---|
text | string | Content to escape |
Returns: stringSource
parseEmoji
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 (
<:name:id>or<a:name:id>) Parameters
| Name | Type | Description |
|---|---|---|
text | string | Emoji string to parse |
Returns: APIEmoji — Object with animated, name, and id properties Source
resolvePartialEmoji
Resolves a partial emoji object from an EmojiIdentifierResolvable, without checking a Client. Parameters
| Name | Type | Description |
|---|---|---|
emoji | EmojiIdentifierResolvable | Emoji identifier to resolve |
Returns: RawEmojiSource
cloneObject
Shallow-copies an object with its class/prototype intact. Parameters
| Name | Type | Description |
|---|---|---|
obj | Object | Object to clone |
Returns: ObjectSource
mergeDefault
Sets default properties on an object that aren't already specified. Parameters
| Name | Type | Description |
|---|---|---|
def | Object | Default properties |
given | Object | Object to assign defaults to |
Returns: ObjectSource
makeError
Makes an Error from a plain info object. Parameters
| Name | Type | Description |
|---|---|---|
obj | MakeErrorOptions | Error info |
Returns: ErrorSource
makePlainError
Makes a plain error info object from an Error. Parameters
| Name | Type | Description |
|---|---|---|
err | Error | Error to get info from |
Returns: MakeErrorOptionsSource
moveElementInArray
Moves an element in an array in place. Parameters
| Name | Type | Description |
|---|---|---|
array | Array<*> | Array to modify |
element | * | Element to move |
newIndex | number | Index or offset to move the element to |
offset? | boolean | Move the element by an offset amount rather than to a set index Default: false. |
Returns: numberSource
verifyString
Verifies the provided data is a string, otherwise throws provided error. Parameters
| Name | Type | Description |
|---|---|---|
data | string | The string resolvable to resolve |
error? | function | The Error constructor to instantiate. Defaults to Error |
errorMessage? | string | The error message to throw with. Defaults to "Expected string, got <data> instead." |
allowEmpty? | boolean | Whether an empty string should be allowed Default: true. |
Returns: stringSource
resolveColor
Resolves a ColorResolvable into a color number. Parameters
| Name | Type | Description |
|---|---|---|
color | ColorResolvable | Color to resolve |
Returns: number — A color Source
discordSort
Sorts by Discord's position and id. Parameters
| Name | Type | Description |
|---|---|---|
collection | Collection | Collection of objects to sort |
Returns: CollectionSource
setPosition
Sets the position of a Channel or Role. Parameters
| Name | Type | Description |
|---|---|---|
item | Channel | Role | Object to set the position of |
position | number | New position for the object |
relative | boolean | Whether position is relative to its current position |
sorted | Collection<string, (Channel|Role)> | A collection of the objects sorted properly |
route | APIRouter | Route to call PATCH on |
reason? | string | Reason for the change |
Returns: Promise<(Array<Channel>\|Array<Role>)> — Updated item list, with id and position properties Source
basename
Alternative to Node's path.basename, removing query string after the extension if it exists. Parameters
| Name | Type | Description |
|---|---|---|
path | string | Path to get the basename of |
ext? | string | File extension to remove |
Returns: string — Basename of the path Source
removeMentions
Breaks user, role and everyone/here mentions by adding a zero width space after every @ character Parameters
| Name | Type | Description |
|---|---|---|
str | string | The string to sanitize |
Returns: stringSource
cleanContent
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
| Name | Type | Description |
|---|---|---|
str | string | The string to be converted |
channel | TextBasedChannels | The channel the string was sent in |
Returns: stringSource
cleanCodeBlockContent
The content to put in a code block with all code block fences replaced by the equivalent backticks. Parameters
| Name | Type | Description |
|---|---|---|
text | string | The string to be converted |
Returns: stringSource
archivedThreadSweepFilter
Creates a sweep filter that sweeps archived threads Parameters
| Name | Type | Description |
|---|---|---|
lifetime? | number | How long a thread has to be archived to be valid for sweeping Default: 14400. |
Returns: SweepFilterSource
resolveAutoArchiveMaxLimit
Resolves the maximum time a guild's thread channels should automatically archive in case of no recent activity. Parameters
| Name | Type | Description |
|---|---|---|
guild | Guild | The guild to resolve this limit from. |
Returns: numberSource
calculateUserDefaultAvatarIndex
Calculates the default avatar index for a given user id. Parameters
| Name | Type | Description |
|---|---|---|
userId | Snowflake | The user id to calculate the default avatar index for |
Returns: numberSource
lazy
Lazily evaluates a callback function (yea it's v14 :yay:) Parameters
| Name | Type | Description |
|---|---|---|
cb | function | The callback to lazily evaluate |
Returns: function
const User = lazy(() => require('../structures/User'));
const user = new (User())(client, data);verifyProxyAgent
Hacking check object instanceof Proxy-agent Parameters
| Name | Type | Description |
|---|---|---|
object | Object | any |
Returns: booleanSource
createTLSOptions
Creates the TLS options shared by REST and WebSocket connections. User options intentionally take precedence over the browser-like defaults. Parameters
| Name | Type | Description |
|---|---|---|
options? | Object | Agent | TLS or HTTPS agent options |
Returns: ObjectSource
resolveWebSocketTLS
Resolves the TLS and agent options accepted by ws. Parameters
| Name | Type | Description |
|---|---|---|
options? | WebsocketOptions | Client WebSocket options |
Returns: ObjectSource
getPayloadType
Get the payload type of the codec Parameters
| Name | Type | Description |
|---|---|---|
codecName | 'opus' | 'H264' | 'H265' | 'VP8' | 'VP9' | 'AV1' | Codec name |
Returns: numberSource