[Python] multiple instance of discord.py bots with multiprocessing issue with instances that...

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

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

    A bot is represented as follows

    class Bot(commands.Bot):

    client = discord.Client()

    def __init__(self, token):
    intents = discord.Intents(messages=True, members=True, guilds=True)
    self.client = commands.Bot(command_prefix="!", intents=intents)
    self.token = token

    print("Bot is starting...")

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

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


    But in console, I'm only getting Bot is starting... 3 times because I have 3 tokens

    The main bootstrapper code is like that

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


    async def main():
    global global_bots_queue
    global global_bots

    pool = multiprocessing.Pool()

    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:
    pool.apply_async(launch_bot, args=(global_bots[token],))

    pool.close()
    pool.join()


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


    What should I edit to make this code works, please?

    Output :

    Bot is starting...
    Bot is starting...
    Bot is starting...
    Process finished with exit code 0

    Continue reading...

Compartilhe esta Página