Skip to content

Commit 039347f

Browse files
committed
Use new timestamps for userinfo and fix potential blocked parsing
1 parent 3d7b626 commit 039347f

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

bot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "4.0.0-dev20"
1+
__version__ = "4.0.0"
22

33

44
import asyncio

cogs/modmail.py

+38-10
Original file line numberDiff line numberDiff line change
@@ -1654,21 +1654,48 @@ async def blocked(self, ctx):
16541654
blocked_users = list(self.bot.blocked_users.items())
16551655
for id_, reason in blocked_users:
16561656
# parse "reason" and check if block is expired
1657-
# etc "blah blah blah... until 2019-10-14T21:12:45.559948."
1658-
end_time = re.search(r"until ([^`]+?)\.$", reason)
1659-
if end_time is None:
1657+
# etc "blah blah blah... until <t:XX:f>."
1658+
end_time = re.search(r"until <t:(\d+):(?:R|f)>.$", reason)
1659+
attempts = [
16601660
# backwards compat
1661-
end_time = re.search(r"%([^%]+?)%", reason)
1661+
re.search(r"until ([^`]+?)\.$", reason),
1662+
re.search(r"%([^%]+?)%", reason),
1663+
]
1664+
if end_time is None:
1665+
for i in attempts:
1666+
if i is not None:
1667+
end_time = i
1668+
break
1669+
16621670
if end_time is not None:
1671+
# found a deprecated version
1672+
try:
1673+
after = (
1674+
datetime.fromisoformat(end_time.group(1)).replace(tzinfo=timezone.utc) - now
1675+
).total_seconds()
1676+
except ValueError:
1677+
logger.warning(
1678+
r"Broken block message for user %s, block and unblock again with a different message to prevent further issues",
1679+
id_,
1680+
)
1681+
continue
16631682
logger.warning(
16641683
r"Deprecated time message for user %s, block and unblock again to update.",
16651684
id_,
16661685
)
1686+
else:
1687+
try:
1688+
after = (
1689+
datetime.fromtimestamp(int(end_time.group(1))).replace(tzinfo=timezone.utc) - now
1690+
).total_seconds()
1691+
except ValueError:
1692+
logger.warning(
1693+
r"Broken block message for user %s, block and unblock again with a different message to prevent further issues",
1694+
id_,
1695+
)
1696+
continue
16671697

16681698
if end_time is not None:
1669-
after = (
1670-
datetime.fromisoformat(end_time.group(1)).replace(tzinfo=timezone.utc) - now
1671-
).total_seconds()
16721699
if after <= 0:
16731700
# No longer blocked
16741701
self.bot.blocked_users.pop(str(id_))
@@ -1862,12 +1889,13 @@ async def block(
18621889
if after is not None:
18631890
if "%" in reason:
18641891
raise commands.BadArgument('The reason contains illegal character "%".')
1865-
unixtime = int(after.dt.replace(tzinfo=timezone.utc).timestamp())
18661892

18671893
if after.arg:
1868-
reason += f" until: <t:{unixtime}:R>"
1894+
fmt_dt = discord.utils.format_dt(after.dt, "R")
18691895
if after.dt > after.now:
1870-
reason += f" until <t:{unixtime}:f>"
1896+
fmt_dt = discord.utils.format_dt(after.dt, "f")
1897+
1898+
reason += f" until {fmt_dt}"
18711899

18721900
reason += "."
18731901

core/thread.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ def _format_info_embed(self, user, log_url, log_count, color):
322322

323323
role_names = separator.join(roles)
324324

325-
created = str((time - user.created_at).days)
326325
user_info = []
327326
if self.bot.config["thread_show_account_age"]:
328-
user_info.append(f" was created {days(created)}")
327+
created = discord.utils.format_dt(user.created_at, "R")
328+
user_info.append(f" was created {created}")
329329

330330
embed = discord.Embed(color=color, description=user.mention, timestamp=time)
331331

@@ -337,10 +337,9 @@ def _format_info_embed(self, user, log_url, log_count, color):
337337
if member is not None:
338338
embed.set_author(name=str(user), icon_url=member.display_avatar.url, url=log_url)
339339

340-
joined = str((time - member.joined_at).days)
341-
# embed.add_field(name='Joined', value=joined + days(joined))
342340
if self.bot.config["thread_show_join_age"]:
343-
user_info.append(f"joined {days(joined)}")
341+
joined = discord.utils.format_dt(member.joined_at, "R")
342+
user_info.append(f"joined {joined}")
344343

345344
if member.nick:
346345
embed.add_field(name="Nickname", value=member.nick, inline=True)

core/time.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
import datetime
9+
import discord
910
from typing import TYPE_CHECKING, Any, Optional, Union
1011
import parsedatetime as pdt
1112
from dateutil.relativedelta import relativedelta
@@ -39,16 +40,6 @@ def __format__(self, format_spec: str) -> str:
3940
return f"{v} {singular}"
4041

4142

42-
def format_dt(dt: datetime.datetime, style: Optional[str] = None) -> str:
43-
"""https://github.com/Rapptz/RoboDanny/blob/bf7d4226350dff26df4981dd53134eeb2aceeb87/cogs/utils/formats.py#L89-L95"""
44-
if dt.tzinfo is None:
45-
dt = dt.replace(tzinfo=datetime.timezone.utc)
46-
47-
if style is None:
48-
return f"<t:{int(dt.timestamp())}>"
49-
return f"<t:{int(dt.timestamp())}:{style}>"
50-
51-
5243
class ShortTime:
5344
compiled = re.compile(
5445
"""
@@ -365,4 +356,4 @@ def human_timedelta(
365356

366357

367358
def format_relative(dt: datetime.datetime) -> str:
368-
return format_dt(dt, "R")
359+
return discord.utils.format_dt(dt, "R")

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extend-exclude = '''
2121

2222
[tool.poetry]
2323
name = 'Modmail'
24-
version = '4.0.0-dev20'
24+
version = '4.0.0'
2525
description = "Modmail is similar to Reddit's Modmail, both in functionality and purpose. It serves as a shared inbox for server staff to communicate with their users in a seamless way."
2626
license = 'AGPL-3.0-only'
2727
authors = [

0 commit comments

Comments
 (0)