aboutsummaryrefslogtreecommitdiff
path: root/docs/2_PLUGINS.md
blob: 6ac4b24662e3a261e9b955f2b47472e03c6985ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Plugins Guide

Welcome to Megu's Plugin Guide! In this file, you will learn about how to write your own plugin!

You don't need to run `pnpm build` every time you make a change. Instead, use `pnpm watch` - this will auto-compile Vencord whenever you make a change. If using code patches (recommended), you will need to CTRL+R to load the changes.

## Plugin Entrypoint

> If it doesn't already exist, create a folder called `userplugins` in the `src` directory of this repo.

1. Create a folder in `src/userplugins/` with the name of your plugin. For example, `src/userplugins/epicPlugin/` - All of your plugin files will go here.

2. Create a file in that folder called `index.ts`

3. In `index.ts`, copy-paste the following template code:

```ts
import definePlugin from "../../utils/types";

export default definePlugin({
    name: "Epic Plugin",
    description: "This plugin is absolutely epic",
    authors: [
        {
            id: 12345n,
            name: "Your Name",
        },
    ],
    patches: [],
    // Delete these two below if you are only using code patches
    start() {},
    stop() {},
});
```

Change the name, description, and authors to your own information.

Replace `12345n` with your user ID ending in `n` (e.g., `545581357812678656n`). If you don't want to share your Discord account, use `0n` instead!

## How Plugins Work In Vencord

Vencord uses a different way of making mods than you're used to.
Instead of monkeypatching webpack, we directly modify the code before Discord loads it.

This is _significantly_ more efficient than monkeypatching webpack, and is surprisingly easy, but it may be confusing at first.

## Making your patch

For an in-depth guide into patching code, see [CONTRIBUTING.md](../CONTRIBUTING.md)

in the `index.ts` file we made earlier, you'll see a `patches` array.

> You'll see examples of how patches are used in all the existing plugins, and it'll be easier to understand by looking at those examples, so do that first, and then return here!

> For a good example of a plugin using code patches AND runtime patching, check `src/plugins/unindent.ts`, which uses code patches to run custom runtime code.

One of the patches in the `isStaff` plugin, looks like this:

```ts
{
    match: /(\w+)\.isStaff=function\(\){return\s*!1};/,
    replace: "$1.isStaff=function(){return true};",
},
```

The above regex matches the string in discord that will look something like:

```js
abc.isStaff = function () {
    return !1;
};
```

Remember that Discord code is minified, so there won't be any newlines, and there will only be spaces where necessary. So the source code looks something like:

```
abc.isStaff=function(){return!1;}
```

You can find these snippets by opening the devtools (`ctrl+shift+i`) and pressing `ctrl+shift+f`, searching for what you're looking to modify in there, and beautifying the file to make it more readable.

In the `match` regex in the example shown above, you'll notice at the start there is a `(\w+)`.
Anything in the brackets will be accessible in the `replace` string using `$<number>`. e.g., the first pair of brackets will be `$1`, the second will be `$2`, etc.

The replacement string we used is:

```
"$1.isStaff=function(){return true;};"
```

Which, using the above example, would replace the code with:

> **Note**
> In this example, `$1` becomes `abc`

```js
abc.isStaff = function () {
    return true;
};
```

The match value _can_ be a string, rather than regex, however usually regex will be better suited, as it can work with unknown values, whereas strings must be exact matches.

Once you've made your plugin, make sure you run `pnpm test` and make sure your code is nice and clean!

If you want to publish your plugin into the Vencord repo, move your plugin from `src/userplugins` into the `src/plugins` folder and open a PR!

> **Warning**
> Make sure you've read [CONTRIBUTING.md](../CONTRIBUTING.md) before opening a PR

If you need more help, ask in the support channel in our [Discord Server](https://discord.gg/D9uwnFnqmd).