[PYTHON] [Discord.py] Understanding Cog


I studied about Cog on Discord.py, so I wrote the memo and for those people who said "I don't understand even if I read the reference !!".


**: warning: Note: This is a description of Cog. If you haven't mastered Discord.py yet, please master it first. ** **


What is Cog ??

What is Cog in the first place? ??

In bot development, you may want to combine commands, listeners, and several states into one class. Cog is the realization of that.

It is written in Discord.py document, but even if you look at this, some people will be "insignificant" Isn't it? (I was the same w)

By applying Cog, you can divide the Discord.py file into two or more files, and you can easily reload the file even while the bot is running !!

How to write Cog

command

When not using Cog, write as follows when defining the command,

@bot.command()
async def test(ctx):
    pass #Execution content

When defining a command in Cog, write as follows.

@commands.command()
async def test(self,ctx):
    pass #Execution content

The only difference is that @ bot. ~ is now @ commands. ~ And that self is added as an argument (on the surface).

Event

When not using Cog, when defining the event, write as follows,

@bot.event
async def on_ready():
    pass #Execution content

When defining an event in Cog, write as follows.

@commands.Cog.listener()
async def on_ready(self):
    pass #Execution content

The difference is that @ bot.event is now@ commands.Cog.listener ()and self is added to the argument as before.

How to use the variable Bot

When not using Cog, when displaying the name of the bot, write as follows,

print(bot.user)

When used in Cog, it looks like this:

print(self.bot.user)

The only difference is that you have to write self.

Add Cog

When the same file

First, let's call Cog of the same file.

main.py


import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")

class Greetings(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._last_member = None

    @commands.command()
    async def test(self,ctx):
        await ctx.send("test!")

bot.add_cog(Greetings(bot))

I feel like this. In other words, Cog is loaded with bot.add_cog (Class name (bot)) !!

When it is a separate file

.
┣━main.py
┗━sub.py

If the file directory is as above and you want to read ClassGreetings in sub.py, you can read it by doing the following !!

main.py


import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")

import sub

bot.add_cog(sub.Greetings(bot))

First, ʻimport sub loads sub.py, and bot.add_cog (sub.Greetings (bot)) loads Greetingsinsub.py` !!

For directories other than the above

You can read directories other than the above by using ʻimport` in the same way. For details, see "Summary of how to import files in Python 3".

What is Extension ??

Extension is also mentioned in above However, it is a function that allows you to easily reload files while the bot is running.

By using Extension, you don't have to specify the file using ʻimport`.

Example

.
┣━ main.py
┗━ sub.py

If the file directory looks like the one above, you can load the Greetings of sub.py by doing the following !!

main.py


import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!")

bot.load_extension("sub")

sub.py


import discord
from discord.ext import commands

class Greetings(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._last_member = None

    @commands.command()
    async def test(self,ctx):
        pass

def setup(bot):
    return bot.add_cog(Greetings(bot))

Read file

bot.load_extension() Code to read the Cog file. You can do it with bot.load_extension ("filename ").

.
┣━ main.py
┗━ Cog
 ┗━ sub.py

If the file directory looks like the one above, you can load it with bot.load_extension ("Cog.sub ").

The most important guy to use Extension

def setup(bot):
    return bot.add_cog(Greetings(bot))

**: warning: Extension cannot be used without this! ** Write it at the bottom of the file. How to write

def setup(bot):
    return bot.add_cog(Class name(bot))

is.

Finally

Please let us know if you have any questions or information you would like us to add.


End of previous article advertises advertising bots I did. So, this time I would like to introduce Takkun's Server.

This server is a chat server created by Takkun # 1643 and a support server for bots created by Takkun # 1643. In the future, we plan to move the support server for advertising bots here as well!

[participate] https://discord.gg/VX7ceJw

Recommended Posts

[Discord.py] Understanding Cog
nest cog in discord.py
Understand Cog and Extension in discord.py
Understanding VQ-VAE
Understanding Concatenate