【发布时间】:2019-12-31 22:29:22
【问题描述】:
我在 discord.py 中为我的 Discord 机器人创建了一个 embed_test 命令,并且我正在尝试使用 try ... except 子句让机器人说“无效的 url”,如果给定的图像 URL 无效:
@commands.command()
async def embed_test(sefl, ctx, *, image_url):
embed = discord.Embed(title = 'Very nice title', description = 'Interesting description', color = 0x00A900)
if (image_url[0], image_url[-1]) == ('"', '"'):
try: embed.set_image(url = image_url[1:])
except Exception: await ctx.send('invalid image url')
else: await ctx.send('please enter the url in "quotation marks"')
await ctx.send(embed = embed)
当子句不在 if ... else 语句中时,这非常有效,但是,如图所示,我还想确保用户在引号中输入他们的 URL。问题是,当我使用引号中的无效 URL 运行命令时,我得到:
Command raised an exception: HTTPException: 400 BAD REQUEST (error code: 50035): Invalid Form Body
In embed.image.url: Not a well formed URL.
在我的控制台中,即使我试图在我的程序中捕获异常。
【问题讨论】:
-
您确实应该在此处添加代码而不是 png 链接。这样人们就可以试一试。
标签: python exception bots discord.py