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:
DevilXD
2020-08-12 10:46:32 +02:00
committed by GitHub
parent 73a34eacd6
commit 6e63ed4e60
10 changed files with 63 additions and 59 deletions

View File

@@ -1,5 +1,6 @@
import discord
import re
from datetime import timezone
from typing import Union, Set, Literal
from redbot.core import checks, Config, modlog, commands
@@ -339,10 +340,11 @@ class Filter(commands.Cog):
filter_time = guild_data["filterban_time"]
user_count = member_data["filter_count"]
next_reset_time = member_data["next_reset_time"]
created_at = message.created_at.replace(tzinfo=timezone.utc)
if filter_count > 0 and filter_time > 0:
if message.created_at.timestamp() >= next_reset_time:
next_reset_time = message.created_at.timestamp() + filter_time
if created_at.timestamp() >= next_reset_time:
next_reset_time = created_at.timestamp() + filter_time
async with self.config.member(author).all() as member_data:
member_data["next_reset_time"] = next_reset_time
if user_count > 0:
@@ -361,10 +363,7 @@ class Filter(commands.Cog):
if filter_count > 0 and filter_time > 0:
user_count += 1
await self.config.member(author).filter_count.set(user_count)
if (
user_count >= filter_count
and message.created_at.timestamp() < next_reset_time
):
if user_count >= filter_count and created_at.timestamp() < next_reset_time:
reason = _("Autoban (too many filtered messages.)")
try:
await guild.ban(author, reason=reason)
@@ -374,7 +373,7 @@ class Filter(commands.Cog):
await modlog.create_case(
self.bot,
guild,
message.created_at,
message.created_at.replace(tzinfo=timezone.utc),
"filterban",
author,
guild.me,

View File

@@ -1,5 +1,5 @@
import logging
from datetime import datetime
from datetime import timezone
from collections import defaultdict, deque
import discord
@@ -60,7 +60,7 @@ class Events(MixinMeta):
await modlog.create_case(
self.bot,
guild,
message.created_at,
message.created_at.replace(tzinfo=timezone.utc),
"ban",
author,
guild.me,
@@ -84,7 +84,7 @@ class Events(MixinMeta):
await modlog.create_case(
self.bot,
guild,
message.created_at,
message.created_at.replace(tzinfo=timezone.utc),
"kick",
author,
guild.me,
@@ -116,7 +116,7 @@ class Events(MixinMeta):
await modlog.create_case(
self.bot,
guild,
message.created_at,
message.created_at.replace(tzinfo=timezone.utc),
"warning",
author,
guild.me,

View File

@@ -1,7 +1,7 @@
import asyncio
import contextlib
import logging
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Optional, Union
import discord
@@ -120,7 +120,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"ban",
user,
author,
@@ -142,10 +142,11 @@ class KickBanMixin(MixinMeta):
async with self.config.guild(guild).current_tempbans() as guild_tempbans:
for uid in guild_tempbans.copy():
unban_time = datetime.utcfromtimestamp(
await self.config.member_from_ids(guild.id, uid).banned_until()
unban_time = datetime.fromtimestamp(
await self.config.member_from_ids(guild.id, uid).banned_until(),
timezone.utc,
)
if datetime.utcnow() > unban_time: # Time to unban the user
if datetime.now(timezone.utc) > unban_time: # Time to unban the user
queue_entry = (guild.id, uid)
try:
await guild.unban(
@@ -228,7 +229,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"kick",
user,
author,
@@ -410,7 +411,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"hackban",
user_id,
author,
@@ -436,7 +437,7 @@ class KickBanMixin(MixinMeta):
"""Temporarily ban a user from this server."""
guild = ctx.guild
author = ctx.author
unban_time = datetime.utcnow() + duration
unban_time = datetime.now(timezone.utc) + duration
if author == user:
await ctx.send(
@@ -491,7 +492,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"tempban",
user,
author,
@@ -574,7 +575,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"softban",
user,
author,
@@ -621,7 +622,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"vkick",
member,
author,
@@ -660,7 +661,7 @@ class KickBanMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"unban",
user,
author,

View File

@@ -1,4 +1,5 @@
import asyncio
from datetime import timezone
from typing import cast, Optional
import discord
@@ -107,7 +108,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"voiceunban",
user,
author,
@@ -148,7 +149,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"voiceban",
user,
author,
@@ -188,7 +189,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"vmute",
user,
author,
@@ -232,7 +233,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"cmute",
user,
author,
@@ -262,7 +263,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"smute",
user,
author,
@@ -305,7 +306,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"vunmute",
user,
author,
@@ -349,7 +350,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"cunmute",
user,
author,
@@ -383,7 +384,7 @@ class MuteMixin(MixinMeta):
await modlog.create_case(
self.bot,
guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"sunmute",
user,
author,

View File

@@ -1,3 +1,4 @@
from datetime import timezone
from typing import Optional, Union
import discord
@@ -181,7 +182,7 @@ class ModLog(commands.Cog):
to_modify = {"reason": reason}
if case_obj.moderator != author:
to_modify["amended_by"] = author
to_modify["modified_at"] = ctx.message.created_at.timestamp()
to_modify["modified_at"] = ctx.message.created_at.replace(tzinfo=timezone.utc).timestamp()
await case_obj.edit(to_modify)
await ctx.send(
_("Reason for case #{num} has been updated.").format(num=case_obj.case_number)

View File

@@ -1,5 +1,6 @@
import asyncio
import contextlib
from datetime import timezone
from collections import namedtuple
from copy import copy
from typing import Union, Optional, Literal
@@ -494,7 +495,7 @@ class Warnings(commands.Cog):
await modlog.create_case(
self.bot,
ctx.guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"warning",
user,
ctx.message.author,
@@ -616,7 +617,7 @@ class Warnings(commands.Cog):
await modlog.create_case(
self.bot,
ctx.guild,
ctx.message.created_at,
ctx.message.created_at.replace(tzinfo=timezone.utc),
"unwarned",
member,
ctx.message.author,