用户登录
用户注册

分享至

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

  • 作者: 段子他亲爹
  • 来源: 51数据库
  • 2023-02-01

问题描述

在 discord.py 文档中,有扩展:http://www.51sjk.com/Upload/Articles/1/0/342/342147_20230201092422753.html和齿轮:http://www.51sjk.com/Upload/Articles/1/0/342/342147_20230201092423316.html有什么区别?

In the discord.py documention, there are Extensions: http://www.51sjk.com/Upload/Articles/1/0/342/342147_20230201092422753.html and Cogs: http://www.51sjk.com/Upload/Articles/1/0/342/342147_20230201092423316.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_message 或 on_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 是类

软件
前端设计
程序设计
Java相关