Subclassing Bot

Basic Subclassing

First, a basic article on subclassing will provide some fundamental knowledge, which is highly suggested before moving on to this topic, as subclassing 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 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 can be overridden to add more functionality.

You can subclass commands.Bot as shown below:

class CustomBot(commands.Bot):
    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

    # 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 get_launch_time_str(self) -> str:
        """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(
    command_prefix="!",  # Prefix can be set to any string.
    # Discord intents, refer to

# Example bot command
async def start_time(ctx):
    Creates a command with the name `start_time`.

    When invoked, sends the output of the custom method `get_launch_time_str`.
    await ctx.send(bot.get_launch_time_str())

# Having the token as an environment variable is recommended.
# Refer to

The next step would be to look into cogs as they help in organizing collections of commands into various files and folders. Refer to the official docs for more on them.