Skip to content

Commit

Permalink
Add "forever" keyword for !ro and !ban aiogram#10
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Sheykin committed Jan 2, 2022
1 parent 092f347 commit dcc7dc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
25 changes: 17 additions & 8 deletions aiogram_bot/handlers/simple_admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import datetime
from contextlib import suppress
from typing import List, Optional

Expand Down Expand Up @@ -84,12 +85,16 @@ async def cmd_ro(message: types.Message, chat: Chat):
logger.error("Failed to restrict chat member: {error!r}", error=e)
return False

if duration >= datetime.timedelta(days=367):
duration = "forever"
else:
duration = format_timedelta(
duration, locale=chat.language, granularity="seconds", format="short"
)

await message.reply_to_message.answer(
_("<b>Read-only</b> activated for user {user}. Duration: {duration}").format(
user=message.reply_to_message.from_user.get_mention(),
duration=format_timedelta(
duration, locale=chat.language, granularity="seconds", format="short"
),
user=message.reply_to_message.from_user.get_mention(), duration=duration
)
)
return True
Expand Down Expand Up @@ -120,12 +125,16 @@ async def cmd_ban(message: types.Message, chat: Chat):
logger.error("Failed to kick chat member: {error!r}", error=e)
return False

if duration >= datetime.timedelta(days=367):
duration = "forever"
else:
duration = format_timedelta(
duration, locale=chat.language, granularity="seconds", format="short"
)

await message.reply_to_message.answer(
_("User {user} <b>banned</b> for {duration}").format(
user=message.reply_to_message.from_user.get_mention(),
duration=format_timedelta(
duration, locale=chat.language, granularity="seconds", format="short"
),
user=message.reply_to_message.from_user.get_mention(), duration=duration
)
)
return True
Expand Down
6 changes: 4 additions & 2 deletions aiogram_bot/utils/timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from aiogram import types

PATTERN = re.compile(r"(?P<value>\d+)(?P<modifier>[wdhms])")
LINE_PATTERN = re.compile(r"^(\d+[wdhms]){1,}$")
PATTERN = re.compile(r"(?P<value>\d+)(?P<modifier>[wdhms])|^(?P<keyword>forever)$")
LINE_PATTERN = re.compile(r"^(\d+[wdhms])+$|^(forever)$")

MODIFIERS = {
"w": datetime.timedelta(weeks=1),
Expand All @@ -26,6 +26,8 @@ def parse_timedelta(value: str) -> datetime.timedelta:
raise TimedeltaParseError("Invalid time format")

try:
if PATTERN.match(value).group("keyword"):
return datetime.timedelta(days=367)
result = datetime.timedelta()
for match in PATTERN.finditer(value):
value, modifier = match.groups()
Expand Down

0 comments on commit dcc7dc1

Please sign in to comment.