我正在尝试制作一个接受多行输入的discord bot,这样它就可以执行python代码。我的代码如下:
@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问题是这个函数只能接受一行输入,如下所示:
b!python a=1;print(a)但是,我想让discord.py机器人接受这种类型的消息:完整消息的示例如下:
b!python
a = 1
print(a)我希望接受多行输入,并将其赋给代码中的一个变量,然后执行它:
code = """a = 1
print(a)
"""
exec(message)我见过一些机器人执行这样的python代码,但我不知道如何在不使用*args的情况下这样做,在这种情况下,它只接受一行代码。有没有办法接受多行输入?
发布于 2020-06-22 12:06:23
您可以使用keyword-only argument syntax向discord.py指示您希望将所有其余输入捕获为单个参数:
@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 messagehttps://stackoverflow.com/questions/62506621
复制相似问题