Class Injector

Inject code into Discord's webpack modules.

Example

import { Injector, webpack } from "replugged";

const injector = new Injector();

export async function start() {
const typingMod = (await webpack.waitForModule<{
startTyping: (channelId: string) => void;
}>(
webpack.filters.byProps("startTyping")
));

injector.after(typingMod, "startTyping", ([channel]) => {
console.log(`Typing in channel ID ${channel}`);
});
}

export function stop() {
injector.uninjectAll();
}

Hierarchy

  • Injector

Constructors

Properties

#slashCommandManager: CommandManager = ...
#uninjectors: Set<(() => void)> = ...

Type declaration

    • (): void
    • Returns void

utils: {
    addMenuItem: (<T>(navId, item, sectionId?, indexInSection?) => (() => void));
    addPopoverButton: ((item) => (() => void));
    registerSlashCommand: (<const T>(cmd) => (() => void));
} = ...

A few utils to add stuff in frequent modules.

Type declaration

  • addMenuItem: (<T>(navId, item, sectionId?, indexInSection?) => (() => void))
      • <T>(navId, item, sectionId?, indexInSection?): (() => void)
      • A utility function to add an item to any context menu. By default, items are placed in a group for custom items, though that can be customized with sectionId and indexInSection

        Type Parameters

        • T extends Record<string, unknown> = Record<string, unknown>

        Parameters

        • navId: ContextMenuTypes

          The id of the menu to add to

        • item: GetContextItem<T>

          The function that creates the item to add

        • sectionId: undefined | number = undefined

          — The number of the section to add to. Defaults to replugged's section

        • indexInSection: number = Infinity

          — The index in the section to add to. Defaults to the end position

        Returns (() => void)

        A callback to de-register the function

          • (): void
          • Add an item to any context menu

            Returns void

            A callback to de-register the function

        Example

        import { Injector, components, types } from "replugged";
        const { ContextMenu: { MenuItem } } = components;
        const { ContextMenuTypes } = types;

        const injector = new Injector();

        export function start() {
        injector.utils.addMenuItem(ContextMenuTypes.UserContext, // Right-clicking a user
        (data, menu) => {
        return <MenuItem
        id="my-item"
        label="An Item!"
        action={() => console.log(data)}
        />
        }
        )
        }

        export function stop() {
        injector.uninjectAll();
        }
  • addPopoverButton: ((item) => (() => void))
      • (item): (() => void)
      • A utility function to add a button to any message popover.

        Parameters

        • item: GetButtonItem

          The function that creates the button to add

        Returns (() => void)

        Uninject Function.

          • (): void
          • Adds a button to any MessagePopover

            Returns void

            A callback to remove the button from set.

        Example

        import { Injector, webpack } from "replugged";

        const injector = new Injector();

        export function start() {
        injector.utils.addPopoverButton((msg: Message, channel: Channel) => {
        return {
        label: "Click the button!",
        icon: myVeryCoolIcon(), // Cool icon
        onClick: () => {
        // do stuff here when someone left clicks the button
        },
        onContextMenu: () => {
        // do other stuff here when someone right clicks the button
        },
        };
        });
        }

        export function stop() {
        injector.uninjectAll();
        }
  • registerSlashCommand: (<const T>(cmd) => (() => void))
      • <const T>(cmd): (() => void)
      • A utility function to add a custom slash command.

        Type Parameters

        Parameters

        Returns (() => void)

        A callback to de-register the command

          • (): void
          • Code to register an slash command

            Returns void

            An Callback to unregister the slash command

        Example

        import { Injector, components, types } from "replugged";

        const injector = new Injector();

        export function start() {
        injector.utils.registerSlashCommand({
        name: "use",
        description: "a command meant to be used",
        usage: "/use",
        executor: (interaction) => {},
        })
        }

        export function stop() {
        injector.uninjectAll();
        }

Methods

  • Run code after a native module

    Type Parameters

    • T extends Record<U, AnyFunction>

    • U extends string

    • A extends unknown[] = Parameters<T[U]>

    • R = ReturnType<T[U]>

    Parameters

    • obj: T

      Module to inject to

    • funcName: U

      Function name on that module to inject

    • cb: AfterCallback<A, R>

      Code to run

    Returns (() => void)

    Uninject function

      • (): void
      • Run code after a native module

        Returns void

        Uninject function

  • Run code before a native module

    Type Parameters

    • T extends Record<U, AnyFunction>

    • U extends string

    • A extends unknown[] = Parameters<T[U]>

    Parameters

    • obj: T

      Module to inject to

    • funcName: U

      Function name on that module to inject

    • cb: BeforeCallback<A>

      Code to run

    Returns (() => void)

    Uninject function

      • (): void
      • Run code before a native module

        Returns void

        Uninject function

  • Run code instead of a native module

    Type Parameters

    • T extends Record<U, AnyFunction>

    • U extends string

    • A extends unknown[] = Parameters<T[U]>

    • R = ReturnType<T[U]>

    Parameters

    • obj: T

      Module to inject to

    • funcName: U

      Function name on that module to inject

    • cb: InsteadCallback<A, R>

      Code to run

    Returns (() => void)

    Uninject function

      • (): void
      • Run code instead of a native module

        Returns void

        Uninject function

Generated using TypeDoc