I'm trying to make discord bot execute python code line by line similar to python interactive shell
Everything works fine but I have some problems with loops
Instead of printing i
one by one, and waiting second between them, it print 0 to 9 instantly
There is a simplified example:
if 'q' in message.content:
redirected_output = sys.stdout = StringIO()
exec('for i in range(10):\n print(i)\n time.sleep(1)')
if redirected_output.getvalue():
await message.channel.send(f'`{redirected_output.getvalue()}`')
Attempt using asyncio.sleep
inside of async exec
if 'q' in message.content:
redirected_output = sys.stdout = StringIO()
await aexec('for i in range(10):\n print(i)\n await asyncio.sleep(1)')
if redirected_output.getvalue():
await message.channel.send(f'`{redirected_output.getvalue()}`')
Result is same, it print from 0 to 9 instantly