aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content
diff options
context:
space:
mode:
Diffstat (limited to 'pydis_site/apps/content')
-rw-r--r--pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md58
1 files changed, 58 insertions, 0 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
new file mode 100644
index 00000000..91df2199
--- /dev/null
+++ b/pydis_site/apps/content/resources/guides/python-guides/subclassing_bot.md
@@ -0,0 +1,58 @@
+---
+title: Subclassing Bot
+description: "Subclassing the discord.py Bot class to add more functionality and customisability."
+---
+
+## 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.
+
+## 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:
+```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:
+ 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')
+ await super().start(*args, **kwargs)
+
+
+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
+
+ 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'
+
+# Here you set the *args and **kwargs
+bot = CustomBot(command_prefix="!", intents=discord.Intents.default())
+
+async def example(ctx):
+ print(bot.hello())
+ # In this case, this will print Hello World!
+```
+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.