我无法在 discord.py 中捕获来自 Wikipedia 的 PageError 异常
I can't catch PageError exception from Wikipedia in discord.py
所以,我正在为我的 discord.py 机器人制作维基百科 cog,如果按预期使用命令,一切正常,但是,如果我输入错误(例如“rb!resumen sjsajsajsja”) ,出现 PageError 异常并且代码停止。我已经尝试了很多东西来捕获异常并将其发送到通道或在终端中打印它,以便我的机器人可以保留 运行 但没有任何效果。
@commands.command()
async def resumen(self, ctx, name1, name2=None):
if name2 is not None:
try:
name = name1 + " " + name2
wiki_summary = wikipedia.summary(name, sentences=2)
wiki_page = wikipedia.page(name)
embed = discord.Embed(colour=discord.Colour.blue(),
timestamp=datetime.datetime.utcnow(),
title='Aquí está tu resumen:',
description=f'{wiki_summary}')
embed.add_field(name='Enlace', value=f'{wiki_page.url}',
inline=False)
embed.set_footer(text='Wikipedia de Robotito',
icon_url=f'{bot_icon}')
await ctx.send(embed=embed)
except wikipedia.exceptions.PageError as e:
print(e.args)
else:
try:
wiki_summary = wikipedia.summary(name1, sentences=2)
wiki_page = wikipedia.page(name1)
embed = discord.Embed(colour=discord.Colour.blue(),
timestamp=datetime.datetime.utcnow(),
title='Aquí está tu resumen:',
description=f'{wiki_summary}')
embed.add_field(name='Enlace', value=f'{wiki_page.url}',
inline=False)
embed.set_footer(text='Wikipedia de Robotito',
icon_url=f'{bot_icon}')
await ctx.send(embed=embed)
except wikipedia.exceptions.PageError as e:
print(e.args)
这是例外情况:
Exception has occurred: PageError
Page id "aeasjaosaopsja" does not match any pages. Try another id!
File "/home/ticiano/Documents/RoboTito/cog/wikiarts.py", line 65, in resumen
wiki_summary = wikipedia.summary(name1, sentences=2)
File "/home/ticiano/Documents/RoboTito/main_bot.py", line 51, in <module>
bot.run(token)
我设法“解决”了这个问题。我所说的错误出现在 VsCode 而不是终端中是因为终端不调试文件,只是运行它。我设法做的是在机器人中保留两个维基百科 API:Wikipedia 用于搜索页面和 Wikipedia-API 用于检索其信息。所以,是的,这不是解决方案,而是我为避免出现此错误而采取的方式。
所以,我正在为我的 discord.py 机器人制作维基百科 cog,如果按预期使用命令,一切正常,但是,如果我输入错误(例如“rb!resumen sjsajsajsja”) ,出现 PageError 异常并且代码停止。我已经尝试了很多东西来捕获异常并将其发送到通道或在终端中打印它,以便我的机器人可以保留 运行 但没有任何效果。
@commands.command()
async def resumen(self, ctx, name1, name2=None):
if name2 is not None:
try:
name = name1 + " " + name2
wiki_summary = wikipedia.summary(name, sentences=2)
wiki_page = wikipedia.page(name)
embed = discord.Embed(colour=discord.Colour.blue(),
timestamp=datetime.datetime.utcnow(),
title='Aquí está tu resumen:',
description=f'{wiki_summary}')
embed.add_field(name='Enlace', value=f'{wiki_page.url}',
inline=False)
embed.set_footer(text='Wikipedia de Robotito',
icon_url=f'{bot_icon}')
await ctx.send(embed=embed)
except wikipedia.exceptions.PageError as e:
print(e.args)
else:
try:
wiki_summary = wikipedia.summary(name1, sentences=2)
wiki_page = wikipedia.page(name1)
embed = discord.Embed(colour=discord.Colour.blue(),
timestamp=datetime.datetime.utcnow(),
title='Aquí está tu resumen:',
description=f'{wiki_summary}')
embed.add_field(name='Enlace', value=f'{wiki_page.url}',
inline=False)
embed.set_footer(text='Wikipedia de Robotito',
icon_url=f'{bot_icon}')
await ctx.send(embed=embed)
except wikipedia.exceptions.PageError as e:
print(e.args)
这是例外情况:
Exception has occurred: PageError
Page id "aeasjaosaopsja" does not match any pages. Try another id!
File "/home/ticiano/Documents/RoboTito/cog/wikiarts.py", line 65, in resumen
wiki_summary = wikipedia.summary(name1, sentences=2)
File "/home/ticiano/Documents/RoboTito/main_bot.py", line 51, in <module>
bot.run(token)
我设法“解决”了这个问题。我所说的错误出现在 VsCode 而不是终端中是因为终端不调试文件,只是运行它。我设法做的是在机器人中保留两个维基百科 API:Wikipedia 用于搜索页面和 Wikipedia-API 用于检索其信息。所以,是的,这不是解决方案,而是我为避免出现此错误而采取的方式。