Reputation: 312
I'm trying to make a discord bot that accepts multi-line input, so that it can execute a python code. My code is the following:
@bot.command(name="python", help="executes python script")
async def python(ctx, *args):
try:
with Capturing() as output: #Capture output
exec(" ".join(args).replace('"', "'"))
# send captured output to thread
except Exception as e:
await ctx.send("```\n{}\n```".format(e)) # Send error message
The thing is that this function can only take one-line input like:
b!python a=1;print(a)
However, I want to make the discord.py bot take this type of message: An example of a complete message is the following:
b!python
a = 1
print(a)
I want to accept a multi-line input and assign it to a variable in my code and execute it:
code = """a = 1
print(a)
"""
exec(message)
I have seen some bots execute python code like that but I have no idea how to do it without using *args, in which case it only accepts one-line code. Is there a way to accept multi-line input?
Upvotes: 0
Views: 2841
Reputation: 61032
You can use the keyword-only argument syntax to indicate to discord.py
that you want to capture all of the rest of the input as a single argument:
@bot.command(name="python", help="executes python script")
async def python(ctx, *, arg):
try:
exec(arg)
except Exception as e:
await ctx.send("```\n{}\n```".format(e)) # Send error message
Upvotes: 2