[英]SyntaxError (Python): 'await' outside async function
I am trying to write a bot for Discord but I keep getting an error message:我正在尝试为 Discord 编写一个机器人,但我不断收到错误消息:
File "bot.py", line 33 await member.create_dm() ^ SyntaxError: 'await' outside async function文件“bot.py”,第 33 行 await member.create_dm() ^ SyntaxError: 'await' 在异步函数之外
I am trying to get the bot to send a DM to a person who has just joined the server.我试图让机器人向刚加入服务器的人发送 DM。
@client.event
@asyncio.coroutine
def on_member_join(member):
await member.create_dm()
member.dm_channel.send("Hi and welcome!")
I would very much appreciate your help.我非常感谢您的帮助。
Your code should look like this:您的代码应如下所示:
@client.event
@asyncio.coroutine
async def on_member_join(member):
await member.create_dm()
member.dm_channel.send("Hi and welcome!")
add async
before def
在def
前添加async
for more information about async in python consider reading this: https://docs.python.org/3/library/asyncio-task.html有关 python 中异步的更多信息,请考虑阅读: https : //docs.python.org/3/library/asyncio-task.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.