From a126da5f23436c378ad0fbe06fadf1762c027a43 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Thu, 19 Mar 2020 08:19:01 +0100 Subject: [PATCH] Prevent PyPI's unavailability from causing command failure (#3663) --- redbot/core/core_commands.py | 23 ++++++++++++++++------- redbot/core/events.py | 3 ++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index 675e66fad..5ddff5569 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -327,10 +327,14 @@ class Core(commands.Cog, CoreLogic): owner = app_info.owner custom_info = await self.bot._config.custom_info() - async with aiohttp.ClientSession() as session: - async with session.get(red_pypi_json) as r: - data = await r.json() - outdated = VersionInfo.from_str(data["info"]["version"]) > red_version_info + try: + async with aiohttp.ClientSession() as session: + async with session.get(red_pypi_json) as r: + data = await r.json() + except (aiohttp.ClientError, asyncio.TimeoutError): + outdated = None + else: + outdated = VersionInfo.from_str(data["info"]["version"]) > red_version_info about = _( "This bot is an instance of [Red, an open source Discord bot]({}) " "created by [Twentysix]({}) and [improved by many]({}).\n\n" @@ -345,10 +349,15 @@ class Core(commands.Cog, CoreLogic): embed.add_field(name="Python", value=python_version) embed.add_field(name="discord.py", value=dpy_version) embed.add_field(name=_("Red version"), value=red_version) - if outdated: - embed.add_field( - name=_("Outdated"), value=_("Yes, {} is available").format(data["info"]["version"]) + if outdated is True: + outdated_value = _("Yes, {version} is available").format( + version=data["info"]["version"] ) + elif outdated is None: + outdated_value = _("Checking for updates failed.") + else: + outdated_value = _("No") + embed.add_field(name=_("Outdated"), value=outdated_value) if custom_info: embed.add_field(name=_("About this instance"), value=custom_info, inline=False) embed.add_field(name=_("About Red"), value=about, inline=False) diff --git a/redbot/core/events.py b/redbot/core/events.py index 5c8347504..5bfa9f894 100644 --- a/redbot/core/events.py +++ b/redbot/core/events.py @@ -1,3 +1,4 @@ +import asyncio import contextlib import sys import codecs @@ -84,7 +85,7 @@ def init_events(bot, cli_flags): INFO.append("{} cogs with {} commands".format(len(bot.cogs), len(bot.commands))) outdated_red_message = "" - with contextlib.suppress(aiohttp.ClientError, discord.HTTPException): + with contextlib.suppress(aiohttp.ClientError, asyncio.TimeoutError): async with aiohttp.ClientSession() as session: async with session.get("https://pypi.org/pypi/red-discordbot/json") as r: data = await r.json()