-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
76 lines (55 loc) · 2.42 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from aiogram.filters import BaseFilter
from aiogram.types import Message
from typing import Tuple, List, Union, Any, Dict
from datetime import datetime
from datetime import date as datef
from aiogram import Bot
class CheckAdmin(BaseFilter):
def __init__(self, admins: dict):
self.admins = admins
def __call__(self, message: Message) -> bool:
return message.from_user.id in self.admins[message.chat.id]
class CheckDateFormat(BaseFilter):
async def __call__(self, message: Message) -> Union[bool, None]:
try:
date_text = message.text
print(f"Received date: {date_text}")
date_format = "%d/%m/%Y"
parsed_date = datetime.strptime(date_text, date_format)
day, month, year = parsed_date.day, parsed_date.month, parsed_date.year
print("THE TEMPLATE IS PASSED")
today = datetime.now()
future_date = datetime(year, month, day)
print(f"Today's date and time: {today}")
print(f"Future date and time: {future_date}")
print(f"{year} and {type(year)}")
print(f"{month} and {type(month)}")
print(f"{day} and {type(day)}")
if (today.day <= future_date.day and today.month <= future_date.month and today.year <= future_date.year):
print("THE DATE IS SET FOR A FUTURE EVENT")
return {"date": [year, month, day]}
else:
print("THE DATE IS IN THE PAST")
return {"date": ["error"]}
except ValueError as e:
print(f"DATE FORMAT ERROR: {e}")
return {"date": ["error"]}
class CheckTimeFormat(BaseFilter):
async def __call__(self, message: Message) -> Union[bool, Dict[str, Any]]:
try:
time = message.text
format = "%H:%M"
parsed_date = datetime.strptime(time, format)
hour, minute = parsed_date.hour, parsed_date.minute
return {"time" : [hour, minute]}
except ValueError:
return False
class AdminFilter(BaseFilter):
async def __call__(self, message: Message, bot: Bot) -> bool:
chat_member = await bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id)
if chat_member.status in ("administrator", "creator"):
# The user is an admin, handle accordingly
return True
else:
# The user is not an admin
return False