0

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.

3
  • 1
    Have you tried changing def check1 to async def check1 and def check2 to async def check2? I haven't done much with async/await, but that is what I would try first as it seems to be what the error message is suggesting you do. Commented Nov 22, 2020 at 6:30
  • I think its identation. Notice that the try block is inside the function check1 ... you want to dedent that code so that it is in ban. Commented Nov 22, 2020 at 6:40
  • The same for check2. you put the following try inside the function when it should be dedented 4 spaces to the left so that it is part of the parent function. Commented Nov 22, 2020 at 6:43

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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")

3 Comments

neither check1 nor check2 need to be async. They are simple logic checks and do not block.
I see it now i thought the try was outside of it,
I was caught by that at first too. Puzzled about why you'd do a try block after a return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.