/
utils.py
59 lines (52 loc) · 1.99 KB
/
utils.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
import datetime
import typing
import localization as lang
import config
def get_restriction_time(string: str) -> typing.Optional[int]:
"""
Get user restriction time in seconds
:param string: string to check for multiplier. The last symbol should be one of:
"m" for minutes, "h" for hours and "d" for days
:return: number of seconds to restrict or None if error
"""
if len(string) < 2:
return None
letter = string[-1]
try:
number = int(string[:-1])
except TypeError:
return None
else:
if letter == "m":
return 60 * number
elif letter == "h":
return 3600 * number
elif letter == "d":
return 86400 * number
else:
return None
def get_report_comment(message_date: datetime.datetime, message_id: int, report_message: typing.Optional[str]) -> str:
"""
Generates a report message for admins
:param message_date: Datetime when reported message was sent
:param message_id: ID of that message
:param report_message: An optional note for admins so that they can understand what's wrong
:return: A report message for admins in report chat
"""
msg = lang.get_string("report_message").format(
date=message_date.strftime(lang.get_string("report_date_format")),
chat_id=get_url_chat_id(config.group_main),
msg_id=message_id)
if report_message:
msg += lang.get_string("report_note").format(note=report_message)
return msg
def get_url_chat_id(chat_id):
"""
Well, this value is a "magic number", so I have to explain it a bit.
I don't want to use hardcoded chat username, so I just take its ID (see "group_main" variable above),
add id_compensator and take a positive value. This way I can use https://t.me/c/{chat_id}/{msg_id} links,
which don't rely on chat username.
:param chat_id: chat_id to apply magic number to
:return: chat_id for t.me links
"""
return abs(chat_id+1_000_000_000_000)