Skip to content

Class: Patcher ​

Patcher is a utility class for modifying existing functions. An instance is available on BdApi. This is extremely useful for modifying the internals of Discord by adjusting return values of React renders, or arguments of internal functions.

Methods ​

after() ​

after<M, K>(caller, moduleToPatch, functionName, callback): (() => void) | null

This method patches onto another function, allowing your code to run afterwards. Using this, you are able to modify the return value after the original method is run.

Type Parameters ​

Type Parameter
M extends object
K extends string

Parameters ​

ParameterTypeDescription
callerstringName of the caller of the patch function
moduleToPatchMObject with the function to be patched. Can also be an object's prototype.
functionNameKName of the function to be patched
callbackAsAfterCallback<M, K>Function to run after the original method. The function is given the this context, the arguments of the original function, and the return value of the original function.

Returns ​

(() => void) | null

Function that cancels the original patch


before() ​

before<M, K>(caller, moduleToPatch, functionName, callback): (() => void) | null

This method patches onto another function, allowing your code to run beforehand. Using this, you are also able to modify the incoming arguments before the original method is run.

Type Parameters ​

Type Parameter
M extends object
K extends string

Parameters ​

ParameterTypeDescription
callerstringName of the caller of the patch function
moduleToPatchMObject with the function to be patched. Can also be an object's prototype.
functionNameKName of the function to be patched
callbackAsBeforeCallback<M, K>Function to run before the original method. The function is given the this context and the arguments of the original function.

Returns ​

(() => void) | null

Function that cancels the original patch


getPatchesByCaller() ​

getPatchesByCaller(caller): void | GenericChildPatch[]

Returns all patches by a particular caller. The patches all have an unpatch() method.

Parameters ​

ParameterTypeDescription
callerstringID of the original patches

Returns ​

void | GenericChildPatch[]

Array of all the patch objects


instead() ​

instead<M, K>(caller, moduleToPatch, functionName, callback): (() => void) | null

This method patches onto another function, allowing your code to run instead. Using this, you are able to replace the original completely. You can still call the original manually if needed.

Type Parameters ​

Type Parameter
M extends object
K extends string

Parameters ​

ParameterTypeDescription
callerstringName of the caller of the patch function
moduleToPatchMObject with the function to be patched. Can also be an object's prototype.
functionNameKName of the function to be patched
callbackAsInsteadCallback<M, K>Function to run before the original method. The function is given the this context, arguments of the original function, and also the original function.

Returns ​

(() => void) | null

Function that cancels the original patch


unpatchAll() ​

unpatchAll(caller): void

Automatically cancels all patches created with a specific ID.

Parameters ​

ParameterTypeDescription
callerstringID of the original patches

Returns ​

void