简体   繁体   English

discord.py 语法错误:异步函数外的“等待”

[英]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. Python 使用标识来标识代码块。 In your code, you placed the await call inside of the non-async function check1 .在您的代码中,您将await调用放在非异步函数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 .解决方法是将代码移到check1之外。 It should align with the "def" statement above.它应该与上面的“def”语句一致。

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我在# ---- here ----添加了# ---- here ----这样您就知道在哪里结束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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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