Python 如何使discord.py命令能够执行ctx.send命令
现在,我有一个机器人,我希望能够用Python命令控制它。下面是我的>执行命令的代码,我想用它来执行Python代码段Python 如何使discord.py命令能够执行ctx.send命令,python,async-await,discord.py,Python,Async Await,Discord.py,现在,我有一个机器人,我希望能够用Python命令控制它。下面是我的>执行命令的代码,我想用它来执行Python代码段 async def execute(ctx, *, arg): if ctx.author.id == 645264167623983124: try: exec(arg.replace("```", "")) await ctx.send(embed=msg(tit
async def execute(ctx, *, arg):
if ctx.author.id == 645264167623983124:
try:
exec(arg.replace("```", ""))
await ctx.send(embed=msg(title="Execution complete!", desc="The code ran successfully."))
except Exception as e:
await ctx.send(embed=msg(title="Error", desc=str(e)))
else:
await ctx.send(embed=msg(title="Nuh Uh Uhh", desc="You are not allowed to use this command!"))
但是,我还想使用此命令发送测试消息,如下所示:
>execute await ctx.send("Hello world!")
当我运行这个时,它会说:
Error
'await' outside function (<string>, line 1)
错误
“等待”外部函数(,第1行)
但是当我在没有等待的情况下运行它时,它说它运行得很好,但是我运行机器人的控制台说:
<string>:1: RuntimeWarning: coroutine 'Messageable.send' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
:1:RuntimeWarning:coroutine“Messageable.send”从未等待过
RuntimeWarning:启用tracemalloc以获取对象分配回溯
我将其用于异步函数:
arg = arg.replace("```", "")
if arg.startswith("await"):
await eval(arg.replace("await ", ""))
else:
exec(arg)
您是否使用了
@client.command()
装饰程序?是的。看起来我没有包括在内,哎呀。