So, i’m making an Wikipedia cog for my discord.py bot and everything is ok if the commands are used as expected, but, if i type something wrong (like “rb!resumen sjsajsajsja”), an PageError exception appears and the code stops. I have tried so many things to catch the exception and send it to the channel or print it in the terminal, so that my bot can keep running but nothing works.
@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)
This is the exception:
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)
Advertisement
Answer
I managed to “fix” this problem. The thing i was saying about that the error appears in VsCode and not in the terminal was because the terminal doesn’t debug the file, simply runs it. The thing i managed to do is to keep two Wikipedia API’s in the bot: Wikipedia for searching pages and Wikipedia-API for retrieving its information. So, yes, it’s not a solution but is the way i took for not having this error.