如何修复无效语法(<unknown>,第79行)

2024-05-01 21:45:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图发表一个不和谐的声明:

但是,我一直收到一个错误:the error

我试图完成的是制作一个公告机器人,这样当频道中的用户执行.announce <text>操作时,它会向特定频道发送一个嵌入,如下所示:https://prnt.sc/rgw4yc

async def announce(ctx, message : str):
    try:
        for chan in channels:
            try: 
                channel = bot.get_channel(channel id)
                info = discord.Embed(title="New Announcement!", description=str(message), color=0xFFFFFF)
                await channel.send(embed=info)

                try: 
                    except Exception as e:
                    await ctx.send(e)
                    await ctx.send("Error: " + str(chan))

                 except Exception as e:
                 await ctx.send(e) ```

Tags: infosendmessageasexceptionchannelawait频道
1条回答
网友
1楼 · 发布于 2024-05-01 21:45:35

基于error

错误出现在代码的以下部分

try: 
                    except Exception as e:
                    await ctx.send(e)
                    await ctx.send("Error: " + str(chan))

除了在try循环中,您不能使用。逻辑流程如下 如果失败,请尝试执行一个操作以引发异常,这样可以防止整个代码失败

范例

try:
       r=requests.get(some_url)
 except:
       print("Request Failed")

在上面的示例中,如果变量some_url有错误的URL或站点关闭,它将打印请求失败并继续执行其余代码

相关问题 更多 >