Creating New Cogs¶
What is a Cog?¶
In the words of the Discord API docs, a cog is “a collection of commands, listeners, and optional state to help group commands together”. A more intuitive way to think of what a cog is to recall what a cog is in a machine: an individual component that serves some purpose within the machine. Cogs are a nice way to think of grouping toghether functionalities that the bot should have.
How Can I Set Up a Cog?¶
Create a new python module in the
src/cogs/cogs directory.Open the aforementioned file in your editor of choice and do the following
Copy and paste this expression to initialize logging for the module:
logger = getLogger(f"main.{__name__}")Define a class that inherits from
commands.Cogand defines its constructor as the following:
def __init__(self, client): self.client = client
To define a new command, create an asynchronous method and decorate it with the @commands.command decorator
That’s it. To pull it all toghether by definining a cog that replys “hello” when the user types the command “/hi” and logs the user that entered the command
from logging import getLogger
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands.context import Context
class Hello(commands.Cog):
def __init__(self, client: Bot):
self.client = client
@commands.command(alias=['hi'])
async def say_hello(self, ctx: Context):
sender = ctx.author
await sender.send("hello")
logger.debug(f"{sender} said hi")