【发布时间】:2019-09-17 02:47:30
【问题描述】:
我正在编写一个 Discord 机器人,但在测试异步函数时遇到了问题。我想用exec()来测试它,但我似乎无法正确调用该函数。
我尝试了 exec()-ing 函数,无论是否有等待。我查看了API docs,但这并没有让我对我的问题有太多的了解。使用eval(),返回协程对象,但不执行。
exec() 是通过使用异步函数处理消息来完成的
async def f(message)
#other stuff
...
...
exec(strip2(message.content, "exec"))
return #exec doesn't return anything, so we return to not send an empty message
异步函数看起来像这样:
async def move_message(message_id, old_channel, new_channel):
"""
check the 20 latest messages in old_channel, and if
one of them matches the id, move it to new_channel
"""
print("ok")
async for message in old_channel.history(limit=20):
#do stuff
...
print("good!")
没有等待,它会给出这个错误:...\commands.py:1: RuntimeWarning: coroutine 'move_message' was never awaited
随着等待,它给了我一个SyntaxError:
File "<string>", line 1
await move_message(message, message.channel, "admin-test-playground")
^
SyntaxError: invalid syntax
我希望函数能够正确执行,至少打印一些东西。但既不是“好”也不是“好!”用我现在拥有的东西打印出来。
【问题讨论】:
标签: python python-3.x discord.py