r/pythontips • • Sep 10 '22

Module Multiple Discord Bots

Hello, I'm trying trying to learn how to develop bots in discord. So for fun I want to prank my friends by having it so that they join a discord, 7 waluigi bots jump in screaming "WAAAHHHHH" at them. Childish, but it has already taught me a decent chunk about the API. So far I have one bot and the code for its "followers" as I will call them. However when I try to call upon bot #2 from bot #1, I get "RuntimeError: asyncio.run() cannot be called from a running event loop". Does anyone know either a get around or a better way to approach this? I feel like multithreading is an option, but one I'd like to not explore at this point. I already have the extra tokens and bots created on discords side, its just calling them that is giving me issues.

I was wondering if anyone here has more experience with asyncronous programming, specifically in python and if there are any tricks I can use in native python that will help me solve this issue?

29 Upvotes

4 comments sorted by

5

u/HostileHarmony Sep 10 '22

Multi threading is… not very straightforward in python. Python (well really cPython), by default, is single threaded on a single CPU core (due to it’s built-in Global Interpreter Lock), so you have to “unlock” this feature. You may be looking for the multiprocessing library, which provides more insight and explains it’s APIs quite well.

Best of luck!

Alternatively, consider setting it up in such a way where you can spawn multiple instances of the script by running it several (7 in this case) times.

1

u/Spined_Serpent Sep 10 '22

I’m currently using a bash script to start 4 instances with start “name” script_name. My original goal was to have one bot start the other 6, so once I work on this project again I’m going to look into python running a bash script instead.

3

u/Nordrokar2 Sep 10 '22

I think you might also have the option of using os.startfile to execute a secondary script 7 times, but I’m a relative newbie so take my advice with a handful of salt.

1

u/Indiewood Sep 18 '22

You can probably put the tokens in a list, start a discord Client for token in tokens and run until complete