Class: Util

Util

Contains various general-purpose utility methods. These functions are also available on the base `Discord` object.

Constructor

new Util()

Source:

Methods

(private, static) basename(path, extopt) → {string}

Alternative to Node's `path.basename`, removing query string after the extension if it exists.
Parameters:
Name Type Attributes Description
path string Path to get the basename of
ext string <optional>
File extension to remove
Source:
Returns:
Basename of the path
Type
string

(private, static) binaryToID(num) → {Snowflake}

Transforms a snowflake from a bit string to a decimal string.
Parameters:
Name Type Description
num string Bit string to be transformed
Source:
Returns:
Type
Snowflake

(static) cleanCodeBlockContent(text) → {string}

The content to put in a codeblock with all codeblock fences replaced by the equivalent backticks.
Parameters:
Name Type Description
text string The string to be converted
Source:
Returns:
Type
string

(static) cleanContent(str, message) → {string}

The content to have all mentions replaced by the equivalent text.
Parameters:
Name Type Description
str string The string to be converted
message Message The message object to reference
Source:
Returns:
Type
string

(private, static) cloneObject(obj) → {Object}

Shallow-copies an object with its class/prototype intact.
Parameters:
Name Type Description
obj Object Object to clone
Source:
Returns:
Type
Object

(private, static) convertToBuffer(ab) → {Buffer}

Converts an ArrayBuffer or string to a Buffer.
Parameters:
Name Type Description
ab ArrayBuffer | string ArrayBuffer to convert
Source:
Returns:
Type
Buffer

(private, static) delayFor(ms) → {Promise.<void>}

Creates a Promise that resolves after a specified duration.
Parameters:
Name Type Description
ms number How long to wait before resolving (in milliseconds)
Source:
Returns:
Type
Promise.<void>

(static) discordSort(collection) → {Collection}

Sorts by Discord's position and ID.
Parameters:
Name Type Description
collection Collection Collection of objects to sort
Source:
Returns:
Type
Collection

(static) escapeBold(text) → {string}

Escapes bold markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) escapeCodeBlock(text) → {string}

Escapes code block markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) escapeInlineCode(text) → {string}

Escapes inline code markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) escapeItalic(text) → {string}

Escapes italic markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) escapeMarkdown(text, optionsopt) → {string}

Escapes any Discord-flavour markdown in a string.
Parameters:
Name Type Attributes Default Description
text string Content to escape
options Object <optional>
{} What types of markdown to escape
Properties
Name Type Attributes Default Description
codeBlock boolean <optional>
true Whether to escape code blocks or not
inlineCode boolean <optional>
true Whether to escape inline code or not
bold boolean <optional>
true Whether to escape bolds or not
italic boolean <optional>
true Whether to escape italics or not
underline boolean <optional>
true Whether to escape underlines or not
strikethrough boolean <optional>
true Whether to escape strikethroughs or not
spoiler boolean <optional>
true Whether to escape spoilers or not
codeBlockContent boolean <optional>
true Whether to escape text inside code blocks or not
inlineCodeContent boolean <optional>
true Whether to escape text inside inline code or not
Source:
Returns:
Type
string

(static) escapeSpoiler(text) → {string}

Escapes spoiler markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) escapeStrikethrough(text) → {string}

Escapes strikethrough markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) escapeUnderline(text) → {string}

Escapes underline markdown in a string.
Parameters:
Name Type Description
text string Content to escape
Source:
Returns:
Type
string

(static) fetchRecommendedShards(token, guildsPerShardopt) → {Promise.<number>}

Gets the recommended shard count from Discord.
Parameters:
Name Type Attributes Default Description
token string Discord auth token
guildsPerShard number <optional>
1000 Number of guilds per shard
Source:
Returns:
The recommended number of shards
Type
Promise.<number>

(static) flatten(obj, …propsopt) → {Object}

Flatten an object. Any properties that are collections will get converted to an array of keys.
Parameters:
Name Type Attributes Description
obj Object The object to flatten.
props Object.<string, (boolean|string)> <optional>
<repeatable>
Specific properties to include/exclude.
Source:
Returns:
Type
Object

(private, static) idToBinary(num) → {string}

Transforms a snowflake from a decimal string to a bit string.
Parameters:
Name Type Description
num Snowflake Snowflake to be transformed
Source:
Returns:
Type
string

(private, static) makeError(obj) → {Error}

Makes an Error from a plain info object.
Parameters:
Name Type Description
obj Object Error info
Properties
Name Type Description
name string Error type
message string Message for the error
stack string Stack for the error
Source:
Returns:
Type
Error

(private, static) makePlainError(err) → {Object}

Makes a plain error info object from an Error.
Parameters:
Name Type Description
err Error Error to get info from
Source:
Returns:
Type
Object

(private, static) mergeDefault(def, given) → {Object}

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
Source:
Returns:
Type
Object

(private, static) moveElementInArray(array, element, newIndex, offsetopt) → {number}

Moves an element in an array *in place*.
Parameters:
Name Type Attributes Default Description
array Array.<*> Array to modify
element * Element to move
newIndex number Index or offset to move the element to
offset boolean <optional>
false Move the element by an offset amount rather than to a set index
Source:
Returns:
Type
number

(private, static) parseEmoji(text) → {Object}

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 ``)
Parameters:
Name Type Description
text string Emoji string to parse
Source:
Returns:
Object with `animated`, `name`, and `id` properties
Type
Object

(static) removeMentions(str) → {string}

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
Source:
Returns:
Type
string

(static) resolveColor(color) → {number}

Resolves a ColorResolvable into a color number.
Parameters:
Name Type Description
color ColorResolvable Color to resolve
Source:
Returns:
A color
Type
number

(static) resolveString(data) → {string}

Resolves a StringResolvable to a string.
Parameters:
Name Type Description
data StringResolvable The string resolvable to resolve
Source:
Returns:
Type
string

(private, static) setPosition(item, position, relative, sorted, route, reasonopt) → {Promise.<Array.<Object>>}

Sets the position of a Channel or Role.
Parameters:
Name Type Attributes 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 <optional>
Reason for the change
Source:
Returns:
Updated item list, with `id` and `position` properties
Type
Promise.<Array.<Object>>

(static) splitMessage(text, optionsopt) → {Array.<string>}

Splits a string into multiple chunks at a designated character that do not exceed a specific length.
Parameters:
Name Type Attributes Description
text StringResolvable Content to split
options SplitOptions <optional>
Options controlling the behavior of the split
Source:
Returns:
Type
Array.<string>

(private, static) str2ab(str) → {ArrayBuffer}

Converts a string to an ArrayBuffer.
Parameters:
Name Type Description
str string String to convert
Source:
Returns:
Type
ArrayBuffer