You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+58Lines changed: 58 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -118,6 +118,64 @@ schema = {
118
118
```
119
119
We really encourage you to follow these instructions, even if you’re coding privately, as it will make your code more readable and maintainable in the long run.
120
120
121
+
## Using Patch Files
122
+
123
+
Botkit supports the use of patch files to modify or extend the functionality of the bot or its dependencies before the main extension code runs. This is particularly useful for applying global changes or monkey-patching existing classes.
124
+
125
+
### How It Works
126
+
127
+
1. Create a file named `patch.py` in your extension's directory.
128
+
2. Define a `patch()` function in this file. This function will be called before the extension is loaded.
129
+
3. The `patch()` function can modify global state, patch classes, or perform any other setup needed.
130
+
131
+
### Example: Error Handling Patch
132
+
133
+
Here's an example from the `nice-errors` extension that demonstrates how to use a patch file to enhance error handling:
134
+
135
+
```python
136
+
# nice-errors/patch.py
137
+
138
+
import discord
139
+
from discord import Interaction
140
+
from discord.ui import Item
141
+
from typing_extensions import override
142
+
143
+
def patch():
144
+
class PatchedView(discord.ui.View):
145
+
@override
146
+
async def on_error(
147
+
self,
148
+
error: Exception,
149
+
item: Item,
150
+
interaction: Interaction,
151
+
) -> None:
152
+
if not isinstance(error, discord.Forbidden):
153
+
await interaction.respond(
154
+
"Whoops! An error occurred while executing this command",
155
+
ephemeral=True,
156
+
)
157
+
raise error
158
+
await interaction.respond(
159
+
f"Whoops! I don't have permission to do that\n`{error.args[0].split(':')[-1].strip()}`",
160
+
ephemeral=True,
161
+
)
162
+
163
+
discord.ui.View = PatchedView
164
+
165
+
```
166
+
167
+
This patch modifies the `discord.ui.View` class to provide more user-friendly error messages. It catches exceptions and responds to the user with an appropriate message, enhancing the overall user experience.
168
+
169
+
### When to Use Patch Files
170
+
171
+
Patch files are powerful but should be used judiciously. They are best suited for:
172
+
173
+
1. Applying global changes that affect multiple parts of your bot.
174
+
2. Modifying third-party libraries when you can't or don't want to fork them.
175
+
3. Implementing cross-cutting concerns like logging or error handling.
176
+
177
+
Remember that patches are applied early in the bot's lifecycle, so they can affect all subsequent code. Use them carefully and document their effects clearly.
0 commit comments