I have recently been building a discord bot that is, apart from being a small IDE, trains and teaches you Python, by asking you questions about it. But I am stuck on the IDE part. The problem is that the functions in Python used for executing code inside of code are terminal-based and don't return the code they evaluate. Here are some functions I've tried:
- Exec()
- Eval()
- Subprocess.call()
My code so far looks like this:
import discord
import os
from keep_alive import keep_alive
client = discord.Client()
global new_content
@client.event
async def on_ready():
print("I'm in")
@client.event
async def on_message(message):
global new_content
if message.author != client.user:
if message.content == "!help":
await message.channel.send(f'''**---- HELP OPTIONS ----**
Prefix: `!`
To train: `!train`
To start new IDE file: `!start`
To open an old IDE file: `!open`
To run an IDE file: `!run`''')
elif message.content == "!start":
new_content = ''
await message.channel.send("Write your Python code!! Leave RUN to end")
@client.event
async def on_message(message):
global new_content
if message.author != client.user:
if message.content == "RUN":
code = str(eval(new_content))
await message.channel.send(f'''**---- OUTPUT ----**
{code}''')
else:
print(message.content)
new_content = new_content + str(message.content)
keep_alive() #To keep bot running forever
token = os.environ.get("DISCORD_BOT_SECRET")
client.run(token)
But when I type in !start
and type up some code the result looks like this:
---- OUTPUT ----
None
I think this is because the functions don't return what they output/print(). Can anyone help?