peanut
peanut

Reputation: 41

discord.py ways eval(string)

This is my code:

import discord
from discord.ext import commands

class Compile(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name = 'say')
    async def SayCommand(self, ctx, query):
        cmd = await ctx.send(eval(query))
    
def setup(bot):
    bot.add_cog(Compile(bot))

I'm trying to get my bot to respond depending on the python I enter with the t.say command. E.g. t.say print('hello') would result in the bot returning Hello.

However, I'm running into this error:

SyntaxError: unexpected EOF while parsing (<string>, line 1)

Upvotes: 1

Views: 283

Answers (1)

Kelo
Kelo

Reputation: 1893

eval() is strictly to be used for expressions, not statements. To make your code work, use exec()

E.g.

@commands.command(name = 'say')
    async def SayCommand(self, ctx, query):
        cmd = await ctx.send(exec(query))

I'm assuming that you're doing this to allow for other python code to be ran. If not, I suggest you just use a command which ctx.send(query) back, and skip using python code in the command call.

Upvotes: 1

Related Questions