如何在 discord.py 中使用 cogs?

问题描述

我为 Discord 编写了一个相当大的机器人.它有超过 1000 行代码.当我在 Youtube 和这里​​研究如何做到这一点时,似乎没有任何效果.我想知道是否有人可以解释如何正确使用齿轮,可能还有照片示例.我可以展示我必须使用的代码来帮助您理解我想要什么.

I have quite a large bot for Discord written up. It has over 1000 lines of code. When I researched how to do it on Youtube and here, nothing seems to be working. I was wondering if someone could explain how to use a cog properly, possibly with photo examples. I can show what code I have to help you understand what I want.

举个例子,我想将我所有的 mod 命令放在一个单独的文件中,这样它就更干净、更有条理了.那么,我该怎么做呢?这是我的代码示例:

An example would be that I want to have all of my mod commands in a separate file, just so its cleaner and more organized. so, how do I go about doing this? Here is an example of my code:

Mod 命令我想使用 cog 移动到单独的文件

Mod Commands I want to move to a separate file using a cog

我目前拥有的进口商品

前缀和目录

调用令牌 ID - 令牌 ID 在上面,图中未显示:

Calling token ID - token id is above, not shown in photo:

我不确定如何完全启动一个 cog,还要导入什么,如何调用文件.我很了解 Java,但我正在尝试使用 Discord 来提高我的 Python 技能.

I am unsure how to start a cog completely, what else to import, how to call the file. I know Java well, but I am trying to work on my python skills with Discord.


解决方案

注意:

以下内容是为较旧的 0.16 版本编写的,该版本没有良好的 cogs 文档.新的 1.0 版本有很好的文档,并且完全改变了 cogs 的结构.如果您使用的是现代版本的 discord.py,你应该查阅官方文档.

每个 cog 都有两部分:一个类和一个 setup 函数.几乎所有 setup 函数看起来都一样:

Every cog has two parts: a class and a setup function. Almost all setup functions look the same:

def setup(bot):
    bot.add_cog(Cog(bot))

其中 Cog 是 cog 类.

where Cog is the cog class.

cog 类包含我们所有的命令和事件作为方法.

The cog class contains all of our commands and events as methods.

您需要进行四个主要的转换才能将您的机器人变成一个齿轮:

There are four main transformations that you need to do to change your bot to a cog:

  1. bot.command 替换为 commands.command(commandsfrom discord.ext import commands)

更改函数的签名以在开头包含 self,因为您的所有命令和事件现在都是 cog 类的方法

Change the signatures of your functions to include self at the beginning, as all of your commands and events are now methods of the cog class

将所有对 bot 的引用改为引用 self.bot

Change all references to bot to refer to self.bot instead

移除所有 bot.event 装饰器.您的 cog 中的事件侦听器仅在名称上注册

Remove all bot.event decorators. Event listeners from your cog are registered on name alone

还有一些陷阱:

  1. 从 cog 中的任何 on_message 事件中删除 await bot.process_commands(message).对于任何消息,这应该只等待一次.默认的 on_message 已经为您完成了这项工作.

  1. Remove await bot.process_commands(message) from any on_message events in your cog. For any message this should only be awaited once. The default on_message does already does this for you.

通过 cog 注册事件不会从您的主文件或其他 cog 中删除与该事件相关的其他回调.这意味着您的机器人可以多次响应 on_member_join 事件,例如,如果您在多个位置定义了该事件的行为.

Registering an event through a cog does not remove other callbacks related to that event, from your main file or other cogs. That means that your bot could respond to a on_member_join event multiple times for example, if you have behaviour for that event defined in multiple places.

示例

假设您在目录 src 中有以下 discord.py bot,bot.py:

Example

Let's say you have the following discord.py bot, bot.py in the directory src:

from discord.ext import commands

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

@bot.command(pass_context=True)
@commands.has_role("Mod")
async def acommand(ctx, argument):
   await bot.say("Stuff")

@bot.event
async def on_message(message):
    print(message.content)
    await bot.process_commands(message)

bot.run("token")

然后您将该功能分解为一个 cog src/cogs/maincog.py

You then factor that functionality out into a cog src/cogs/maincog.py

from discord.ext import commands

class MainCog:
    def __init__(self, bot):
        self.bot = bot

    @commands.command(pass_context=True)
    @commands.has_role("Mod")
    async def acommand(self, ctx, argument):
       await self.bot.say("Stuff")        

    async def on_message(self, message):
        print(message.content)

def setup(bot):
    bot.add_cog(MainCog(bot))

你的 bot.py 文件看起来像

And your bot.py file would look like

from discord.ext import commands

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

bot.load_extension("cogs.maincog")

bot.run("token")

请注意,要在 cogs/maincog.py 加载扩展,我们使用 load_extension("cogs.maincog").

Note that to load the extension at cogs/maincog.py, we use load_extension("cogs.maincog").

Cogs 还允许你定义一些特殊的方法.其中大部分仅在 discord.py-rewrite 并记录在 here.

Cogs also allow you to define some special methods. Most of these are available only in discord.py-rewrite and are documented here.

  1. __global_check,以前称为 __check,在每个命令之前运行,并且必须返回 True 才能继续执行该命令.

  1. __global_check, formerly __check, runs before every command and must return True for that command to proceed.

__local_check 仅在来自此 cog 的命令之前运行.

__local_check runs only before commands from this cog.

__global_check_once 我相信这类似于 __global_check 只是它只在子命令的情况下检查一次.我没用过这么多.

__global_check_once I believe that this is similar to __global_check except that it only checks once in the case of subcommands. I haven't used this much.

__unload 您可以通过卸载扩展程序,然后重新加载它来实时刷新您的机器人,这样您就可以在不让您的机器人离线的情况下更新您的 cogs.当您卸载扩展程序或当您的机器人停止运行时调用此方法,以防您需要进行清理.

__unload You can live refresh your bot by unloading the extension, then reloading it, allowing you to update your cogs without taking your bot offline. This method is called when you unload the extension, or when your bot stop running, in case you need to do cleanup.

__before_invoke__after_invoke 分别在来自该 cog 的每个命令之前和之后运行.

__before_invoke and __after_invoke are run before and after every command from this cog, respectively.

__error 是来自此 cog 的命令的错误处理程序.

__error is an error handler for commands from this cog.

相关文章