Thanks~.
I'm Sumidora who recently started a server review of discord on note. Please look by all means. Please.
https://note.com/sizumita
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.
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
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!')
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')
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.
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 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!
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.
I want this kind of explanation / commentary on Cog and Extension! If you like, please write in the comments.
Recommended Posts