aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-08-16 11:06:47 -0700
committerGravatar MarkKoz <[email protected]>2020-08-16 11:12:17 -0700
commitbebe09b74b24e140581cb317f300d2bbad8999df (patch)
tree8d6b0e1e93cf32cbfba6b4ad1b8ab262fd23d409
parentSilence: reschedule silences on startup (diff)
Silence: use aware datetimes
`datetime.timestamp()` assumes naïve `datetime`s are in local time, so getting POSIX timestamps in UTC isn't easy for naïve ones. Technically, the timestamp's timezone doesn't matter if all code is on the same page and parsing it with the same timezone. Keeping it in the local timezone would be okay then, but I feel safer locking it to UTC explicitly.
-rw-r--r--bot/cogs/moderation/silence.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py
index 02d8de29e..adf469661 100644
--- a/bot/cogs/moderation/silence.py
+++ b/bot/cogs/moderation/silence.py
@@ -2,7 +2,7 @@ import asyncio
import json
import logging
from contextlib import suppress
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
from typing import Optional
from discord import TextChannel
@@ -111,7 +111,7 @@ class Silence(commands.Cog):
await ctx.send(f"{Emojis.check_mark} silenced current channel for {duration} minute(s).")
self.scheduler.schedule_later(duration * 60, ctx.channel.id, ctx.invoke(self.unsilence))
- unsilence_time = (datetime.utcnow() + timedelta(minutes=duration))
+ unsilence_time = (datetime.now(tz=timezone.utc) + timedelta(minutes=duration))
await self.muted_channel_times.set(ctx.channel.id, unsilence_time.timestamp())
@commands.command(aliases=("unhush",))
@@ -218,12 +218,13 @@ class Silence(commands.Cog):
self.notifier.add_channel(channel)
continue
- dt = datetime.utcfromtimestamp(timestamp)
- if dt <= datetime.utcnow():
+ dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
+ delta = (dt - datetime.now(tz=timezone.utc)).total_seconds()
+ if delta <= 0:
await self._unsilence_wrapper(channel)
else:
log.info(f"Rescheduling silence for #{channel} ({channel.id}).")
- self.scheduler.schedule_at(dt, channel_id, self._unsilence_wrapper(channel))
+ self.scheduler.schedule_later(delta, channel_id, self._unsilence_wrapper(channel))
def cog_unload(self) -> None:
"""Cancel scheduled tasks."""