加载多个机器人的齿轮 [英] Load cog for multiple bots
问题描述
使用 discord.py
,我可以从一个代码中运行多个机器人,但是我正在寻找一种将cog或扩展名加载到多个机器人中的方法。对于一个测试用例,我有 bot.py
和 cog.py
这是一个简单的齿轮,会将1递增到计数器中
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
,其中任一命令都会增加计数器。
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 the @commands.command()
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.
这篇关于加载多个机器人的齿轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!