I tried executing
import ast
ast.literal_eval('5+5')
Then I got ValueError: malformed node or string on line 1: <ast.BinOp object at 0x70e6bd2830>
.
So then I tried another:
import ast
ast.literal_eval(str(5+5))
And it evaluates it successfully:
10
But when I finally used it on an actual command $math 5+5
@client.command()
async def math(ctx, expression):
resp = ast.literal_eval(str(expression))
await ctx.send(resp)
I still get the same error: ValueError: malformed node or string on line 1: <ast.BinOp object at 0x7d05357700>
for some reason. I'm using Python 3.10.5.
How can I solve this? Any help would be appreciated.
Whole code:
import discord, ast
from discord.ext import commands
TOKEN = ""
client = commands.Bot(command_prefix="$")
prefix = client.command_prefix
def init():
client.run(TOKEN, reconnect=False)
@client.event
async def on_connect():
print('Connecting to server...')
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle)
print('Logged in as ' + str(client.user))
print('Prefix: ' + str(prefix))
@client.command()
async def math(ctx, expression):
resp = ast.literal_eval(str(expression))
await ctx.send(resp)
init()