Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/betterdiscord/betterdiscord/llms.txt

Use this file to discover all available pages before exploring further.

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

Methods

before

Patches onto another function, allowing your code to run beforehand. You can modify the incoming arguments before the original method runs.
const unpatch = BdApi.Patcher.before("MyPlugin", moduleToPatch, "functionName", (thisObject, args) => {
    // Modify args before the original function runs
    args[0] = "modified value";
});
caller
string
required
Name of the caller of the patch function. Only required when using the global API.
moduleToPatch
object
required
Object with the function to be patched. Can also be an object’s prototype.
functionName
string
required
Name of the function to be patched.
callback
function
required
Function to run before the original method. The function is given the this context and the arguments of the original function.Parameters:
  • thisObject - The this context of the original function
  • args - Array of arguments passed to the original function (modifiable)
Returns: function - Function that cancels the original patch when called

instead

Patches onto another function, allowing your code to run instead of the original. You can replace the original completely while still being able to call it manually if needed.
const unpatch = BdApi.Patcher.instead("MyPlugin", moduleToPatch, "functionName", (thisObject, args, original) => {
    // Optionally call the original function
    const result = original.apply(thisObject, args);
    return "completely new return value";
});
caller
string
required
Name of the caller of the patch function. Only required when using the global API.
moduleToPatch
object
required
Object with the function to be patched. Can also be an object’s prototype.
functionName
string
required
Name of the function to be patched.
callback
function
required
Function to run instead of the original method. The function is given the this context, arguments of the original function, and the original function itself.Parameters:
  • thisObject - The this context of the original function
  • args - Array of arguments passed to the original function
  • original - The original function that was patched
Returns: function - Function that cancels the original patch when called

after

Patches onto another function, allowing your code to run afterwards. You can modify the return value after the original method runs.
const unpatch = BdApi.Patcher.after("MyPlugin", moduleToPatch, "functionName", (thisObject, args, returnValue) => {
    // Modify and return the new return value
    returnValue.modified = true;
    return returnValue;
});
caller
string
required
Name of the caller of the patch function. Only required when using the global API.
moduleToPatch
object
required
Object with the function to be patched. Can also be an object’s prototype.
functionName
string
required
Name of the function to be patched.
callback
function
required
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.Parameters:
  • thisObject - The this context of the original function
  • args - Array of arguments passed to the original function
  • returnValue - The return value from the original function (modifiable)
Returns: function - Function that cancels the original patch when called

getPatchesByCaller

Returns all patches by a particular caller. The patches all have an unpatch() method.
const patches = BdApi.Patcher.getPatchesByCaller("MyPlugin");
patches.forEach(patch => patch.unpatch());
caller
string
required
ID of the original patches. When using scoped API, this parameter is automatically filled.
Returns: Array<function> - Array of all the patch objects

unpatchAll

Automatically cancels all patches created with a specific ID.
BdApi.Patcher.unpatchAll("MyPlugin");
caller
string
required
ID of the original patches. When using scoped API, this parameter is automatically filled.

Examples

Patching a message component

const MessageContent = BdApi.Webpack.getByKeys("type", "render");

const unpatch = BdApi.Patcher.after("MyPlugin", MessageContent, "type", (thisObject, args, returnValue) => {
    // Modify the rendered message
    if (returnValue && returnValue.props) {
        returnValue.props.className += " my-custom-class";
    }
    return returnValue;
});

// Later, clean up
unpatch();

Modifying function arguments

const SomeModule = BdApi.Webpack.getByKeys("sendMessage");

BdApi.Patcher.before("MyPlugin", SomeModule, "sendMessage", (thisObject, args) => {
    // args[0] might be the message object
    if (args[0] && args[0].content) {
        args[0].content = args[0].content.toUpperCase();
    }
});

Replacing a function entirely

const SomeModule = BdApi.Webpack.getByKeys("someFunction");

BdApi.Patcher.instead("MyPlugin", SomeModule, "someFunction", (thisObject, args, original) => {
    if (someCondition) {
        return "custom behavior";
    }
    // Fall back to original
    return original.apply(thisObject, args);
});