Skip to content

Commit 49f91d8

Browse files
committed
Convert MuteTime to static definitions
1 parent 08699d6 commit 49f91d8

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

forward/converters.py

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,30 @@
2727
TIME_RE = re.compile(TIME_RE_STRING, re.I)
2828
TIME_SPLIT = re.compile(r"t(?:ime)?=")
2929

30-
_ = i18n.Translator("Mutes", __file__)
31-
32-
33-
class MuteTime(Converter):
34-
"""
35-
This will parse my defined multi response pattern and provide usable formats
36-
to be used in multiple reponses
37-
"""
38-
39-
async def convert(
40-
self, duration: str
41-
) -> Dict[str, Union[timedelta, str, None]]:
42-
time_split = TIME_SPLIT.split(argument)
43-
result: Dict[str, Union[timedelta, str, None]] = {}
44-
if time_split:
45-
maybe_time = time_split[-1]
46-
else:
47-
maybe_time = argument
48-
49-
time_data = {}
50-
for time in TIME_RE.finditer(maybe_time):
51-
argument = argument.replace(time[0], "")
52-
for k, v in time.groupdict().items():
53-
if v:
54-
time_data[k] = int(v)
55-
if time_data:
56-
try:
57-
result["duration"] = timedelta(**time_data)
58-
except OverflowError:
59-
raise commands.BadArgument(
60-
_("The time provided is too long; use a more reasonable time.")
61-
)
62-
result["reason"] = argument.strip()
63-
return result
30+
_ = i18n.Translator("Forward", __file__)
31+
32+
def str_to_timedelta(
33+
duration: str
34+
) -> Dict[str, Union[timedelta, str, None]]:
35+
time_split = TIME_SPLIT.split(argument)
36+
result: Dict[str, Union[timedelta, str, None]] = {}
37+
if time_split:
38+
maybe_time = time_split[-1]
39+
else:
40+
maybe_time = argument
41+
42+
time_data = {}
43+
for time in TIME_RE.finditer(maybe_time):
44+
argument = argument.replace(time[0], "")
45+
for k, v in time.groupdict().items():
46+
if v:
47+
time_data[k] = int(v)
48+
if time_data:
49+
try:
50+
result["duration"] = timedelta(**time_data)
51+
except OverflowError:
52+
raise commands.BadArgument(
53+
_("The time provided is too long; use a more reasonable time.")
54+
)
55+
result["reason"] = argument.strip()
56+
return result

forward/forward.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# from googletrans import Translator
33
import discord
44
import uuid
5-
from .converters import MuteTime
5+
from .converters import str_to_timedelta
66
from datetime import datetime, timezone
77
import asyncio
88
import logging
@@ -270,13 +270,13 @@ async def tpm(self, ctx, language, user: discord.Member, *, message: str):
270270
@commands.command()
271271
@commands.guild_only()
272272
@checks.guildowner()
273-
async def tblock(self, ctx, user: discord.Member, time: Optional[MuteTime] = None):
273+
async def tblock(self, ctx, user: discord.Member, time: Optional[str] = "5d"):
274274
"""Blocks a member from sending dms to the bot
275275
"""
276276
async with self.config.blocked() as blocked:
277277
userid = str(user.id)
278278
if userid not in blocked:
279-
duration = time.get("duration") if time else MuteTime().convert(duration="5d").get("duration")
279+
duration = str_to_timedelta(duration=time).get("duration")
280280
blocked[userid] = (datetime.now(timezone.utc) + duration).timestamp() # until
281281
await ctx.maybe_send_embed("Blocked <@{id}> from send messages to the bot until {until}.".format(id=userid, until=datetime.fromtimestamp(blocked[userid])))
282282
else:

0 commit comments

Comments
 (0)