Skip to content

Class: Utils

Defined in: src/betterdiscord/api/utils.ts:13

Utils is a utility containing commonly reused functions. Instance is accessible through the BdApi.

Properties

className()

ts
static className: (...inputs: ClassValue[]) => string = clsx;

Defined in: src/betterdiscord/api/utils.ts:81

Builds a classname string from any number of arguments. This includes arrays and objects. When given an array all values from the array are added to the list. When given an object they keys are added as the classnames if the value is truthy. Copyright (c) Luke Edwards [email protected] (lukeed.com)

Parameters

ParameterType
...inputsClassValue[]

Returns

string

Joined classname


debounce()

ts
static debounce: <T>(executor: T, delay: number) => (...args: Parameters<T>) => void;

Defined in: src/betterdiscord/api/utils.ts:56

Returns a function, that, as long as it continues to be invoked, will not be triggered. The function will be called after it stops being called for delay milliseconds. It is called at the end of the sequence (trailing edge).

Adapted from the version by David Walsh (https://davidwalsh.name/javascript-debounce-function)

Returns a function, that, as long as it continues to be invoked, will not be triggered. The function will be called after it stops being called for N milliseconds.

Type Parameters

Type Parameter
T extends (...args: any[]) => any

Parameters

ParameterTypeDescription
executorTfunction to debounce
delaynumbertime to delay in milliseconds

Returns

ts
(...args: Parameters<T>): void;
Parameters
ParameterType
...argsParameters<T>
Returns

void

Param

The function to be debounced

Param

Number of ms to delay calls

Returns

A debounced version of the function


extend()

ts
static extend: (target: object, ...extenders: object[]) => object;

Defined in: src/betterdiscord/api/utils.ts:43

Deep extends an object with a set of other objects. Objects later in the list of extenders have priority, that is to say if one sets a key to be a primitive, it will be overwritten with the next one with the same key. If it is an object, and the keys match, the object is extended. This happens recursively.

Deep extends an object with a set of other objects. Objects later in the list of extenders have priority, that is to say if one sets a key to be a primitive, it will be overwritten with the next one with the same key. If it is an object, and the keys match, the object is extended. This happens recursively.

Parameters

ParameterTypeDescription
targetobjecttarget object for extension
...extendersobject[]series of objects to use for extension

Returns

object

Param

Object to be extended

Param

Objects to extend with

Returns

A reference to extendee


findInTree()

ts
static findInTree: (tree: 
  | Record<string | number, unknown>
  | null, searchFilter: string | TreeFilter, options: {
  ignore?: string[];
  walkable?: string[] | null;
}) => any;

Defined in: src/betterdiscord/api/utils.ts:23

Finds a value, subobject, or array from a tree that matches a specific filter. This is a DFS.

Finds a value, subobject, or array from a tree that matches a specific filter.

Parameters

ParameterTypeDescription
tree| Record<string | number, unknown> | nullTree that should be walked
searchFilterstring | TreeFilterFilter to check against each object and subobject
options{ ignore?: string[]; walkable?: string[] | null; }Additional options to customize the search
options.ignore?string[]Array of strings to use as keys to exclude from the search, most helpful when walkable = null.
options.walkable?string[] | nullArray of strings to use as keys that are allowed to be walked on. Null value indicates all keys are walkable

Returns

any

Param

Tree that should be walked

Param

Filter to check against each object and subobject

Param

Additional options to customize the search

Param

Array of strings to use as keys that are allowed to be walked on. null indicates all keys are walkable.

Param

Array of strings to use as keys to exclude from the search. Most helpful when walkable = null.


forceLoad()

ts
static forceLoad: (id: string | number) => Promise<any[]>;

Defined in: src/betterdiscord/api/utils.ts:31

Loads the module ids within a chunk

Parameters

ParameterTypeDescription
idstring | numbermodule with the chunk id.

Returns

Promise<any[]>

resolved chunk module


semverCompare()

ts
static semverCompare: (currentVersion: string, remoteVersion: string) => -1 | 0 | 1 = comparator;

Defined in: src/betterdiscord/api/utils.ts:99

This works on semantic versioning e.g. "1.0.0".

This works on semantic versioning e.g. "1.0.0".

Parameters

ParameterTypeDescription
currentVersionstring
remoteVersionstring

Returns

-1 | 0 | 1

0 indicates equal, -1 indicates left hand greater, 1 indicates right hand greater

Param

Param

Returns

0 indicates equal, -1 indicates left hand greater, 1 indicates right hand greater


Store

ts
static Store: typeof Store;

Defined in: src/betterdiscord/api/utils.ts:101

Methods

escapeHTML()

ts
static escapeHTML(html: string): string;

Defined in: src/betterdiscord/api/utils.ts:64

Takes a string of HTML and escapes it using the browser's own escaping mechanism.

Parameters

ParameterTypeDescription
htmlstringHTML to be escaped

Returns

string

Escaped HTML string


getNestedValue()

ts
static getNestedValue<T, R>(object: T, path: string): R;

Defined in: src/betterdiscord/api/utils.ts:88

Gets a nested value (if it exists) of an object safely. keyPath should be something like key.key2.key3. Numbers can be used for arrays as well like key.key2.array.0.id.

Type Parameters

Type ParameterDefault type
T extends Record<string | number | symbol, unknown>-
Rany

Parameters

ParameterType
objectT
pathstring

Returns

R