From e3576f83993109b49711a2c7c705b2a833107832 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Wed, 17 May 2023 00:47:24 +0530 Subject: use launch_time attribute in the example method and command --- .../guides/python-guides/subclassing_bot.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'pydis_site/apps/content/resources/guides') 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 2562d606..47186e3d 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 @@ -1,13 +1,13 @@ --- title: Subclassing Bot -description: "Subclassing the discord.py Bot class to add more functionality and customisability." +description: "Subclassing the discord.py Bot class to add more functionality and customizability." --- ## 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. +## The Benefits of Subclassing Bot +Subclassing `Bot` can be very beneficial as it provides you with more control and customizability 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 overridden to add more functionality. You can subclass `commands.Bot` as shown below: ```python @@ -27,9 +27,9 @@ class CustomBot(commands.Bot): 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." + def get_launch_time_str(self) -> datetime.datetime: + """Get bot launch datetime without milliseconds in UTC and status.""" + return f"Bot started at: {self.launch_time.strftime('%F %T')} UTC." # All arguments as passed to commands.Bot can be passed here. bot = CustomBot( @@ -41,13 +41,13 @@ bot = CustomBot( # Example bot command @bot.command() -async def ping(ctx): +async def start_time(ctx): """ - Creates a command with the name `ping`. + Creates a command with the name `start_time`. - When invoked, sends `pong`. + When invoked, sends the output of the custom method `get_launch_time_str`. """ - await ctx.send("pong") + await ctx.send(bot.get_launch_time_str()) # Having the token as an environment variable is recommended. -- cgit v1.2.3