aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar mbaruh <[email protected]>2021-03-02 20:46:02 +0200
committerGravatar mbaruh <[email protected]>2021-03-02 20:46:02 +0200
commit91d9748b30bc36169b16405177582d2aa0fe233f (patch)
treedf095c9e7f02cb8ef722f460aefc3bb6f14b1f83
parentImproved docstring for threshold command. (diff)
parentProvide default cache values when syncing (diff)
Merge branch 'mbaruh/defcon' of https://github.com/python-discord/bot into mbaruh/defcon
-rw-r--r--bot/exts/moderation/defcon.py27
-rw-r--r--bot/utils/time.py4
2 files changed, 19 insertions, 12 deletions
diff --git a/bot/exts/moderation/defcon.py b/bot/exts/moderation/defcon.py
index 02302612f..bd16289b9 100644
--- a/bot/exts/moderation/defcon.py
+++ b/bot/exts/moderation/defcon.py
@@ -6,6 +6,7 @@ from datetime import datetime
from enum import Enum
from typing import Optional, Union
+from aioredis import RedisError
from async_rediscache import RedisCache
from dateutil.relativedelta import relativedelta
from discord import Colour, Embed, Member, User
@@ -84,9 +85,9 @@ class Defcon(Cog):
try:
settings = await self.defcon_settings.to_dict()
- self.threshold = parse_duration_string(settings["threshold"]) if settings["threshold"] else None
- self.expiry = datetime.fromisoformat(settings["expiry"]) if settings["expiry"] else None
- except Exception:
+ self.threshold = parse_duration_string(settings["threshold"]) if settings.get("threshold") else None
+ self.expiry = datetime.fromisoformat(settings["expiry"]) if settings.get("expiry") else None
+ except RedisError:
log.exception("Unable to get DEFCON settings!")
await self.channel.send(
f"<@&{Roles.moderators}> <@&{Roles.devops}> **WARNING**: Unable to get DEFCON settings!"
@@ -215,14 +216,20 @@ class Defcon(Cog):
if self.expiry is not None:
self.scheduler.schedule_at(expiry, 0, self._remove_threshold())
- await self.defcon_settings.update(
- {
- 'threshold': Defcon._stringify_relativedelta(self.threshold) if self.threshold else "",
- 'expiry': expiry.isoformat() if expiry else 0
- }
- )
self._update_notifier()
+ # Make sure to handle the critical part of the update before writing to Redis.
+ error = ""
+ try:
+ await self.defcon_settings.update(
+ {
+ 'threshold': Defcon._stringify_relativedelta(self.threshold) if self.threshold else "",
+ 'expiry': expiry.isoformat() if expiry else 0
+ }
+ )
+ except RedisError:
+ error = ", but failed to write to cache"
+
action = Action.DURATION_UPDATE
expiry_message = ""
@@ -238,7 +245,7 @@ class Defcon(Cog):
channel_message = "removed"
await self.channel.send(
- f"{action.value.emoji} DEFCON threshold {channel_message}."
+ f"{action.value.emoji} DEFCON threshold {channel_message}{error}."
)
await self._send_defcon_log(action, author)
self._update_channel_topic()
diff --git a/bot/utils/time.py b/bot/utils/time.py
index a7b441327..f862e40f7 100644
--- a/bot/utils/time.py
+++ b/bot/utils/time.py
@@ -9,7 +9,7 @@ from dateutil.relativedelta import relativedelta
RFC1123_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"
INFRACTION_FORMAT = "%Y-%m-%d %H:%M"
-_duration_parser = re.compile(
+_DURATION_REGEX = re.compile(
r"((?P<years>\d+?) ?(years|year|Y|y) ?)?"
r"((?P<months>\d+?) ?(months|month|m) ?)?"
r"((?P<weeks>\d+?) ?(weeks|week|W|w) ?)?"
@@ -100,7 +100,7 @@ def parse_duration_string(duration: str) -> Optional[relativedelta]:
The units need to be provided in descending order of magnitude.
If the string does represent a durationdelta object, it will return None.
"""
- match = _duration_parser.fullmatch(duration)
+ match = _DURATION_REGEX.fullmatch(duration)
if not match:
return None