-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_status.py
342 lines (248 loc) · 11.6 KB
/
chat_status.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from functools import wraps
from telegram import Bot, Chat, ChatMember, Update, ParseMode
from tg_bot import dispatcher, DEL_CMDS, WHITELIST_USERS, TIGER_USERS, SUPPORT_USERS, SUDO_USERS, DEV_USERS
from tg_bot.modules.helper_funcs.extraction import extract_user # needed
def is_whitelist_plus(chat: Chat, user_id: int, member: ChatMember = None) -> bool:
return any(user_id in user for user in [WHITELIST_USERS, TIGER_USERS, SUPPORT_USERS, SUDO_USERS, DEV_USERS])
def is_support_plus(chat: Chat, user_id: int, member: ChatMember = None) -> bool:
return user_id in SUPPORT_USERS or user_id in SUDO_USERS or user_id in DEV_USERS
def is_sudo_plus(chat: Chat, user_id: int, member: ChatMember = None) -> bool:
return user_id in SUDO_USERS or user_id in DEV_USERS
def is_user_admin(chat: Chat, user_id: int, member: ChatMember = None) -> bool:
if (chat.type == 'private'
or user_id in SUDO_USERS
or user_id in DEV_USERS
or chat.all_members_are_administrators):
return True
if not member:
member = chat.get_member(user_id)
return member.status in ('administrator', 'creator')
def is_bot_admin(chat: Chat, bot_id: int, bot_member: ChatMember = None) -> bool:
if chat.type == 'private' or chat.all_members_are_administrators:
return True
if not bot_member:
bot_member = chat.get_member(bot_id)
return bot_member.status in ('administrator', 'creator')
def can_delete(chat: Chat, bot_id: int) -> bool:
return chat.get_member(bot_id).can_delete_messages
def is_user_ban_protected(chat: Chat, user_id: int, member: ChatMember = None) -> bool:
if (chat.type == 'private'
or user_id in SUDO_USERS
or user_id in DEV_USERS
or user_id in WHITELIST_USERS
or user_id in TIGER_USERS
or chat.all_members_are_administrators):
return True
if not member:
member = chat.get_member(user_id)
return member.status in ('administrator', 'creator')
def is_user_in_chat(chat: Chat, user_id: int) -> bool:
member = chat.get_member(user_id)
return member.status not in ('left', 'kicked')
def dev_plus(func):
@wraps(func)
def is_dev_plus_func(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
if user.id in DEV_USERS:
return func(bot, update, *args, **kwargs)
elif not user:
pass
elif DEL_CMDS and " " not in update.effective_message.text:
update.effective_message.delete()
else:
update.effective_message.reply_text("This is a developer restricted command."
" You do not have permissions to run this Visit @JuliaUpdatesChat.")
return is_dev_plus_func
def sudo_plus(func):
@wraps(func)
def is_sudo_plus_func(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
chat = update.effective_chat
if user and is_sudo_plus(chat, user.id):
return func(bot, update, *args, **kwargs)
elif not user:
pass
elif DEL_CMDS and " " not in update.effective_message.text:
update.effective_message.delete()
else:
update.effective_message.reply_text("Only admins can execute this command")
return is_sudo_plus_func
def support_plus(func):
@wraps(func)
def is_support_plus_func(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
chat = update.effective_chat
if user and is_whitelist_plus(chat, user.id):
return func(bot, update, *args, **kwargs)
elif DEL_CMDS and " " not in update.effective_message.text:
update.effective_message.delete()
return is_support_plus_func
def whitelist_plus(func):
@wraps(func)
def is_whitelist_plus_func(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
chat = update.effective_chat
if user and is_whitelist_plus(chat, user.id):
return func(bot, update, *args, **kwargs)
else:
update.effective_message.reply_text("You don't have access to use this.\nVisit @JuliaUpdates")
return is_whitelist_plus_func
def user_admin(func):
@wraps(func)
def is_admin(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
chat = update.effective_chat
if user and is_user_admin(chat, user.id):
return func(bot, update, *args, **kwargs)
elif not user:
pass
elif DEL_CMDS and " " not in update.effective_message.text:
update.effective_message.delete()
else:
update.effective_message.reply_text("Only admins can execute this command")
return is_admin
def user_admin_no_reply(func):
@wraps(func)
def is_not_admin_no_reply(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
chat = update.effective_chat
if user and is_user_admin(chat, user.id):
return func(bot, update, *args, **kwargs)
elif not user:
pass
elif DEL_CMDS and " " not in update.effective_message.text:
update.effective_message.delete()
return is_not_admin_no_reply
def user_not_admin(func):
@wraps(func)
def is_not_admin(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user
chat = update.effective_chat
if user and not is_user_admin(chat, user.id):
return func(bot, update, *args, **kwargs)
elif not user:
pass
return is_not_admin
def bot_admin(func):
@wraps(func)
def is_admin(bot: Bot, update: Update, *args, **kwargs):
chat = update.effective_chat
update_chat_title = chat.title
message_chat_title = update.effective_message.chat.title
if update_chat_title == message_chat_title:
not_admin = "I'm not admin! "
else:
not_admin = f"I'm not admin in <b>{update_chat_title}</b>! "
if is_bot_admin(chat, bot.id):
return func(bot, update, *args, **kwargs)
else:
update.effective_message.reply_text(not_admin, parse_mode=ParseMode.HTML)
return is_admin
def bot_can_delete(func):
@wraps(func)
def delete_rights(bot: Bot, update: Update, *args, **kwargs):
chat = update.effective_chat
update_chat_title = chat.title
message_chat_title = update.effective_message.chat.title
if update_chat_title == message_chat_title:
cant_delete = f"I can't delete messages here!\nMake sure I'm admin and can delete other user's messages."
else:
cant_delete = f"I can't delete messages in <b>{update_chat_title}</b>!\nMake sure I'm admin and can delete other user's messages there."
if can_delete(chat, bot.id):
return func(bot, update, *args, **kwargs)
else:
update.effective_message.reply_text(cant_delete, parse_mode=ParseMode.HTML)
return delete_rights
def can_pin(func):
@wraps(func)
def pin_rights(bot: Bot, update: Update, *args, **kwargs):
chat = update.effective_chat
update_chat_title = chat.title
message_chat_title = update.effective_message.chat.title
if update_chat_title == message_chat_title:
cant_pin = f"I can't pin messages here!\nMake sure I'm admin and can pin messages."
else:
cant_pin = f"I can't pin messages in <b>{update_chat_title}</b>!\nMake sure I'm admin and can pin messages there."
if chat.get_member(bot.id).can_pin_messages:
return func(bot, update, *args, **kwargs)
else:
update.effective_message.reply_text(cant_pin, parse_mode=ParseMode.HTML)
return pin_rights
def can_promote(func):
@wraps(func)
def promote_rights(bot: Bot, update: Update, *args, **kwargs):
chat = update.effective_chat
update_chat_title = chat.title
message_chat_title = update.effective_message.chat.title
if update_chat_title == message_chat_title:
cant_promote = f"I can't promote/demote people here!\nMake sure I'm admin and can appoint new admins."
else:
cant_promote = (f"I can't promote/demote people in <b>{update_chat_title}</b>!\n"
f"Make sure I'm admin there and can appoint new admins.")
if chat.get_member(bot.id).can_promote_members:
return func(bot, update, *args, **kwargs)
else:
update.effective_message.reply_text(cant_promote, parse_mode=ParseMode.HTML)
return promote_rights
def can_restrict(func):
@wraps(func)
def restrict_rights(bot: Bot, update: Update, *args, **kwargs):
chat = update.effective_chat
update_chat_title = chat.title
message_chat_title = update.effective_message.chat.title
if update_chat_title == message_chat_title:
cant_restrict = f"I can't restrict people here!\nMake sure I'm admin and can restrict users."
else:
cant_restrict = f"I can't restrict people in <b>{update_chat_title}</b>!\nMake sure I'm admin there and can restrict users."
if chat.get_member(bot.id).can_restrict_members:
return func(bot, update, *args, **kwargs)
else:
update.effective_message.reply_text(cant_restrict, parse_mode=ParseMode.HTML)
return restrict_rights
def connection_status(func):
@wraps(func)
def connected_status(bot: Bot, update: Update, *args, **kwargs):
conn = connected(bot, update, update.effective_chat, update.effective_user.id, need_admin=False)
if conn:
chat = dispatcher.bot.getChat(conn)
update.__setattr__("_effective_chat", chat)
return func(bot, update, *args, **kwargs)
else:
if update.effective_message.chat.type == "private":
update.effective_message.reply_text("This command can be used in Groups and not in pms")
return connected_status
return func(bot, update, *args, **kwargs)
return connected_status
# Workaround for circular import with connection.py
from tg_bot.modules import connection
connected = connection.connected
def user_can_ban(func):
@wraps(func)
def user_is_banhammer(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user.id
member = update.effective_chat.get_member(user)
if not (member.can_restrict_members or member.status == "creator") and not user in SUDO_USERS:
update.effective_message.reply_text("Sorry you don't have sufficient rights !")
return ""
return func(bot, update, *args, **kwargs)
return user_is_banhammer
def user_can_restrict(func):
@wraps(func)
def user_is_restrict(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user.id
member = update.effective_chat.get_member(user)
if not (member.can_promote_members or member.status == "creator") and not user in SUDO_USERS:
update.effective_message.reply_text("Sorry you don't have sufficient rights !")
return ""
return func(bot, update, *args, **kwargs)
return user_is_restrict
def user_can_pin(func):
@wraps(func)
def user_pin_can(bot: Bot, update: Update, *args, **kwargs):
user = update.effective_user.id
member = update.effective_chat.get_member(user)
if not (member.can_pin_messages or member.status == "creator") and not user in SUDO_USERS:
update.effective_message.reply_text("Sorry you don't have sufficient rights !")
return ""
return func(bot, update, *args, **kwargs)
return user_pin_can