Using discord.py , I can run several bots from one piece of code, but I'm looking for a way to load a six or extension into several bots. For the test case, I have bot.py that handles loading cog and launching the bot, and cog.py is a simple cog that incrementally adds 1 to the 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))
Using !ping , you will reply client1 to Pong, and when using ~ping reply client2 to Pong, the expected behavior.
However, only one of the bots will respond to both !add and ~add , while the counter is incremented using any command. It seems to depend on which bot loads the latest cog.
Is there a way for the correct bot to respond to the correct command while increasing the counter with any command? I know that I can split it into two cogs and save the result in a file, for example, but can this be done without saving the counter to disk?
source share