Newline discord.py bot problem
You are piping stdout, which means the newline character (
\n) is replaced with an actual newline whitespace. Now imagine how this would print in discord. Reading
One way to solve this would be to write the code the user sent into a file, and then pipe the bash console.
import os # replace this with the username username = "dacoconutnut" # replace this with the code msg msg = ["print('lol1\nlol2')", "print('this is line 2')", "print('last line!')"] for i, line in enumerate(msg): # replace all newlines with escapped newline # then add a newline to end of the line msg[i] = line.replace("\n", "\\n") + "\n" # write it to the py file open(username + ".py", "w").writelines(msg) # read the result of bash "python filename.py" res = os.popen("python " + username + ".py").read() # print the result print("Result: \n" + str(res)) print("Raw Result String: \n" + repr(res))