From dfc1e742f88310f5ea6d3af4e08622d29e1216d7 Mon Sep 17 00:00:00 2001 From: Jakub Kuczys Date: Thu, 6 Feb 2025 01:42:42 +0100 Subject: [PATCH] Handle invalid locale configuration graciously during startup (#6517) --- redbot/core/_i18n.py | 3 ++- redbot/core/bot.py | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/redbot/core/_i18n.py b/redbot/core/_i18n.py index f2b5e7ec9..f1f445045 100644 --- a/redbot/core/_i18n.py +++ b/redbot/core/_i18n.py @@ -22,8 +22,9 @@ __all__ = ( ) +FRESH_INSTALL_LOCALE = "en-US" current_locale = ContextVar("current_locale") -current_locale_default = "en-US" +current_locale_default = FRESH_INSTALL_LOCALE current_regional_format = ContextVar("current_regional_format") current_regional_format_default = None diff --git a/redbot/core/bot.py b/redbot/core/bot.py index a1d1048a2..f322595bd 100644 --- a/redbot/core/bot.py +++ b/redbot/core/bot.py @@ -115,7 +115,7 @@ class Red( owner=None, whitelist=[], blacklist=[], - locale="en-US", + locale=_i18n.FRESH_INSTALL_LOCALE, regional_format=None, embeds=True, color=15158332, @@ -1145,9 +1145,28 @@ class Red( self.owner_ids.add(self._owner_id_overwrite) i18n_locale = await self._config.locale() - _i18n.set_global_locale(i18n_locale) + try: + _i18n.set_global_locale(i18n_locale) + except ValueError: + log.warning( + "The bot's global locale was set to an invalid value (%r)" + " and will be reset to default (%s).", + i18n_locale, + _i18n.FRESH_INSTALL_LOCALE, + ) + i18n_locale = _i18n.FRESH_INSTALL_LOCALE + await self._config.locale.clear() i18n_regional_format = await self._config.regional_format() - _i18n.set_global_regional_format(i18n_regional_format) + try: + _i18n.set_global_regional_format(i18n_regional_format) + except ValueError: + log.warning( + "The bot's global regional format was set to an invalid value (%r)" + " and will be reset to default (which is to inherit global locale, i.e. %s).", + i18n_regional_format, + i18n_locale, + ) + await self._config.regional_format.clear() async def _pre_connect(self) -> None: """