Skip to content

Commit cd35c16

Browse files
committedNov 9, 2024··
🎨 Format code and add pre-commit
1 parent 3e81d42 commit cd35c16

File tree

17 files changed

+433
-230
lines changed

17 files changed

+433
-230
lines changed
 

‎.pre-commit-config.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
ci:
2+
autoupdate_commit_msg: "chore(pre-commit): pre-commit autoupdate"
3+
autofix_commit_msg: "style(pre-commit): auto fixes from pre-commit.com hooks"
4+
5+
repos:
6+
- repo: https://github.com/pre-commit/pre-commit-hooks
7+
rev: v5.0.0
8+
hooks:
9+
- id: trailing-whitespace
10+
exclude: \.(po|pot|yml|yaml)$
11+
- id: end-of-file-fixer
12+
exclude: \.(po|pot|yml|yaml)$
13+
- repo: https://github.com/pre-commit/mirrors-prettier
14+
rev: v4.0.0-alpha.8
15+
hooks:
16+
- id: prettier
17+
args: [--prose-wrap=always, --print-width=88]
18+
exclude: \.(po|pot|yml|yaml)$
19+
- repo: https://github.com/astral-sh/ruff-pre-commit
20+
# Ruff version.
21+
rev: v0.7.3
22+
hooks:
23+
# Run the linter.
24+
- id: ruff
25+
args: [ --fix ]
26+
# Run the formatter.
27+
- id: ruff-format
28+
- repo: https://github.com/DetachHead/basedpyright-pre-commit-mirror
29+
rev: v1.13.0 # or whatever the latest version is at the time
30+
hooks:
31+
- id: basedpyright

‎guides/getting-started.md

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Getting Started with Botkit and Pycord: Creating Your First Bot Extension
22

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.
46

57
## Prerequisites
68

@@ -10,18 +12,21 @@ Before we begin, ensure you have the following:
1012
2. Basic understanding of Python and Discord concepts
1113
3. A Discord account and access to the Discord Developer Portal
1214

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.
1518
1619
## Step 1: Install Git
1720

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.
1923

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.
2126
2. Follow the installation instructions for your OS.
2227

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.
2530
2631
To verify Git is installed correctly, open a terminal or command prompt and run:
2732

@@ -49,12 +54,13 @@ git clone https://github.com/nicebots-xyz/botkit
4954
cd botkit
5055
```
5156

52-
> [!NOTE]
53-
> 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.
5459
5560
## Step 3: Set Up a Virtual Environment (Optional but Recommended)
5661

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.
5864

5965
1. Create a virtual environment:
6066

@@ -63,17 +69,17 @@ python -m venv venv
6369
```
6470

6571
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.
7783
7884
## Step 4: Install Dependencies
7985
@@ -91,8 +97,8 @@ pip install pdm
9197
pdm install
9298
```
9399

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.
96102
97103
## Step 5: Configure Your Bot
98104

@@ -106,8 +112,8 @@ bot:
106112
107113
Replace `YOUR_BOT_TOKEN_HERE` with the actual token of your Discord bot.
108114

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.
111117

112118
## Step 6: Create a New Extension Folder
113119

@@ -138,8 +144,9 @@ from .main import setup, default, schema
138144
__all__ = ["setup", "default", "schema"]
139145
```
140146

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.
143150

144151
## Step 8: Create the `main.py` File
145152

@@ -177,17 +184,20 @@ schema = {
177184
Let's break down what we've done here:
178185

179186
- 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.
182191
- The `setup` function is required by Botkit to add our cog to the bot.
183192
- We define `default` and `schema` dictionaries for the extension's configuration.
184193

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.
187196

188197
## Step 9: Adding Commands
189198

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.
191201

192202
Add the following methods to your `MyFirstExtension` class in `main.py`:
193203

@@ -215,22 +225,27 @@ async def userinfo(
215225
Let's explain these commands:
216226

217227
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.
221233

222234
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.
227240

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.
230244

231245
## Step 10: Adding an Event Listener
232246

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.
234249

235250
Add the following method to your `MyFirstExtension` class in `main.py`:
236251

@@ -240,10 +255,11 @@ async def on_ready(self):
240255
print(f"Bot is ready! Logged in as {self.bot.user}")
241256
```
242257

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.
244260

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.
247263

248264
## Step 11: Final `main.py` File
249265

@@ -304,25 +320,29 @@ Now that we've created our extension, let's run the bot:
304320
pdm run start
305321
```
306322

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.
309325

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.
311328

312329
## Conclusion
313330

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:
315333

316334
1. A simple "hello" slash command
317335
2. A more complex "userinfo" slash command that creates an embed
318336
3. An event listener for the "on_ready" event
319337

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.
322340

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.
325343

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.
327347

328-
Happy coding, and enjoy building your Discord bot!
348+
Happy coding, and enjoy building your Discord bot!

‎pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ dev = [
4949
]
5050

5151
[tool.pyright]
52+
venvPath = "."
53+
venv = ".venv"
5254
reportAny = false
5355
reportUnusedCallResult = false
5456
reportUnknownMemberType = false
57+
reportMissingTypeStubs = false
5558
pythonVersion = "3.12"
5659

5760
[tool.ruff]
@@ -98,4 +101,4 @@ extend-ignore = [
98101
"D203",
99102
"FBT001",
100103
"FBT002"
101-
]
104+
]

0 commit comments

Comments
 (0)
Please sign in to comment.