/
utils.py
129 lines (100 loc) · 3.76 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import datetime
import typing
import localization
from configurator import config
import sys
sys.path.append("./censure") # allow module import from git submodule
from censure import Censor
censor_ru = Censor.get(lang='ru')
censor_en = Censor.get(lang='en')
def check_for_profanity(text, lang = "ru"):
_profanity_detected = False
_word = None
if lang == "ru":
line_info = censor_ru.clean_line(text)
else:
line_info = censor_en.clean_line(text)
# line, bad_words_count, bad_phrases_count, detected_bad_words, detected_bad_phrases
# check
if line_info[1] or line_info[2]:
if line_info[1]:
_word = line_info[3][0]
else:
_word = line_info[4][0]
_profanity_detected = True
return _profanity_detected, _word, line_info
def check_for_profanity_all(text):
_del = False
_word = None
_line = None
# Check for RUSSIAN
_del, _word, _line = check_for_profanity(text, lang = "ru")
if not _del:
# Check for ENGLISH
_del, _word, _line = check_for_profanity(text, lang = "en")
return _del, _word
def user_mention(from_user):
_s = from_user.full_name
if from_user.full_name != from_user.mention:
_s += " (" + from_user.mention + ")"
else:
_s += " (<a href=\""+from_user.url+"\">id"+str(from_user.id)+"</a>)"
return _s
async def write_log(bot, message, log_type = "default"):
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
log_message = "🕥 <i>" + current_time + "</i> <b>[" + log_type.upper() + "]</b> "
log_message += message
return await bot.send_message(config.groups.logs, log_message)
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 = localization.get_string("report_message").format(
date=message_date.strftime(localization.get_string("report_date_format")),
chat_id=get_url_chat_id(config.groups.main),
msg_id=message_id)
if report_message:
msg += localization.get_string("report_note").format(note=report_message)
return msg
def get_url_chat_id(chat_id: int) -> int:
"""
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)
def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text # or whatever