except syntax erroring incorrectly discord py

Viewed 24

every time i run this code it says the syntax is wrong for except ^ but nothing in my code suggests why that shouldnt work i’ve found no solutions online my code was written exactly as i saw it writen on the tutorial i watched for this command

error: except asyncio.TimeoutError: ^ SyntaxError: invalid syntax

@bot.command(name="echo", description="command test")
async def echo(self, ctx):
    await ctx.message.delete()
    embed = discord.Embed(title="tell me wtf is up", description="timeout request")
    sent = await ctx.send(embed=embed)
    try:
        msg = await self.bot.wait_for("message", timeout=30, check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
        if msg:
            await sent.delete()
            await msg.delete()
            await ctx.send(msg.content)
        except asyncio.TimeoutError:
                await sent.delete()
                ctx.send("cancelling due to timeout", delete_after=10)
1 Answers

except needs to follow a corresponding try block. I.e., it needs to have the same indentation:

try:
    msg = await self.bot.wait_for("message", timeout=30, check=lambda message: message.author == ctx.author and message.channel == ctx.channel)
    if msg:
        await sent.delete()
        await msg.delete()
        await ctx.send(msg.content)
except asyncio.TimeoutError:
    await sent.delete()
    ctx.send("cancelling due to timeout", delete_after=10)
Related