如何与discord.py一起使用嵌齿轮? [英] How do I use cogs with discord.py?

查看:105
本文介绍了如何与discord.py一起使用嵌齿轮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个相当大的不和谐机器人.它具有超过1000行代码.当我研究如何在youtube上和此处进行操作时,似乎没有任何效果.我想知道是否有人可以通过照片示例解释如何正确使用玉米棒.我可以显示我需要哪些代码来帮助您了解我想要的东西.

一个例子是,我想将我所有的mod命令放在一个单独的文件中,以使其更加整洁和井井有条.所以,我该怎么做呢?这是我的代码示例:

Mod命令,我想使用嵌齿轮移动到单独的文件

当前导入我拥有的

前缀和目录

呼叫令牌ID-令牌ID在上方,未在照片中显示

我不确定如何完全启动齿轮,还需要导入什么,如何调用文件.我非常了解Java,但是我正在尝试以Discord的方式学习python技能.先感谢您!

解决方案

注意:

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

简介

每个齿轮都有两个部分:类和setup函数.几乎所有setup函数看起来都相同:

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

其中Cog是齿轮类.

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

主要变化

要将机器人更改为齿轮需要做四个主要的转变:

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

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

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

  4. 删除所有bot.event装饰器.来自您齿轮的事件侦听器仅以名称进行注册

还有一些陷阱:

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

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

示例

假设您在目录src中具有以下discord.py机器人bot.py:

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")

然后您将该功能分解为齿轮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文件看起来像

from discord.ext import commands

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

bot.load_extension("cogs.maincog")

bot.run("token")

请注意,要使用load_extension("cogs.maincog")加载扩展名,请使用load_extension("cogs.maincog").

其他功能

Cogs还允许您定义一些特殊方法.其中大多数仅在,并在此处

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

  2. __local_check仅在此齿轮发出的命令之前运行.

  3. __global_check_once我相信,这与__global_check相似,只是在子命令的情况下它仅检查一次.我没用太多.

  4. __unload您可以通过卸载扩展程序,然后重新加载它来实时刷新您的机器人,从而使您可以在不使机器人离线的情况下更新齿轮.当您卸载扩展程序时,或者在您的机器人停止运行以防万一需要清理时,将调用此方法.

  5. __before_invoke__after_invoke分别在此齿轮的每个命令之前和之后运行.

  6. __error是此齿轮命令的错误处理程序.

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 cob properly, possibly with photo examples. I can show what code I have to help you understand what I want.

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 Commands I want to move to a separate file using a cog

Imports currently that I have

Prefix and directory

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

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. Thank you in advance!

解决方案

Note:

The below was written for the older 0.16 version, which did not have good documentation of cogs. The new 1.0 version has good documentation, and has completely changed the structure of cogs. If you're using a modern version of , you should consult the official documentation.

Introduction

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))

where Cog is the cog class.

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

Main Changes

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

  1. Replace bot.command with commands.command (commands being from discord.ext import commands)

  2. 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

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

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

There are also some gotchas:

  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.

  2. 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.

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")

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))

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")

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

Other features

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

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

  2. __local_check runs only before commands from this cog.

  3. __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.

  4. __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.

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

  6. __error is an error handler for commands from this cog.

这篇关于如何与discord.py一起使用嵌齿轮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆