简体   繁体   中英

discord.py SyntaxError: 'await' outside async function

I got an error in the whole code below. I would like to seek help with the error. Can I ask for help by looking at the code below?

async def ban(ctx, member: discord.Member, *, reason: typing.Optional[str] = "사유 없음."):
    await ctx.message.delete()
    author = ctx.message.author
    embed = None
    ch = bot.get_channel(id=772349649553850368)

    mesge = await ctx.send("차단을 시킬까요?")
    await mesge.add_reaction('✅')
    await mesge.add_reaction('❌')
        
    def check1(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) == "✅"

        try:
            reaction, user = await bot.wait_for("reaction_add", timeout = 30.0, check = check1)
            embed = discord.Embed(title="종합게임 커뮤니티 제재내역 - 차단", description=f'담당자 : {author.mention} \n대상자 : {member.mention} \n제재사유 : {reason} \n\n위와 같은 사유로 인해 제재처리 되었습니다.', color=0xff0000)
            embed.set_author(name=f"{str(member)}님을 서버에서 영구적으로 차단했어요.", icon_url=member.avatar_url_as(static_format='png', size=2048))
            embed.set_footer(text=f'처리 시각 - {str(now.year)} 년 {str(now.month)} 월 {str(now.day)} 일 | {str(now.hour)} 시 {str(now.minute)} 분 {str(now.second)}초 - 담당자 : {author.display_name}')
            await ch.send(embed=embed)
            await member.send(embed=embed)
            await ctx.guild.ban(member, reason=f'사유 : {reason}  -  담당자 : {author.display_name}')
    
        except asyncio.TimeoutError:
            print("Timeout")
    
    def check2(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) == "❌"

        try:
            reaction, user = await bot.wait_for("reaction_add", timeout = 30.0, check = check2)
            await ctx.send("취소되었다")
        
        except asyncio.TimeoutError:
            print("Timeout")

The following error appears in the above code.

reaction, user = await bot.wait_for("reaction_add", timeout = 30.0, check = check1)
                     ^
SyntaxError: 'await' outside async function

If you know how to fix it, please help.

I used a translator.

Python uses identation to identify code blocks. In your code, you placed the await call inside of the non-async function check1 . Here is an example of the same problem:

async def foo():

    def check1():
        return True
        
        baz = await bar() # improperly indented and in fact can never
                          # run because it is after the function `return`

The fix is to move the code outside of check1 . It should align with the "def" statement above.

async def foo():

    def check1():
        return True
        
    baz = await bar()

Your issue is with indentation after both checks.

I have added # ---- here ---- So that you know where to end the check

async def ban(ctx, member: discord.Member, *, reason: typing.Optional[str] = "사유 없음."):
    await ctx.message.delete()
    author = ctx.message.author
    embed = None
    ch = bot.get_channel(id=772349649553850368)

    mesge = await ctx.send("차단을 시킬까요?")
    await mesge.add_reaction('✅')
    await mesge.add_reaction('❌')
        
    def check1(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) == "✅"
    
    # ---- here ----
    
    try:
        reaction, user = await bot.wait_for("reaction_add", timeout = 30.0, check = check1)
        embed = discord.Embed(title="종합게임 커뮤니티 제재내역 - 차단", description=f'담당자 : {author.mention} \n대상자 : {member.mention} \n제재사유 : {reason} \n\n위와 같은 사유로 인해 제재처리 되었습니다.', color=0xff0000)
        embed.set_author(name=f"{str(member)}님을 서버에서 영구적으로 차단했어요.", icon_url=member.avatar_url_as(static_format='png', size=2048))
        embed.set_footer(text=f'처리 시각 - {str(now.year)} 년 {str(now.month)} 월 {str(now.day)} 일 | {str(now.hour)} 시 {str(now.minute)} 분 {str(now.second)}초 - 담당자 : {author.display_name}')
        await ch.send(embed=embed)
        await member.send(embed=embed)
        await ctx.guild.ban(member, reason=f'사유 : {reason}  -  담당자 : {author.display_name}')

    except asyncio.TimeoutError:
        print("Timeout")
    
    def check2(reaction, user):
        return user == ctx.message.author and str(reaction.emoji) == "❌"
    # ---- here ----
    
    try:
        reaction, user = await bot.wait_for("reaction_add", timeout = 30.0, check = check2)
        await ctx.send("취소되었다")
    
    except asyncio.TimeoutError:
        print("Timeout")

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