-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
160 lines (115 loc) · 5.06 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Feel free to ignore this file.
It only contains some functions to simplify the rest of the code a bit.
"""
import time
from telegram import InlineKeyboardMarkup, InlineKeyboardButton, ParseMode, Update
from telegram.ext import CallbackContext
from config import ADMINS
from constants import WARNINGS, DEVICES
def message_button_url(
update: Update, context: CallbackContext, text, button_text, button_url
):
"""Send a message with URL."""
if update.message.reply_to_message:
return update.message.reply_to_message.reply_text(
text,
reply_markup=InlineKeyboardMarkup.from_button(
InlineKeyboardButton(button_text, button_url)
),
)
return context.bot.send_message(
update.message.chat_id,
text,
reply_markup=InlineKeyboardMarkup.from_button(
InlineKeyboardButton(button_text, button_url)
),
)
def delay_group_button_url(
update: Update, context: CallbackContext, text: str, button_text, button_url
):
"""Send a delayed message to group with URL."""
update.message.delete()
reply_message = message_button_url(update, context, text, button_text, button_url)
context.job_queue.run_once(
delete, 600, update.message.chat_id, str(reply_message.message_id)
)
def delay_group_preview(update: Update, context: CallbackContext, text: str):
"""Send a delayed message with preview."""
update.message.delete()
if update.message.reply_to_message:
update.message.reply_to_message.reply_text(text)
else:
reply_message = context.bot.send_message(
update.message.chat_id, text)
context.job_queue.run_once(
delete, 600, reply_message.chat_id, str(reply_message.message_id)
)
def delay_group(update: Update, context: CallbackContext, text: str):
"""Send a delayed message to group."""
update.message.delete()
if update.message.reply_to_message:
update.message.reply_to_message.reply_text(text, disable_web_page_preview=True)
else:
reply_message = context.bot.send_message(
update.message.chat_id, text, disable_web_page_preview=True
)
context.job_queue.run_once(
delete, 600, reply_message.chat_id, str(reply_message.message_id)
)
def delay_group_quote(update: Update, context: CallbackContext, text: str):
"""Send a delayed quoted message."""
update.message.delete()
if update.message.reply_to_message:
update.message.reply_to_message.reply_text(
text.format(update.message.reply_to_message.from_user.name),
ParseMode.HTML,
True,
)
else:
reply_message = context.bot.send_message(
update.message.chat_id,
text.format(update.message.from_user.name),
ParseMode.HTML,
True,
)
context.job_queue.run_once(
delete, 600, reply_message.chat_id, str(reply_message.message_id)
)
def delay_html(update: Update, context: CallbackContext, path: str):
if update.message.reply_to_message is not None and update.message.reply_to_message.from_user.name != "Telegram":
delay_group(update, context, f"Hey {update.message.reply_to_message.from_user.name} 🤖\n\n" + open(
f"strings/{path}.html").read())
return
delay_group(update, context, open(f"strings/{path}.html").read())
def delete(context: CallbackContext):
"""Delete given message from job."""
context.bot.delete_message(str(context.job.context), context.job.name)
def remove_message(update: Update, _: CallbackContext):
"""Remove the message."""
if update.message is not None:
update.message.delete()
def now():
"""Return current timestamp."""
return int(round(time.time() * 1000))
def check_admin_quote(update: Update) -> bool:
update.message.delete()
return update.message.from_user.id in ADMINS and update.message.reply_to_message is not None
def check_admin_quote_not_admin(update: Update) -> bool:
update.message.delete()
return update.message.from_user.id in ADMINS and update.message.reply_to_message is not None and update.message.reply_to_message.from_user.id not in ADMINS
def check_quote(update: Update) -> bool:
update.message.delete()
return update.message.reply_to_message is not None
def get_user_info(update: Update, context: CallbackContext, key: str = None) -> any:
info = context.bot_data.get(str(update.message.reply_to_message.from_user.id), {WARNINGS: 0, DEVICES: []})
if key is None:
return info
return info.get(key, None)
def set_user_info(update: Update, context: CallbackContext, value: any, key: str = None):
if key is None:
context.bot_data[str(update.message.reply_to_message.from_user.id)] = value
return
if update.message.reply_to_message.from_user.id not in context.bot_data:
context.bot_data[str(update.message.reply_to_message.from_user.id)] = {WARNINGS: 0, DEVICES: []}
context.bot_data[str(update.message.reply_to_message.from_user.id)][key] = value