discord.py 中的 Cog 和 Extension 有什么区别?

2022-01-15 00:00:00 python discord discord.py

问题描述

在 discord.py 文档中,有扩展:https://discordpy.readthedocs.io/en/stable/ext/commands/extensions.html和齿轮:https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html有什么区别?

In the discord.py documention, there are Extensions: https://discordpy.readthedocs.io/en/stable/ext/commands/extensions.html and Cogs: https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html what is the difference?


解决方案

扩展是被加载的文件,当你调用 load_extension 时,将它们视为 discord.py 库导入的模块.导入发生后,一个 setup 函数被调用,并被传递给它被加载到的 Bot 实例......本质上是 module.setup(bot).就是这样,没有更多的扩展......通常这个设置函数调用 add_cog 将在下面描述,但是没有要求他们这样做.

Extensions are files that are loaded, think of them as modules that the discord.py library imports when you call load_extension. After the import happens, a setup function is called, and is passed the Bot instance that it was loaded into....essentially module.setup(bot). That is it, there is no more to extensions...typically this setup function calls add_cog which will be described next, however there is no requirement that they do so.

Cog 是从 commands.Cog 继承的类,并通过 add_cog 添加到机器人的 cog 列表中,这些类通常包含命令并充当"类别"对于这些命令.它还可以容纳诸如 on_messageon_member_join 等事件的侦听器.

A Cog is a class that inherits from commands.Cog and is added to the bot's list of cogs through add_cog, these classes typically house commands and act as a "category" for these commands. It can also house listeners for events such as on_message or on_member_join, etc.

TL;DR - 扩展是导入的模块,cogs 是类

相关文章