sy1vi3
sy1vi3

Reputation: 181

Discord.py run 2 instances of a bot in one script

I have a discord bot that needs to run in a while(True) loop, as well as still receive commands. The only way I think I can do this that works with my program would be to have 2 separate instances of the bot running. Like saying:

client = commands.Bot(command_prefix = '!');       
client2= commands.Bot(command_prefix = '.');

and then at the bottom:

client.run('TOKEN')
client2.run('TOKEN')

I tried this, and it didn't work. What am I doing wrong? Is there a way to run 2 bots, or the same bot twice from one script?

Upvotes: 1

Views: 4279

Answers (1)

Benjin
Benjin

Reputation: 3495

You need to create your own event loop and run the bots with that, as client.run is blocking.

from discord.ext import commands
import asyncio

client = commands.Bot(command_prefix='!')
client2 = commands.Bot(command_prefix='.')

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(client.start('TOKEN1'))
loop.create_task(client2.start('TOKEN2'))
loop.run_forever()

Upvotes: 8

Related Questions