mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-21 18:27:59 -05:00
Use aware objects when storing and reading UTC timestamps (#4017)
* Use aware objects instead of naive ones * Use aware objects when storing and reading UTC timestamps * Remove unneeded parentheses * Fixed naive and aware objects unable to be compared here * Address feedback * Fix the newly added `modlog.create_case()` calls Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import List, Literal, Union, Optional, cast, TYPE_CHECKING
|
||||
|
||||
import discord
|
||||
@@ -127,7 +127,8 @@ async def _init(bot: Red):
|
||||
if entry:
|
||||
if entry.user.id != guild.me.id:
|
||||
# Don't create modlog entires for the bot's own bans, cogs do this.
|
||||
mod, reason, date = entry.user, entry.reason, entry.created_at
|
||||
mod, reason = entry.user, entry.reason
|
||||
date = entry.created_at.replace(tzinfo=timezone.utc)
|
||||
await create_case(_bot_ref, guild, date, "ban", member, mod, reason)
|
||||
return
|
||||
|
||||
@@ -163,7 +164,8 @@ async def _init(bot: Red):
|
||||
if entry:
|
||||
if entry.user.id != guild.me.id:
|
||||
# Don't create modlog entires for the bot's own unbans, cogs do this.
|
||||
mod, reason, date = entry.user, entry.reason, entry.created_at
|
||||
mod, reason = entry.user, entry.reason
|
||||
date = entry.created_at.replace(tzinfo=timezone.utc)
|
||||
await create_case(_bot_ref, guild, date, "unban", user, mod, reason)
|
||||
return
|
||||
|
||||
@@ -351,9 +353,9 @@ class Case:
|
||||
until = None
|
||||
duration = None
|
||||
if self.until:
|
||||
start = datetime.fromtimestamp(self.created_at)
|
||||
end = datetime.fromtimestamp(self.until)
|
||||
end_fmt = end.strftime("%Y-%m-%d %H:%M:%S")
|
||||
start = datetime.utcfromtimestamp(self.created_at)
|
||||
end = datetime.utcfromtimestamp(self.until)
|
||||
end_fmt = end.strftime("%Y-%m-%d %H:%M:%S UTC")
|
||||
duration = end - start
|
||||
dur_fmt = _strfdelta(duration)
|
||||
until = end_fmt
|
||||
@@ -374,7 +376,7 @@ class Case:
|
||||
last_modified = None
|
||||
if self.modified_at:
|
||||
last_modified = "{}".format(
|
||||
datetime.fromtimestamp(self.modified_at).strftime("%Y-%m-%d %H:%M:%S")
|
||||
datetime.utcfromtimestamp(self.modified_at).strftime("%Y-%m-%d %H:%M:%S UTC")
|
||||
)
|
||||
|
||||
if isinstance(self.user, int):
|
||||
@@ -413,7 +415,7 @@ class Case:
|
||||
emb.add_field(name=_("Amended by"), value=amended_by)
|
||||
if last_modified:
|
||||
emb.add_field(name=_("Last modified at"), value=last_modified)
|
||||
emb.timestamp = datetime.fromtimestamp(self.created_at)
|
||||
emb.timestamp = datetime.utcfromtimestamp(self.created_at)
|
||||
return emb
|
||||
else:
|
||||
user = filter_mass_mentions(filter_urls(user)) # Further sanitization outside embeds
|
||||
|
||||
Reference in New Issue
Block a user