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

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

    <tfoot id='EnxrR'></tfoot>
    <i id='EnxrR'><tr id='EnxrR'><dt id='EnxrR'><q id='EnxrR'><span id='EnxrR'><b id='EnxrR'><form id='EnxrR'><ins id='EnxrR'></ins><ul id='EnxrR'></ul><sub id='EnxrR'></sub></form><legend id='EnxrR'></legend><bdo id='EnxrR'><pre id='EnxrR'><center id='EnxrR'></center></pre></bdo></b><th id='EnxrR'></th></span></q></dt></tr></i><div id='EnxrR'><tfoot id='EnxrR'></tfoot><dl id='EnxrR'><fieldset id='EnxrR'></fieldset></dl></div>
    <legend id='EnxrR'><style id='EnxrR'><dir id='EnxrR'><q id='EnxrR'></q></dir></style></legend>
    1. 为多个机器人加载 cog

      时间:2023-10-11

    2. <legend id='fcfzQ'><style id='fcfzQ'><dir id='fcfzQ'><q id='fcfzQ'></q></dir></style></legend>
        <bdo id='fcfzQ'></bdo><ul id='fcfzQ'></ul>

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

            <tfoot id='fcfzQ'></tfoot>
              <tbody id='fcfzQ'></tbody>

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

              1. 本文介绍了为多个机器人加载 cog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                使用 discord.py,我可以从一段代码运行多个机器人,但我正在寻找一种将 cog 或扩展加载到多个机器人中的方法.对于一个测试用例,我有 bot.py,它处理加载 cog 和启动 bot,以及 cog.py,它是一个简单的 cog,它递增 1 到 a计数器

                Using discord.py, I can run multiple bots from one piece of code, but I'm looking for a way to load a cog or extension into multiple bots. For a test case, I have bot.py, which handles loading the cog and starting the bot, and cog.py which is a simple cog that incrementally adds 1 to a counter

                bot.py

                from discord.ext import commands
                import asyncio
                
                client1 = commands.Bot(command_prefix='!')
                client2 = commands.Bot(command_prefix='~')
                
                client1.load_extension('cog')
                client2.load_extension('cog')
                
                @client1.event
                async def on_ready():
                    print('client1 ready')
                
                @client1.command()
                async def ping():
                    await client1.say('Pong')
                
                @client2.event
                async def on_ready():
                    print('client2 ready')
                
                @client2.command()
                async def ping():
                    await client2.say('Pong')
                
                loop = asyncio.get_event_loop()
                loop.create_task(client1.start('TOKEN1'))
                loop.create_task(client2.start('TOKEN2'))
                loop.run_forever()
                

                cog.py

                from discord.ext import commands
                
                class TestCog:
                
                    def __init__(self, bot):
                        self.bot = bot
                        self.counter = 0
                
                    @commands.command()
                    async def add(self):
                        self.counter += 1
                        await self.bot.say('Counter is now %d' % self.counter)
                
                
                def setup(bot):
                    bot.add_cog(TestCog(bot))
                

                使用 !ping 将使 client1 响应 Pong,而使用 ~ping 将使 client2 响应Pong,这是预期的行为.

                Using !ping will make client1 respond with Pong, while using ~ping will make client2 respond with Pong, which is expected behaviour.

                但是,只有一个机器人会同时响应 !add~add,计数器会随着任一命令而增加.这似乎取决于哪个机器人最后加载 cog.

                However, only one of the bots will respond to both !add and ~add, with the counter increasing with either command. This seems dependent on which bot loads the cog last.

                有没有办法让正确的机器人响应正确的命令,同时通过任一命令增加计数器?我知道我可以将它分成两个齿轮并将结果保存到一个文件中,但是是否可以在不将计数器保存到磁盘的情况下做到这一点?

                Is there a way to have the correct bot respond to the correct command while also having the counter increase with either command? I know I can split it into two cogs and save the result to a file for example, but is it possible to do it without saving the counter to disk?

                推荐答案

                这是因为 @commands.command() 只加载了一次.因此,两个机器人共享相同的 Command 实例.您需要在实例级别添加命令,而不是通过 @commands.command() 装饰器.

                This is due to the fact that @commands.command() is only loaded once. Therefore, both of the bots shared the same Command instance. What you need is to add the command on an instance level, and not by the @commands.command() decorator.

                class TestCog:
                    counter = 0
                
                    def __init__(self, bot):
                        self.bot = bot
                        self.bot.add_command(commands.Command('add', self.add))
                
                    async def add(self):
                        TestCog.counter += 1
                        await self.bot.say('Counter is now %d' % TestCog.counter)
                

                或:

                class TestCog:
                    counter = 0
                
                    def __init__(self, bot):
                        self.bot = bot
                        self.bot.command()(self.add)
                
                    async def add(self):
                        TestCog.counter += 1
                        await self.bot.say('Counter is now %d' % TestCog.counter)
                

                为了让两个机器人共享相同的属性.你想要的是类属性,而不是实例的.

                In order to make both bots share the same attribute. You want class attribute, not instance's.

                这篇关于为多个机器人加载 cog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Discord.py:如何修复“事件循环已关闭"; 下一篇:Discord.py - 使用命令更改前缀

                相关文章

                最新文章

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

              2. <tfoot id='iWYpL'></tfoot>

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

                    • <bdo id='iWYpL'></bdo><ul id='iWYpL'></ul>

                    <legend id='iWYpL'><style id='iWYpL'><dir id='iWYpL'><q id='iWYpL'></q></dir></style></legend>