简体   繁体   中英

SyntaxError (Python): 'await' outside async function

I am trying to write a bot for Discord but I keep getting an error message:

File "bot.py", line 33 await member.create_dm() ^ SyntaxError: 'await' outside async function

I am trying to get the bot to send a DM to a person who has just joined the server.

@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

for more information about async in python consider reading this: https://docs.python.org/3/library/asyncio-task.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM