aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/resources
diff options
context:
space:
mode:
authorGravatar RohanJnr <[email protected]>2023-05-16 23:04:04 +0530
committerGravatar RohanJnr <[email protected]>2023-05-16 23:04:04 +0530
commit548a81e0e3060483b693079f994fcb70e01084e7 (patch)
treea79aef291df573d2d76bc0d3a75f6ad3bcb06e8f /pydis_site/apps/content/resources
parentMerge branch 'main' into subclassing_bot (diff)
condesed the 2 examples in 1 and added additional comments for reference.
Diffstat (limited to 'pydis_site/apps/content/resources')
-rw-r--r--pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md70
1 files changed, 35 insertions, 35 deletions
diff --git a/pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md b/pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md
index 91df2199..2562d606 100644
--- a/pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md
+++ b/pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md
@@ -4,55 +4,55 @@ description: "Subclassing the discord.py Bot class to add more functionality and
---
## Basic Subclassing
-First, a [basic article](https://www.codesdope.com/course/python-subclass-of-a-class/) on subclassing will provide some fundamental knowledge, which is highly suggested before moving on to this topic, as subclassing [`Bot`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot) can ultimately be a complicated task.
+First, a [basic article](https://www.codesdope.com/course/python-subclass-of-a-class/) on subclassing will provide some fundamental knowledge, which is highly suggested before moving on to this topic, as subclassing [`Bot`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot) can ultimately be a complicated task.
## The benefits of subclassing bot
Subclassing Bot can be very beneficial as it provides you with more control and customisability of how your bot functions, also allowing you to add extra features, such as custom bot attributes or methods. For example, the default [Context](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Context) can be overriden to add more functionality.
-There are two ways to subclass `commands.Bot`, as shown below:
+You can subclass `commands.Bot` as shown below:
```python
class CustomBot(commands.Bot):
- def __init__(self):
- super().__init__(
- command_prefix= # Your prefix here as a string
- intents= # Your intents here
- # Other kwargs can be put here
- )
- # custom bot attributes can be set here, for example:
+ def __init__(self, *args, **kwargs) -> None:
+ # Forward all arguments, and keyword-only arguments to commands.Bot
+ super().__init__(*args, **kwargs)
+
+ # Custom bot attributes can be set here.
self.launch_time = datetime.datetime.utcnow()
self.example_integer = 5
-
- async def start(self, *args, **kwargs):
- # here you are overriding the default start method. You can do some code here for example establish a database connection
- # as shown in this example below
- self.db = await aiosqlite.connect('database file name.db')
+ # Here you are overriding the default start method and write your own code.
+ async def start(self, *args, **kwargs) -> None:
+ """Establish a database connection."""
+ self.db = await aiosqlite.connect('sqlite.db')
await super().start(*args, **kwargs)
+ # Example of a custom bot method
+ def status(self) -> str:
+ """Get bot launch time in UTC and status."""
+ return f"Bot started at {self.launch_time}, running."
-bot = CustomBot()
-token = YOUR_TOKEN_HERE
-bot.run(token)
-```
-Or
-```python
-class CustomBot(commands.Bot):
- def __init__(self, *args, **kwargs): # the key-word arguments are not specified here, unlike the example above
+# All arguments as passed to commands.Bot can be passed here.
+bot = CustomBot(
+ command_prefix="!", # Prefix can be set to any string.
+ # Discord intents, refer to https://discordpy.readthedocs.io/en/stable/intents.html
+ intents=discord.Intents.default()
+)
- super().__init__(*args, **kwargs)
- # custom bot attributes can be set here, for example:
- self.example_string = 'This is an example!'
- # You can add a custom bot method, anyhting can be done in this function. This is an example:
- def hello(self):
- return 'Hello World'
+# Example bot command
+async def ping(ctx):
+ """
+ Creates a command with the name `ping`.
-# Here you set the *args and **kwargs
-bot = CustomBot(command_prefix="!", intents=discord.Intents.default())
+ When invoked, sends `pong`.
+ """
+ await ctx.send("pong")
-async def example(ctx):
- print(bot.hello())
- # In this case, this will print Hello World!
+
+# Having the token as an environment variable is recommended.
+# Refer to https://www.pythondiscord.com/pages/guides/python-guides/keeping-tokens-safe/
+token = YOUR_TOKEN_HERE
+bot.run(token)
```
-With either of the above examples, you are not required to change any of the existing or future code, it is identical to code done without subclassing bot.
+With the above example, you are not required to change any of the existing or future code, it is identical to code done without subclassing bot.