aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/decorators.py
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-30 19:48:00 +0200
committerGravatar kwzrd <[email protected]>2020-03-30 19:48:00 +0200
commit0e117b45121a581204ecd845cfabf2696783a602 (patch)
tree24c6b3302c821ae1cffa9e8ca667e750ea94d7bd /bot/utils/decorators.py
parentDeseasonify: remove `autostart` env var (diff)
Deseasonify: move current month resolver to utils
The function is useful to other modules as well - not only decorators. This declares it as public and moves it to a more accessible place.
Diffstat (limited to 'bot/utils/decorators.py')
-rw-r--r--bot/utils/decorators.py22
1 files changed, 4 insertions, 18 deletions
diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py
index a37601be..1e0a1715 100644
--- a/bot/utils/decorators.py
+++ b/bot/utils/decorators.py
@@ -4,7 +4,6 @@ import logging
import random
import typing as t
from asyncio import Lock
-from datetime import datetime
from functools import wraps
from weakref import WeakValueDictionary
@@ -13,6 +12,7 @@ from discord.ext import commands
from discord.ext.commands import CheckFailure, Command, Context
from bot.constants import Client, ERROR_REPLIES, Month
+from bot.utils import resolve_current_month
ONE_DAY = 24 * 60 * 60
@@ -31,20 +31,6 @@ class InMonthCheckFailure(CheckFailure):
pass
-def _resolve_current_month() -> Month:
- """
- Helper for local decorators to determine the correct Month value.
-
- This interfaces with the `MONTH_OVERRIDE` env var. If tha variable was set,
- current month always resolves to this value. Otherwise, the current utc month
- is given.
- """
- if Client.month_override is not None:
- return Month(Client.month_override)
- else:
- return Month(datetime.utcnow().month)
-
-
def seasonal_task(*allowed_months: Month, sleep_time: t.Union[float, int] = ONE_DAY) -> t.Callable:
"""
Perform the decorated method periodically in `allowed_months`.
@@ -64,7 +50,7 @@ def seasonal_task(*allowed_months: Month, sleep_time: t.Union[float, int] = ONE_
log.info(f"Starting seasonal task {task_body.__qualname__} ({allowed_months})")
while True:
- current_month = _resolve_current_month()
+ current_month = resolve_current_month()
if current_month in allowed_months:
await task_body(*args, **kwargs)
@@ -86,7 +72,7 @@ def in_month_listener(*allowed_months: Month) -> t.Callable:
@functools.wraps(listener)
async def guarded_listener(*args, **kwargs) -> None:
"""Wrapped listener will abort if not in allowed month."""
- current_month = _resolve_current_month()
+ current_month = resolve_current_month()
if current_month in allowed_months:
# Propagate return value although it should always be None
@@ -104,7 +90,7 @@ def in_month_command(*allowed_months: Month) -> t.Callable:
Uses the current UTC month at the time of running the predicate.
"""
async def predicate(ctx: Context) -> bool:
- current_month = _resolve_current_month()
+ current_month = resolve_current_month()
can_run = current_month in allowed_months
human_months = ", ".join(m.name for m in allowed_months)