1

Hello I'm making a discord.py snippets that should have multiple discord.py bots instance.

def launch_bot(bot_instance: Bot):
    asyncio.run(bot_instance.run())

async def main():
    global global_bots_queue
    global global_bots

    guild_name = input('Enter the desired guild name: ')
    global_bots_queue = retrieve_tokens()

    for token in global_bots_queue:
        global_bots[token] = Bot(token)

    for token in global_bots_queue:
        p = multiprocessing.Process(target=launch_bot, args=(global_bots[token],))
        p.start()


if __name__ == '__main__':
    event_loop = asyncio.get_event_loop()
    event_loop.run_until_complete(main())

The tokens are stored in a file, that retrieve_tokens() get. A bot is represented as Bot class :

class Bot(commands.Bot):
    client = discord.Client()

    def __init__(self, token):
        intents = discord.Intents(messages=True, members=True, guilds=True)
        super().__init__(command_prefix="!", description="A bot for the Discord server", intents=intents)
        self.token = token

        print("Bot is starting...")

    @client.event
    async def on_ready(self):
        print(f"{self.client.user} has connected to Discord!")
        await self.run()

    async def run(self):
        print("Bot is running...")
        try:
            await self.client.run(self.token)
        except Exception as e:
            print(e)

But I have this issue : TypeError: cannot pickle 'Context' object, and I don't really know how to fix it.

I'd like to know where's the issue and how I can fix it please, and if there are some unexpected thing that I could remove/edit.

Thanks in advance !

  • 1
    Does this answer your question? [Python multiprocessing PicklingError: Can't pickle ](https://stackoverflow.com/questions/8804830/python-multiprocessing-picklingerror-cant-pickle-type-function) second answer – Ibrahim Jan 15 '22 at 13:05
  • Looks like, thanks! Bot is starting... Bot is starting... Bot is starting... Process finished with exit code 0 I'm getting that rn, why is the process finishing ? – Alexis C. Gridel Jan 15 '22 at 13:17
  • does the the bot even run ie the `on_ready` event trigger?Also delete your first three comments and embed the error in your question – Ibrahim Jan 15 '22 at 13:25
  • There is no any error, the on_ready seems not to trigger, otherwise it should prints ... has connected to Discord – Alexis C. Gridel Jan 15 '22 at 13:30
  • remove the `client = dis....` from the class and replace `@client.event` with `self` and same in `run` replace `self.client` to just `self` – Ibrahim Jan 15 '22 at 13:42
  • Okay but what should I replace into ? – Alexis C. Gridel Jan 15 '22 at 13:47
  • Okay I will in 30 minutes, thanks! – Alexis C. Gridel Jan 15 '22 at 13:56

0 Answers0