<small id='suaTg'></small><noframes id='suaTg'>

    1. <i id='suaTg'><tr id='suaTg'><dt id='suaTg'><q id='suaTg'><span id='suaTg'><b id='suaTg'><form id='suaTg'><ins id='suaTg'></ins><ul id='suaTg'></ul><sub id='suaTg'></sub></form><legend id='suaTg'></legend><bdo id='suaTg'><pre id='suaTg'><center id='suaTg'></center></pre></bdo></b><th id='suaTg'></th></span></q></dt></tr></i><div id='suaTg'><tfoot id='suaTg'></tfoot><dl id='suaTg'><fieldset id='suaTg'></fieldset></dl></div>
      • <bdo id='suaTg'></bdo><ul id='suaTg'></ul>
    2. <legend id='suaTg'><style id='suaTg'><dir id='suaTg'><q id='suaTg'></q></dir></style></legend>

        <tfoot id='suaTg'></tfoot>

        使用不带 cogs 的 discord.py 是否可以实现 OOP?

        时间:2023-10-11

          <legend id='TvsQX'><style id='TvsQX'><dir id='TvsQX'><q id='TvsQX'></q></dir></style></legend>
          <tfoot id='TvsQX'></tfoot>

            1. <small id='TvsQX'></small><noframes id='TvsQX'>

            2. <i id='TvsQX'><tr id='TvsQX'><dt id='TvsQX'><q id='TvsQX'><span id='TvsQX'><b id='TvsQX'><form id='TvsQX'><ins id='TvsQX'></ins><ul id='TvsQX'></ul><sub id='TvsQX'></sub></form><legend id='TvsQX'></legend><bdo id='TvsQX'><pre id='TvsQX'><center id='TvsQX'></center></pre></bdo></b><th id='TvsQX'></th></span></q></dt></tr></i><div id='TvsQX'><tfoot id='TvsQX'></tfoot><dl id='TvsQX'><fieldset id='TvsQX'></fieldset></dl></div>
              • <bdo id='TvsQX'></bdo><ul id='TvsQX'></ul>
                    <tbody id='TvsQX'></tbody>
                  本文介绍了使用不带 cogs 的 discord.py 是否可以实现 OOP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  最近几天,我一直在尝试将用 discord.py 编写的不和谐机器人的结构调整为更面向 OOP 的结构(因为周围有功能并不理想).

                  These last few days, I've been trying to adapt the structure of a discord bot written in discord.py to a more OOP one (because having functions lying around isn't ideal).

                  但我发现的问题比我想象的要多得多.问题是我想将所有命令封装到一个单个类中,但我不知道要使用哪些装饰器以及我必须继承哪些类以及如何继承.

                  But I have found way more problems that I could have ever expected. The thing is that I want to encapsulate all my commands into a single class, but I don't know what decorators to use and how and which classes I must inherit.

                  到目前为止,我所取得的成果是类似于下面的代码片段.它会运行,但在执行命令时会抛出类似

                  What I've achieved so far is code like the snippet down below. It runs, but at the moment of executing a command it throws errors like

                  discord.ext.commands.errors.CommandNotFound:命令状态"没找到

                  discord.ext.commands.errors.CommandNotFound: Command "status" is not found

                  我使用的是 Python 3.6.

                  I'm using Python 3.6.

                  from discord.ext import commands
                  
                  
                  class MyBot(commands.Bot):
                  
                      def __init__(self, command_prefix, self_bot):
                          commands.Bot.__init__(self, command_prefix=command_prefix, self_bot=self_bot)
                          self.message1 = "[INFO]: Bot now online"
                          self.message2 = "Bot still online {}"
                  
                      async def on_ready(self):
                          print(self.message1)
                  
                      @commands.command(name="status", pass_context=True)
                      async def status(self, ctx):
                          print(ctx)
                          await ctx.channel.send(self.message2 + ctx.author)
                  
                  
                  bot = MyBot(command_prefix="!", self_bot=False)
                  bot.run("token")
                  

                  推荐答案

                  要注册命令你应该使用self.add_command(setup),但是你不能有self<setup 方法中的/code> 参数,因此您可以执行以下操作:

                  To register the command you should use self.add_command(setup), but you can't have the self argument in the setup method, so you could do something like this:

                  from discord.ext import commands
                      
                  class MyBot(commands.Bot):
                      
                      def __init__(self, command_prefix, self_bot):
                          commands.Bot.__init__(self, command_prefix=command_prefix, self_bot=self_bot)
                          self.message1 = "[INFO]: Bot now online"
                          self.message2 = "Bot still online"
                          self.add_commands()
                      
                      async def on_ready(self):
                          print(self.message1)
                      
                      def add_commands(self):
                          @self.command(name="status", pass_context=True)
                          async def status(ctx):
                              print(ctx)
                              await ctx.channel.send(self.message2, ctx.author.name)
                          
                          self.add_command(status)
                      
                  bot = MyBot(command_prefix="!", self_bot=False)
                  bot.run("token")
                  

                  这篇关于使用不带 cogs 的 discord.py 是否可以实现 OOP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <legend id='D7z3h'><style id='D7z3h'><dir id='D7z3h'><q id='D7z3h'></q></dir></style></legend>
                  <i id='D7z3h'><tr id='D7z3h'><dt id='D7z3h'><q id='D7z3h'><span id='D7z3h'><b id='D7z3h'><form id='D7z3h'><ins id='D7z3h'></ins><ul id='D7z3h'></ul><sub id='D7z3h'></sub></form><legend id='D7z3h'></legend><bdo id='D7z3h'><pre id='D7z3h'><center id='D7z3h'></center></pre></bdo></b><th id='D7z3h'></th></span></q></dt></tr></i><div id='D7z3h'><tfoot id='D7z3h'></tfoot><dl id='D7z3h'><fieldset id='D7z3h'></fieldset></dl></div>

                  1. <tfoot id='D7z3h'></tfoot>
                          <tbody id='D7z3h'></tbody>

                          <bdo id='D7z3h'></bdo><ul id='D7z3h'></ul>

                        • <small id='D7z3h'></small><noframes id='D7z3h'>