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: guides/getting-started.md
+75-55Lines changed: 75 additions & 55 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Getting Started with Botkit and Pycord: Creating Your First Bot Extension
2
2
3
-
This comprehensive tutorial will guide you through the process of setting up Botkit, creating your first bot extension using Pycord, and understanding the core concepts of Discord bot development.
3
+
This comprehensive tutorial will guide you through the process of setting up Botkit,
4
+
creating your first bot extension using Pycord, and understanding the core concepts of
5
+
Discord bot development.
4
6
5
7
## Prerequisites
6
8
@@ -10,18 +12,21 @@ Before we begin, ensure you have the following:
10
12
2. Basic understanding of Python and Discord concepts
11
13
3. A Discord account and access to the Discord Developer Portal
12
14
13
-
> [!IMPORTANT]
14
-
> If you haven't already, create a Discord application and bot user in the [Discord Developer Portal](https://discord.com/developers/applications). You'll need the bot token for later steps.
15
+
> [!IMPORTANT] If you haven't already, create a Discord application and bot user in the
16
+
> [Discord Developer Portal](https://discord.com/developers/applications). You'll need
17
+
> the bot token for later steps.
15
18
16
19
## Step 1: Install Git
17
20
18
-
If you don't have Git installed, you'll need to install it to clone the Botkit repository.
21
+
If you don't have Git installed, you'll need to install it to clone the Botkit
22
+
repository.
19
23
20
-
1. Visit the [Git website](https://git-scm.com/downloads) and download the appropriate version for your operating system.
24
+
1. Visit the [Git website](https://git-scm.com/downloads) and download the appropriate
25
+
version for your operating system.
21
26
2. Follow the installation instructions for your OS.
22
27
23
-
> [!TIP]
24
-
> On Windows, you can use the Git Bash terminal that comes with Git for a Unix-like command-line experience.
28
+
> [!TIP] On Windows, you can use the Git Bash terminal that comes with Git for a
29
+
> Unix-like command-line experience.
25
30
26
31
To verify Git is installed correctly, open a terminal or command prompt and run:
> Cloning the repository creates a local copy of Botkit on your machine, allowing you to build your bot using the Botkit framework.
57
+
> [!NOTE] Cloning the repository creates a local copy of Botkit on your machine,
58
+
> allowing you to build your bot using the Botkit framework.
54
59
55
60
## Step 3: Set Up a Virtual Environment (Optional but Recommended)
56
61
57
-
It's a good practice to use a virtual environment for your Python projects. This keeps your project dependencies isolated from your system-wide Python installation.
62
+
It's a good practice to use a virtual environment for your Python projects. This keeps
63
+
your project dependencies isolated from your system-wide Python installation.
58
64
59
65
1. Create a virtual environment:
60
66
@@ -63,17 +69,17 @@ python -m venv venv
63
69
```
64
70
65
71
2. Activate the virtual environment:
66
-
- On Windows:
67
-
```
68
-
venv\Scripts\activate
69
-
```
70
-
- On macOS and Linux:
71
-
```
72
-
source venv/bin/activate
73
-
```
74
-
75
-
> [!TIP]
76
-
> You'll know the virtual environment is active when you see `(venv)` at the beginning of your terminal prompt.
72
+
- On Windows:
73
+
```
74
+
venv\Scripts\activate
75
+
```
76
+
- On macOS and Linux:
77
+
```
78
+
source venv/bin/activate
79
+
```
80
+
81
+
> [!TIP] You'll know the virtual environment is active when you see `(venv)` at the
82
+
> beginning of your terminal prompt.
77
83
78
84
## Step 4: Install Dependencies
79
85
@@ -91,8 +97,8 @@ pip install pdm
91
97
pdm install
92
98
```
93
99
94
-
> [!NOTE]
95
-
> PDM will read the `pyproject.toml` file and install all necessary dependencies for Botkit.
100
+
> [!NOTE] PDM will read the `pyproject.toml` file and install all necessary dependencies
101
+
> for Botkit.
96
102
97
103
## Step 5: Configure Your Bot
98
104
@@ -106,8 +112,8 @@ bot:
106
112
107
113
Replace `YOUR_BOT_TOKEN_HERE` with the actual token of your Discord bot.
108
114
109
-
> [!CAUTION]
110
-
> Never share your bot token publicly or commit it to version control. Treat it like a password.
115
+
> [!CAUTION] Never share your bot token publicly or commit it to version control. Treat
116
+
> it like a password.
111
117
112
118
## Step 6: Create a New Extension Folder
113
119
@@ -138,8 +144,9 @@ from .main import setup, default, schema
138
144
__all__ = ["setup", "default", "schema"]
139
145
```
140
146
141
-
> [!NOTE]
142
-
> This file imports and exposes the necessary components from our `main.py` file (which we'll create next). It allows Botkit to access these components when loading the extension.
147
+
> [!NOTE] This file imports and exposes the necessary components from our `main.py` file
148
+
> (which we'll create next). It allows Botkit to access these components when loading
149
+
> the extension.
143
150
144
151
## Step 8: Create the `main.py` File
145
152
@@ -177,17 +184,20 @@ schema = {
177
184
Let's break down what we've done here:
178
185
179
186
- We import the necessary modules from discord and discord.ext.
180
-
- We use `typing` to add type hints, which improves code readability and helps catch errors early.
181
-
- We define a `MyFirstExtension` class that inherits from `commands.Cog`. This class will contain our commands and listeners.
187
+
- We use `typing` to add type hints, which improves code readability and helps catch
188
+
errors early.
189
+
- We define a `MyFirstExtension` class that inherits from `commands.Cog`. This class
190
+
will contain our commands and listeners.
182
191
- The `setup` function is required by Botkit to add our cog to the bot.
183
192
- We define `default` and `schema` dictionaries for the extension's configuration.
184
193
185
-
> [!TIP]
186
-
> Using type hints (like `bot: discord.Bot`) helps catch errors early and improves code readability. It's a good practice to use them consistently in your code.
194
+
> [!TIP] Using type hints (like `bot: discord.Bot`) helps catch errors early and
195
+
> improves code readability. It's a good practice to use them consistently in your code.
187
196
188
197
## Step 9: Adding Commands
189
198
190
-
Now, let's add some commands to our extension. We'll create a simple "hello" command and a more complex "userinfo" command.
199
+
Now, let's add some commands to our extension. We'll create a simple "hello" command and
200
+
a more complex "userinfo" command.
191
201
192
202
Add the following methods to your `MyFirstExtension` class in `main.py`:
193
203
@@ -215,22 +225,27 @@ async def userinfo(
215
225
Let's explain these commands:
216
226
217
227
1. The `hello` command:
218
-
- Uses the `@discord.slash_command` decorator to create a slash command.
219
-
- Takes only the `ctx` (context) parameter, which is automatically provided by Discord.
220
-
- Responds with a greeting using the author's name.
228
+
229
+
- Uses the `@discord.slash_command` decorator to create a slash command.
230
+
- Takes only the `ctx` (context) parameter, which is automatically provided by
231
+
Discord.
232
+
- Responds with a greeting using the author's name.
221
233
222
234
2. The `userinfo` command:
223
-
- Also uses `@discord.slash_command` to create a slash command.
224
-
- Takes an optional `user` parameter, which defaults to the command author if not provided.
225
-
- Creates an embed with various pieces of information about the user.
226
-
- Responds with the created embed.
235
+
- Also uses `@discord.slash_command` to create a slash command.
236
+
- Takes an optional `user` parameter, which defaults to the command author if not
237
+
provided.
238
+
- Creates an embed with various pieces of information about the user.
239
+
- Responds with the created embed.
227
240
228
-
> [!NOTE]
229
-
> Slash commands are the modern way to create Discord bot commands. They provide better user experience and are easier to discover than traditional prefix-based commands.
241
+
> [!NOTE] Slash commands are the modern way to create Discord bot commands. They provide
242
+
> better user experience and are easier to discover than traditional prefix-based
243
+
> commands.
230
244
231
245
## Step 10: Adding an Event Listener
232
246
233
-
Let's add an event listener to our extension to demonstrate how to respond to Discord events. We'll add a simple listener that logs when the bot is ready.
247
+
Let's add an event listener to our extension to demonstrate how to respond to Discord
248
+
events. We'll add a simple listener that logs when the bot is ready.
234
249
235
250
Add the following method to your `MyFirstExtension` class in `main.py`:
236
251
@@ -240,10 +255,11 @@ async def on_ready(self):
240
255
print(f"Bot is ready! Logged in as {self.bot.user}")
241
256
```
242
257
243
-
This listener will print a message to the console when the bot has successfully connected to Discord.
258
+
This listener will print a message to the console when the bot has successfully
259
+
connected to Discord.
244
260
245
-
> [!TIP]
246
-
> Event listeners are great for performing actions based on Discord events, such as when a member joins a server or when a message is deleted.
261
+
> [!TIP] Event listeners are great for performing actions based on Discord events, such
262
+
> as when a member joins a server or when a message is deleted.
247
263
248
264
## Step 11: Final `main.py` File
249
265
@@ -304,25 +320,29 @@ Now that we've created our extension, let's run the bot:
304
320
pdm run start
305
321
```
306
322
307
-
> [!IMPORTANT]
308
-
> Ensure your bot token is correctly set in the `config.yml` file before running the bot.
323
+
> [!IMPORTANT] Ensure your bot token is correctly set in the `config.yml` file before
324
+
> running the bot.
309
325
310
-
If everything is set up correctly, you should see the "Bot is ready!" message in your console, indicating that your bot is now online and ready to respond to commands.
326
+
If everything is set up correctly, you should see the "Bot is ready!" message in your
327
+
console, indicating that your bot is now online and ready to respond to commands.
311
328
312
329
## Conclusion
313
330
314
-
Congratulations! You've now created your first bot extension using Botkit and Pycord. This extension includes:
331
+
Congratulations! You've now created your first bot extension using Botkit and Pycord.
332
+
This extension includes:
315
333
316
334
1. A simple "hello" slash command
317
335
2. A more complex "userinfo" slash command that creates an embed
318
336
3. An event listener for the "on_ready" event
319
337
320
-
> [!TIP]
321
-
> To continue improving your bot, consider adding more commands, implementing additional event listeners, or integrating with external APIs or databases.
338
+
> [!TIP] To continue improving your bot, consider adding more commands, implementing
339
+
> additional event listeners, or integrating with external APIs or databases.
322
340
323
-
> [!WARNING]
324
-
> Always be cautious when handling user data and permissions in your bot. Ensure you're following Discord's Terms of Service and Developer Policy.
341
+
> [!WARNING] Always be cautious when handling user data and permissions in your bot.
342
+
> Ensure you're following Discord's Terms of Service and Developer Policy.
325
343
326
-
Remember to always use type hinting in your code. It helps with code readability, catches potential errors early, and provides better autocomplete suggestions in many IDEs.
344
+
Remember to always use type hinting in your code. It helps with code readability,
345
+
catches potential errors early, and provides better autocomplete suggestions in many
346
+
IDEs.
327
347
328
-
Happy coding, and enjoy building your Discord bot!
348
+
Happy coding, and enjoy building your Discord bot!
0 commit comments