Understand Cog and Extension in discord.py

Thanks~.

I'm Sumidora who recently started a server review of discord on note. Please look by all means. Please.

https://note.com/sizumita

Introduction

When I started using the ext.commands framework in discord.py, many people tried to use Cog but it didn't work. I would like to explain for such a person.

What is Cog?

As described in Explanation of Cog, commands, listeners, and some states are described in one class in Bot development. Made to summarize in. For example, it is like classifying commands by category or separating listeners by function. It's object-oriented. Cog can be created by creating a class that inherits commands.Cog. Like this:

from discord.ext import commands


class MyCog(commands.Cog):
    pass

Make commands in Cog

To create a command in Cog, we use a slightly different method. Normally, when you make a command with a bot, you use @ bot.command (), but when you make it in Cog, you use @ commands.command (). Like this:

from discord.ext import commands


class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def cat(self, ctx):
        await ctx.send('Nyan?')

    @commands.command()
    async def dog(self, ctx):
        await ctx.send('Wow!')

Use event listeners in Cog

If you want to get an event like @ bot.event in Cog, use@ commands.Cog.listener (). This listener has a name argument that you can use to make a function with any name a listener! Also, since all listeners are independent, you can create as many as you like. Like this:

from discord.ext import commands


class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener(name='on_message')
    async def good_reaction(self, message):
        if message.author.bot:
            return
        if 'How nice' in message.content:
            await message.add_reaction('\U0001f44d')

I often add a bot argument to \ _ \ _ init \ _ \ _, but what?

For example, if you want to use a function in a bot, such as bot.reload_extension, you need to refer to an instance of the bot. So, by passing it when __init__, you can refer to it later.

What is Extension?

It is also in Extension Description, but it is a function that makes hot reloading easy. With this, you can change the command without dropping the bot. Extension can be load / reload / unload. ** Extension files require the setup function. ** This setup function is called whenever the Extension is loaded. Conversely, the teardown function is called when it is unloaded.

For example:

def setup(bot):
    print('Loaded')

You can use the extension just by reading the file that says.

An example of Extension is here

Cog and Extension combination technique

Cog in the world is, for example:

from cogfile import MyCog
from discord.ext import commands

bot = commands.Bot()

bot.add_cog(MyCog(bot))

You can load it by doing, but when you change the contents of Cog, you may want to reload without dropping the bot. In such a case, using the combination technique of Cog and Extension makes it super easy to apply! ::

cogfile.py


from discord.ext import commands


class MyCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener(name='on_message')
    async def good_reaction(self, message):
        if message.author.bot:
            return
        if 'How nice' in message.content:
            await message.add_reaction('\U0001f44d')


#This is important
def setup(bot):
    return bot.add_cog(MyCog(bot))

thus

# from cogfile import MyCog
from discord.ext import commands

bot = commands.Bot()

# bot.add_cog(MyCog(bot))
bot.load_extension('cogfile')

By doing so, you can load it in a form that allows hot reloading!

Caution

About 80% of Cogs in the world use both Cog and Extension, but there are many people who forget the setup function and get an error when copying it. ** setup function is required **.

Also, Cog and Extension are different! Some people have mistaken it for the same thing, so please let me know.

at the end

I want this kind of explanation / commentary on Cog and Extension! If you like, please write in the comments.

Recommended Posts

Understand Cog and Extension in discord.py
nest cog in discord.py
nest cog in discord.py
Understand Cog and Extension in discord.py
Understanding VQ-VAE
Understanding Concatenate
Implement and understand union-find trees in Go
Carefully understand the exponential distribution and draw in Python
Plot and understand the multivariate normal distribution in Python
Carefully understand the Poisson distribution and draw in Python
Understand in 10 minutes Selenium
Updated messages in discord.py
Neural network to understand and implement in high school mathematics
Understand t-SNE and improve visualization
Understand PyTorch's DataSet and DataLoader (2)
Clipping and normalization in TensorFlow
Understand PyTorch's DataSet and DataLoader (1)
Stack and Queue in Python
Implement extension field in Python
Unittest and CI in Python
I understand Python in Japanese!
Search the file name including the specified word and extension in the directory