Karma commited on
Commit
b11a716
·
2 Parent(s): d9b6ce1 7d248df

Merge pull request #38 from Infamous-Hydra/Infamous-Hydra-patch-10

Browse files
Files changed (1) hide show
  1. Mikobot/plugins/mute.py +192 -628
Mikobot/plugins/mute.py CHANGED
@@ -1,697 +1,261 @@
1
  # <============================================== IMPORTS =========================================================>
2
- from random import choice
3
- from traceback import format_exc
4
-
5
- from pyrogram import enums
6
- from pyrogram.errors import (
7
- ChatAdminRequired,
8
- RightForbidden,
9
- RPCError,
10
- UserNotParticipant,
 
 
 
 
 
11
  )
12
- from pyrogram.filters import regex
13
- from pyrogram.types import (
14
- CallbackQuery,
15
- ChatPermissions,
16
- InlineKeyboardButton,
17
- InlineKeyboardMarkup,
18
- Message,
19
- )
20
-
21
- from Infamous.karma import MUTE_GIFS
22
- from Mikobot import BOT_ID, LOGGER, MESSAGE_DUMP, OWNER_ID, SUPPORT_STAFF, app
23
- from Mikobot.utils.caching import ADMIN_CACHE, admin_cache_reload
24
- from Mikobot.utils.custom_filters import command, restrict_filter
25
- from Mikobot.utils.extract_user import extract_user
26
- from Mikobot.utils.parser import mention_html
27
- from Mikobot.utils.string import extract_time
28
 
29
  # <=======================================================================================================>
30
 
31
 
32
  # <================================================ FUNCTION =======================================================>
33
- @app.on_message(command("tmute") & restrict_filter)
34
- async def tmute_usr(c: app, m: Message):
35
- if len(m.text.split()) == 1 and not m.reply_to_message:
36
- await m.reply_text("I can't mute nothing!")
37
- return
38
-
39
- try:
40
- user_id, user_first_name, _ = await extract_user(c, m)
41
- except Exception:
42
- return
43
-
44
  if not user_id:
45
- await m.reply_text("Cannot find user to mute !")
46
- return
47
- if user_id == BOT_ID:
48
- await m.reply_text("Huh, why would I mute myself?")
49
- return
50
-
51
- if user_id in SUPPORT_STAFF:
52
- LOGGER.info(
53
- f"{m.from_user.id} trying to mute {user_id} (SUPPORT_STAFF) in {m.chat.id}",
54
- )
55
- await m.reply_text(
56
- text="This user is in my support staff, cannot restrict them."
57
- )
58
- return
59
 
60
  try:
61
- admins_group = {i[0] for i in ADMIN_CACHE[m.chat.id]}
62
- except KeyError:
63
- admins_group = await admin_cache_reload(m, "mute")
64
-
65
- if user_id in admins_group:
66
- await m.reply_text(text="This user is an admin, I cannot mute them!")
67
- return
68
-
69
- r_id = m.reply_to_message.id if m.reply_to_message else m.id
70
-
71
- if m.reply_to_message and len(m.text.split()) >= 2:
72
- reason = m.text.split(None, 1)[1]
73
- elif not m.reply_to_message and len(m.text.split()) >= 3:
74
- reason = m.text.split(None, 2)[2]
75
- else:
76
- await m.reply_text("Read /help !!")
77
- return
78
-
79
- if not reason:
80
- await m.reply_text("You haven't specified a time to mute this user for!")
81
- return
82
-
83
- split_reason = reason.split(None, 1)
84
- time_val = split_reason[0].lower()
85
- reason = split_reason[1] if len(split_reason) > 1 else ""
86
- mutetime = await extract_time(m, time_val)
87
-
88
- if not mutetime:
89
- return
90
-
91
- try:
92
- await m.chat.restrict_member(
93
- user_id,
94
- ChatPermissions(),
95
- mutetime,
96
- )
97
- LOGGER.info(f"{m.from_user.id} tmuted {user_id} in {m.chat.id}")
98
- admin = await mention_html(m.from_user.first_name, m.from_user.id)
99
- muted = await mention_html(user_first_name, user_id)
100
- txt = f"Admin {admin} muted {muted}!"
101
- if reason:
102
- txt += f"\n<b>Reason</b>: {reason}"
103
  else:
104
- txt += "\n<b>Reason</b>: Not Specified"
105
- if mutetime:
106
- txt += f"\n<b>Muted for</b>: {time_val}"
107
- keyboard = InlineKeyboardMarkup(
108
- [
109
- [
110
- InlineKeyboardButton(
111
- "UNMUTE",
112
- callback_data=f"unmute_={user_id}",
113
- ),
114
- ],
115
- ],
116
- )
117
- mutt = choice(MUTE_GIFS)
118
- try:
119
- await m.reply_animation(
120
- animation=str(mutt),
121
- caption=txt,
122
- reply_markup=keyboard,
123
- reply_to_message_id=r_id,
124
- )
125
- except Exception:
126
- await m.reply_text(txt, reply_markup=keyboard, reply_to_message_id=r_id)
127
- await c.send_message(MESSAGE_DUMP, f"#REMOVE from MUTE_GIFS\n{mutt}")
128
- except ChatAdminRequired:
129
- await m.reply_text(text="I'm not admin or I don't have rights.")
130
- except RightForbidden:
131
- await m.reply_text(text="I don't have enough rights to ban this user.")
132
- except UserNotParticipant:
133
- await m.reply_text("How can I mute a user who is not a part of this chat?")
134
- except RPCError as ef:
135
- await m.reply_text(
136
- text=f"""Some error occured, report it using `/bug`
137
-
138
- <b>Error:</b> <code>{ef}</code>"""
139
- )
140
- LOGGER.error(ef)
141
-
142
- return
143
-
144
 
145
- @app.on_message(command("dtmute") & restrict_filter)
146
- async def dtmute_usr(c: app, m: Message):
147
- if len(m.text.split()) == 1 and not m.reply_to_message:
148
- await m.reply_text("I can't mute nothing!")
149
- return
150
 
151
- if not m.reply_to_message:
152
- return await m.reply_text("No replied message and user to delete and mute!")
153
-
154
- reason = None
155
- user_id = m.reply_to_message.from_user.id
156
- user_first_name = m.reply_to_message.from_user.first_name
157
-
158
- if not user_id:
159
- await m.reply_text("Cannot find user to mute !")
160
- return
161
- if user_id == BOT_ID:
162
- await m.reply_text("Huh, why would I mute myself?")
163
- return
164
-
165
- if user_id in SUPPORT_STAFF:
166
- LOGGER.info(
167
- f"{m.from_user.id} trying to mute {user_id} (SUPPORT_STAFF) in {m.chat.id}",
168
- )
169
- await m.reply_text(
170
- text="This user is in my support staff, cannot restrict them."
171
- )
172
- return
173
 
174
- try:
175
- admins_group = {i[0] for i in ADMIN_CACHE[m.chat.id]}
176
- except KeyError:
177
- admins_group = await admin_cache_reload(m, "mute")
178
-
179
- if user_id in admins_group:
180
- await m.reply_text(text="This user is an admin, I cannot mute them!")
181
- return
182
-
183
- if m.reply_to_message and len(m.text.split()) >= 2:
184
- reason = m.text.split(None, 1)[1]
185
- elif not m.reply_to_message and len(m.text.split()) >= 3:
186
- reason = m.text.split(None, 2)[2]
187
- else:
188
- await m.reply_text("Read /help !!")
189
- return
190
 
191
- if not reason:
192
- await m.reply_text("You haven't specified a time to mute this user for!")
193
- return
194
 
195
- split_reason = reason.split(None, 1)
196
- time_val = split_reason[0].lower()
197
- reason = split_reason[1] if len(split_reason) > 1 else ""
 
 
 
198
 
199
- mutetime = await extract_time(m, time_val)
 
 
200
 
201
- if not mutetime:
202
- return
203
- try:
204
- await m.chat.restrict_member(
205
- user_id,
206
- ChatPermissions(),
207
- mutetime,
208
- )
209
- LOGGER.info(f"{m.from_user.id} dtmuted {user_id} in {m.chat.id}")
210
- await m.reply_to_message.delete()
211
- admin = await mention_html(m.from_user.first_name, m.from_user.id)
212
- muted = await mention_html(user_first_name, user_id)
213
- txt = f"Admin {admin} muted {muted}!"
214
- if reason:
215
- txt += f"\n<b>Reason</b>: {reason}"
216
- else:
217
- txt += "\n<b>Reason</b>: Not Specified"
218
- if mutetime:
219
- txt += f"\n<b>Muted for</b>: {time_val}"
220
- keyboard = InlineKeyboardMarkup(
221
- [
222
- [
223
- InlineKeyboardButton(
224
- "UNMUTE",
225
- callback_data=f"unmute_={user_id}",
226
- ),
227
- ],
228
- ],
229
- )
230
- mutt = choice(MUTE_GIFS)
231
- try:
232
- await m.reply_animation(
233
- animation=str(mutt),
234
- caption=txt,
235
- reply_markup=keyboard,
236
- )
237
- except Exception:
238
- await m.reply_text(txt, reply_markup=keyboard)
239
- await c.send_message(MESSAGE_DUMP, f"#REMOVE from MUTE_GIFS\n{mutt}")
240
- except ChatAdminRequired:
241
- await m.reply_text(text="I'm not admin or I don't have rights.")
242
- except RightForbidden:
243
- await m.reply_text(text="I don't have enough rights to ban this user.")
244
- except UserNotParticipant:
245
- await m.reply_text("How can I mute a user who is not a part of this chat?")
246
- except RPCError as ef:
247
- await m.reply_text(
248
- text=f"""Some error occured, report it using `/bug`
249
-
250
- <b>Error:</b> <code>{ef}</code>"""
251
- )
252
- LOGGER.error(ef)
253
 
254
- return
 
 
255
 
 
256
 
257
- @app.on_message(command("stmute") & restrict_filter)
258
- async def stmute_usr(c: app, m: Message):
259
- if len(m.text.split()) == 1 and not m.reply_to_message:
260
- await m.reply_text("I can't mute nothing!")
261
- return
 
262
 
263
- try:
264
- user_id, _, _ = await extract_user(c, m)
265
- except Exception:
266
- return
267
 
268
- if not user_id:
269
- await m.reply_text("Cannot find user to mute !")
270
- return
271
- if user_id == BOT_ID:
272
- await m.reply_text("Huh, why would I mute myself?")
273
- return
274
-
275
- if user_id in SUPPORT_STAFF:
276
- LOGGER.info(
277
- f"{m.from_user.id} trying to mute {user_id} (SUPPORT_STAFF) in {m.chat.id}",
278
- )
279
- await m.reply_text(
280
- text="This user is in my support staff, cannot restrict them."
281
  )
282
- return
283
 
284
- try:
285
- admins_group = {i[0] for i in ADMIN_CACHE[m.chat.id]}
286
- except KeyError:
287
- admins_group = await admin_cache_reload(m, "mute")
288
-
289
- if user_id in admins_group:
290
- await m.reply_text(text="This user is an admin, I cannot mute them!")
291
- return
292
-
293
- if m.reply_to_message and len(m.text.split()) >= 2:
294
- reason = m.text.split(None, 1)[1]
295
- elif not m.reply_to_message and len(m.text.split()) >= 3:
296
- reason = m.text.split(None, 2)[2]
297
  else:
298
- await m.reply_text("Read /help !!")
299
- return
300
-
301
- if not reason:
302
- await m.reply_text("You haven't specified a time to mute this user for!")
303
- return
304
-
305
- split_reason = reason.split(None, 1)
306
- time_val = split_reason[0].lower()
307
- reason = split_reason[1] if len(split_reason) > 1 else ""
308
-
309
- mutetime = await extract_time(m, time_val)
310
-
311
- if not mutetime:
312
- return
313
 
314
- try:
315
- await m.chat.restrict_member(
316
- user_id,
317
- ChatPermissions(),
318
- mutetime,
319
- )
320
- LOGGER.info(f"{m.from_user.id} stmuted {user_id} in {m.chat.id}")
321
- await m.delete()
322
- if m.reply_to_message:
323
- await m.reply_to_message.delete()
324
- except ChatAdminRequired:
325
- await m.reply_text(text="I'm not admin or I don't have rights.")
326
- except RightForbidden:
327
- await m.reply_text(text="I don't have enough rights to ban this user.")
328
- except UserNotParticipant:
329
- await m.reply_text("How can I mute a user who is not a part of this chat?")
330
- except RPCError as ef:
331
- await m.reply_text(
332
- text=f"""Some error occured, report it using `/bug`
333
-
334
- <b>Error:</b> <code>{ef}</code>"""
335
- )
336
- LOGGER.error(ef)
337
 
338
- return
339
 
 
 
 
 
 
 
 
 
340
 
341
- @app.on_message(command("mute") & restrict_filter)
342
- async def mute_usr(c: app, m: Message):
343
- if len(m.text.split()) == 1 and not m.reply_to_message:
344
- await m.reply_text("I can't mute nothing!")
345
- return
346
-
347
- reason = None
348
- if m.reply_to_message:
349
- r_id = m.reply_to_message.id
350
- if len(m.text.split()) >= 2:
351
- reason = m.text.split(None, 1)[1]
352
- else:
353
- r_id = m.id
354
- if len(m.text.split()) >= 3:
355
- reason = m.text.split(None, 2)[2]
356
- try:
357
- user_id, user_first_name, _ = await extract_user(c, m)
358
- except Exception:
359
- return
360
-
361
  if not user_id:
362
- await m.reply_text("Cannot find user to mute")
363
- return
364
- if user_id == BOT_ID:
365
- await m.reply_text("Huh, why would I mute myself?")
366
- return
367
-
368
- if user_id in SUPPORT_STAFF:
369
- LOGGER.info(
370
- f"{m.from_user.id} trying to mute {user_id} (SUPPORT_STAFF) in {m.chat.id}",
371
  )
372
- await m.reply_text(
373
- text="This user is in my support staff, cannot restrict them."
374
- )
375
- return
376
-
377
- try:
378
- admins_group = {i[0] for i in ADMIN_CACHE[m.chat.id]}
379
- except KeyError:
380
- admins_group = await admin_cache_reload(m, "mute")
381
 
382
- if user_id in admins_group:
383
- await m.reply_text(text="This user is an admin, I cannot mute them!")
384
- return
385
 
386
- try:
387
- await m.chat.restrict_member(
388
- user_id,
389
- ChatPermissions(),
390
- )
391
- LOGGER.info(f"{m.from_user.id} muted {user_id} in {m.chat.id}")
392
- admin = await mention_html(m.from_user.first_name, m.from_user.id)
393
- muted = await mention_html(user_first_name, user_id)
394
- txt = f"Admin {admin} muted {muted}!"
395
- if reason:
396
- txt += f"\n<b>Reason</b>: {reason}"
397
  else:
398
- txt += "\n<b>Reason</b>: Not Specified"
399
- keyboard = InlineKeyboardMarkup(
400
- [
401
- [
402
- InlineKeyboardButton(
403
- "UNMUTE",
404
- callback_data=f"unmute_={user_id}",
405
- ),
406
- ],
407
- ],
408
- )
409
- mutt = choice(MUTE_GIFS)
410
- try:
411
- await m.reply_animation(
412
- animation=str(mutt),
413
- caption=txt,
414
- reply_markup=keyboard,
415
- reply_to_message_id=r_id,
 
 
 
 
 
 
 
416
  )
417
- except Exception:
418
- await m.reply_text(txt, reply_markup=keyboard, reply_to_message_id=r_id)
419
- await c.send_message(MESSAGE_DUMP, f"#REMOVE from MUTE_GIFS\n{mutt}")
420
- except ChatAdminRequired:
421
- await m.reply_text(text="I'm not admin or I don't have rights.")
422
- except RightForbidden:
423
- await m.reply_text(text="I don't have enough rights to ban this user.")
424
- except UserNotParticipant:
425
- await m.reply_text("How can I mute a user who is not a part of this chat?")
426
- except RPCError as ef:
427
- await m.reply_text(
428
- text=f"""Some error occured, report it using `/bug`
429
-
430
- <b>Error:</b> <code>{ef}</code>"""
431
  )
432
- LOGGER.error(ef)
433
 
434
- return
435
 
436
 
437
- @app.on_message(command("smute") & restrict_filter)
438
- async def smute_usr(c: app, m: Message):
439
- if len(m.text.split()) == 1 and not m.reply_to_message:
440
- await m.reply_text("I can't mute nothing!")
441
- return
 
 
 
442
 
443
- try:
444
- user_id, _, _ = await extract_user(c, m)
445
- except Exception:
446
- return
447
 
448
- if not user_id:
449
- await m.reply_text("Cannot find user to mute")
450
- return
451
- if user_id == BOT_ID:
452
- await m.reply_text("Huh, why would I mute myself?")
453
- return
454
-
455
- if user_id in SUPPORT_STAFF:
456
- LOGGER.info(
457
- f"{m.from_user.id} trying to mute {user_id} (SUPPORT_STAFF) in {m.chat.id}",
458
- )
459
- await m.reply_text(
460
- text="This user is in my support staff, cannot restrict them."
461
- )
462
- return
463
-
464
- try:
465
- admins_group = {i[0] for i in ADMIN_CACHE[m.chat.id]}
466
- except KeyError:
467
- admins_group = await admin_cache_reload(m, "mute")
468
-
469
- if user_id in admins_group:
470
- await m.reply_text(text="This user is an admin, I cannot mute them!")
471
- return
472
-
473
- try:
474
- await m.chat.restrict_member(
475
- user_id,
476
- ChatPermissions(),
477
- )
478
- LOGGER.info(f"{m.from_user.id} smuted {user_id} in {m.chat.id}")
479
- await m.delete()
480
- if m.reply_to_message:
481
- await m.reply_to_message.delete()
482
- return
483
- return
484
- except ChatAdminRequired:
485
- await m.reply_text(text="I'm not admin or I don't have rights.")
486
- except RightForbidden:
487
- await m.reply_text(text="I don't have enough rights to ban this user.")
488
- except UserNotParticipant:
489
- await m.reply_text("How can I mute a user who is not a part of this chat?")
490
- except RPCError as ef:
491
- await m.reply_text(
492
- text=f"""Some error occured, report it using `/bug`
493
-
494
- <b>Error:</b> <code>{ef}</code>"""
495
- )
496
- LOGGER.error(ef)
497
 
498
- return
499
 
 
 
 
500
 
501
- @app.on_message(command("dmute") & restrict_filter)
502
- async def dmute_usr(c: app, m: Message):
503
- if len(m.text.split()) == 1 and not m.reply_to_message:
504
- await m.reply_text("I can't mute nothing!")
505
- return
506
- if not m.reply_to_message:
507
- return await m.reply_text("No replied message and user to delete and mute!")
508
 
509
- reason = None
510
- if m.reply_to_message:
511
- if len(m.text.split()) >= 2:
512
- reason = m.text.split(None, 1)[1]
513
  else:
514
- if len(m.text.split()) >= 3:
515
- reason = m.text.split(None, 2)[2]
516
- user_id = m.reply_to_message.from_user.id
517
- user_first_name = m.reply_to_message.from_user.first_name
518
 
519
- if not user_id:
520
- await m.reply_text("Cannot find user to mute")
521
- return
522
- if user_id == BOT_ID:
523
- await m.reply_text("Huh, why would I mute myself?")
524
- return
525
-
526
- if user_id in SUPPORT_STAFF:
527
- LOGGER.info(
528
- f"{m.from_user.id} trying to mute {user_id} (SUPPORT_STAFF) in {m.chat.id}",
529
- )
530
- await m.reply_text(
531
- text="This user is in my support staff, cannot restrict them."
532
- )
533
- return
534
-
535
- try:
536
- admins_group = {i[0] for i in ADMIN_CACHE[m.chat.id]}
537
- except KeyError:
538
- admins_group = await admin_cache_reload(m, "mute")
539
 
540
- if user_id in admins_group:
541
- await m.reply_text(text="This user is an admin, I cannot mute them!")
542
- return
 
 
 
 
 
 
 
 
 
543
 
544
  try:
545
- await m.chat.restrict_member(
546
- user_id,
547
- ChatPermissions(),
548
- )
549
- LOGGER.info(f"{m.from_user.id} dmuted {user_id} in {m.chat.id}")
550
- await m.reply_to_message.delete()
551
- admin = await mention_html(m.from_user.first_name, m.from_user.id)
552
- muted = await mention_html(user_first_name, user_id)
553
- txt = f"Admin {admin} muted {muted}!"
554
- if reason:
555
- txt += f"\n<b>Reason</b>: {reason}"
556
- else:
557
- txt += "\n<b>Reason</b>: Not Specified"
558
- keyboard = InlineKeyboardMarkup(
559
- [
560
- [
561
- InlineKeyboardButton(
562
- "UNMUTE",
563
- callback_data=f"unmute_={user_id}",
564
- ),
565
- ],
566
- ],
567
- )
568
- mutt = choice(MUTE_GIFS)
569
- try:
570
- await m.reply_animation(
571
- animation=str(mutt),
572
- caption=txt,
573
- reply_markup=keyboard,
574
  )
575
- except Exception:
576
- await m.reply_text(txt, reply_markup=keyboard)
577
- await c.send_message(MESSAGE_DUMP, f"#REMOVE from MUTE_GIFS\n{mutt}")
578
- except ChatAdminRequired:
579
- await m.reply_text(text="I'm not admin or I don't have rights.")
580
- except RightForbidden:
581
- await m.reply_text(text="I don't have enough rights to ban this user.")
582
- except UserNotParticipant:
583
- await m.reply_text("How can I mute a user who is not a part of this chat?")
584
- except RPCError as ef:
585
- await m.reply_text(
586
- text=f"""Some error occured, report it using `/bug`
587
-
588
- <b>Error:</b> <code>{ef}</code>"""
589
- )
590
- LOGGER.error(ef)
591
-
592
- return
593
-
594
-
595
- @app.on_message(command("unmute") & restrict_filter)
596
- async def unmute_usr(c: app, m: Message):
597
- if len(m.text.split()) == 1 and not m.reply_to_message:
598
- await m.reply_text("I can't unmute nothing!")
599
- return
600
-
601
- try:
602
- user_id, user_first_name, _ = await extract_user(c, m)
603
- except Exception:
604
- return
605
-
606
- if user_id == BOT_ID:
607
- await m.reply_text("Huh, why would I unmute myself if you are using me?")
608
- return
609
- try:
610
- statu = (await m.chat.get_member(user_id)).status
611
- if statu not in [
612
- enums.ChatMemberStatus.BANNED,
613
- enums.ChatMemberStatus.RESTRICTED,
614
- ]:
615
- await m.reply_text(
616
- "User is not muted in this chat\nOr using this command as reply to his message"
617
  )
618
- return
619
- except Exception as e:
620
- LOGGER.error(e)
621
- LOGGER.exception(format_exc())
622
- try:
623
- await m.chat.unban_member(user_id)
624
- LOGGER.info(f"{m.from_user.id} unmuted {user_id} in {m.chat.id}")
625
- admin = await mention_html(m.from_user.first_name, m.from_user.id)
626
- unmuted = await mention_html(user_first_name, user_id)
627
- await m.reply_text(text=f"Admin {admin} unmuted {unmuted}!")
628
- except ChatAdminRequired:
629
- await m.reply_text(text="I'm not admin or I don't have rights.")
630
- except UserNotParticipant:
631
- await m.reply_text("How can I unmute a user who is not a part of this chat?")
632
- except RightForbidden:
633
- await m.reply_text(text="I don't have enough rights to ban this user.")
634
- except RPCError as ef:
635
- await m.reply_text(
636
- text=f"""Some error occured, report it using `/bug`
637
-
638
- <b>Error:</b> <code>{ef}</code>"""
639
- )
640
- LOGGER.error(ef)
641
- return
642
-
643
-
644
- @app.on_callback_query(regex("^unmute_"))
645
- async def unmutebutton(c: app, q: CallbackQuery):
646
- splitter = (str(q.data).replace("unmute_", "")).split("=")
647
- user_id = int(splitter[1])
648
- user = await q.message.chat.get_member(q.from_user.id)
649
 
650
- if not user:
651
- await q.answer(
652
- "You don't have enough permission to do this!\nStay in your limits!",
653
- show_alert=True,
654
- )
655
- return
 
 
 
 
 
 
 
 
 
656
 
657
- if not user.privileges.can_restrict_members and user.id != OWNER_ID:
658
- await q.answer(
659
- "You don't have enough permission to do this!\nStay in your limits!",
660
- show_alert=True,
661
- )
662
- return
663
- whoo = await c.get_users(user_id)
664
- try:
665
- await q.message.chat.unban_member(user_id)
666
- except RPCError as e:
667
- await q.message.edit_text(f"Error: {e}")
668
- return
669
- await q.message.edit_text(f"{q.from_user.mention} unmuted {whoo.mention}!")
670
- return
671
 
672
 
673
  # <=================================================== HELP ====================================================>
674
 
675
 
676
  __help__ = """
677
- ➠ *Admin only:*
678
-
679
- » /mute: Mute the user replied to or mentioned.
680
-
681
- » /smute: silences a user without notifying. Can also be used as a reply, muting the replied to user.
682
-
683
- » /dmute: Mute a user by reply, and delete their message.
684
 
685
- » /tmute <userhandle> x(m/h/d): mutes a user for x time. (via handle, or reply). m = minutes, h = hours, d = days.
686
 
687
- » /stmute <userhandle> x(m/h/d): mutes a user for x time without notifying. (via handle, or reply). m = minutes, h = hours, d = days.
688
 
689
- » /dtmute <userhandle> x(m/h/d): Mute the replied user, and delete the replied message. (via reply). m = minutes, h = hours, d = days.
 
690
 
691
- » /unmute: Unmutes the user mentioned or replied to.
 
 
 
692
 
693
- ➠ *Example:*
694
- » `/mute @username`; this mutes a user."""
 
695
 
696
  __mod_name__ = "MUTE"
 
697
  # <================================================ END =======================================================>
 
1
  # <============================================== IMPORTS =========================================================>
2
+ import html
3
+ from typing import Union
4
+
5
+ from telegram import Bot, Chat, ChatMember, ChatPermissions, Update
6
+ from telegram.constants import ParseMode
7
+ from telegram.error import BadRequest
8
+ from telegram.ext import CommandHandler, ContextTypes
9
+ from telegram.helpers import mention_html
10
+
11
+ from Mikobot import LOGGER, function
12
+ from Mikobot.plugins.helper_funcs.chat_status import (
13
+ check_admin,
14
+ connection_status,
15
+ is_user_admin,
16
  )
17
+ from Mikobot.plugins.helper_funcs.extraction import extract_user, extract_user_and_text
18
+ from Mikobot.plugins.helper_funcs.string_handling import extract_time
19
+ from Mikobot.plugins.log_channel import loggable
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # <=======================================================================================================>
22
 
23
 
24
  # <================================================ FUNCTION =======================================================>
25
+ async def check_user(user_id: int, bot: Bot, chat: Chat) -> Union[str, None]:
 
 
 
 
 
 
 
 
 
 
26
  if not user_id:
27
+ reply = "You don't seem to be referring to a user or the ID specified is incorrect.."
28
+ return reply
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  try:
31
+ member = await chat.get_member(user_id)
32
+ except BadRequest as excp:
33
+ if excp.message == "User not found":
34
+ reply = "I can't seem to find this user"
35
+ return reply
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  else:
37
+ raise
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ if user_id == bot.id:
40
+ reply = "I'm not gonna MUTE myself, How high are you?"
41
+ return reply
 
 
42
 
43
+ if await is_user_admin(chat, user_id, member):
44
+ reply = "Sorry can't do that, this user is admin here."
45
+ return reply
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
 
 
49
 
50
+ @connection_status
51
+ @loggable
52
+ @check_admin(permission="can_restrict_members", is_both=True)
53
+ async def mute(update: Update, context: ContextTypes.DEFAULT_TYPE) -> str:
54
+ bot = context.bot
55
+ args = context.args
56
 
57
+ chat = update.effective_chat
58
+ user = update.effective_user
59
+ message = update.effective_message
60
 
61
+ user_id, reason = await extract_user_and_text(message, context, args)
62
+ reply = await check_user(user_id, bot, chat)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
+ if reply:
65
+ await message.reply_text(reply)
66
+ return ""
67
 
68
+ member = await chat.get_member(user_id)
69
 
70
+ log = (
71
+ f"<b>{html.escape(chat.title)}:</b>\n"
72
+ f"#MUTE\n"
73
+ f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n"
74
+ f"<b>User:</b> {mention_html(member.user.id, member.user.first_name)}"
75
+ )
76
 
77
+ if reason:
78
+ log += f"\n<b>Reason:</b> {reason}"
 
 
79
 
80
+ if member.status in [ChatMember.RESTRICTED, ChatMember.MEMBER]:
81
+ chat_permissions = ChatPermissions(can_send_messages=False)
82
+ await bot.restrict_chat_member(chat.id, user_id, chat_permissions)
83
+ await bot.sendMessage(
84
+ chat.id,
85
+ f"Muted <b>{html.escape(member.user.first_name)}</b> with no expiration date!",
86
+ parse_mode=ParseMode.HTML,
87
+ message_thread_id=message.message_thread_id if chat.is_forum else None,
 
 
 
 
 
88
  )
89
+ return log
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  else:
92
+ await message.reply_text("This user is already muted!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
96
 
97
+ @connection_status
98
+ @loggable
99
+ @check_admin(permission="can_restrict_members", is_both=True)
100
+ async def unmute(update: Update, context: ContextTypes.DEFAULT_TYPE) -> str:
101
+ bot, args = context.bot, context.args
102
+ chat = update.effective_chat
103
+ user = update.effective_user
104
+ message = update.effective_message
105
 
106
+ user_id = await extract_user(message, context, args)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  if not user_id:
108
+ await message.reply_text(
109
+ "You'll need to either give me a username to unmute, or reply to someone to be unmuted.",
 
 
 
 
 
 
 
110
  )
111
+ return ""
 
 
 
 
 
 
 
 
112
 
113
+ member = await chat.get_member(int(user_id))
 
 
114
 
115
+ if member.status not in [ChatMember.LEFT, ChatMember.BANNED]:
116
+ if member.status != ChatMember.RESTRICTED:
117
+ await message.reply_text("This user already has the right to speak.")
 
 
 
 
 
 
 
 
118
  else:
119
+ chat_permissions = ChatPermissions(
120
+ can_send_messages=True,
121
+ can_invite_users=True,
122
+ can_pin_messages=True,
123
+ can_send_polls=True,
124
+ can_change_info=True,
125
+ can_send_media_messages=True,
126
+ can_send_other_messages=True,
127
+ can_add_web_page_previews=True,
128
+ )
129
+ try:
130
+ await bot.restrict_chat_member(chat.id, int(user_id), chat_permissions)
131
+ except BadRequest:
132
+ pass
133
+ await bot.sendMessage(
134
+ chat.id,
135
+ f"I shall allow <b>{html.escape(member.user.first_name)}</b> to text!",
136
+ parse_mode=ParseMode.HTML,
137
+ message_thread_id=message.message_thread_id if chat.is_forum else None,
138
+ )
139
+ return (
140
+ f"<b>{html.escape(chat.title)}:</b>\n"
141
+ f"#UNMUTE\n"
142
+ f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n"
143
+ f"<b>User:</b> {mention_html(member.user.id, member.user.first_name)}"
144
  )
145
+ else:
146
+ await message.reply_text(
147
+ "This user isn't even in the chat, unmuting them won't make them talk more than they "
148
+ "already do!",
 
 
 
 
 
 
 
 
 
 
149
  )
 
150
 
151
+ return ""
152
 
153
 
154
+ @connection_status
155
+ @loggable
156
+ @check_admin(permission="can_restrict_members", is_both=True)
157
+ async def temp_mute(update: Update, context: ContextTypes.DEFAULT_TYPE) -> str:
158
+ bot, args = context.bot, context.args
159
+ chat = update.effective_chat
160
+ user = update.effective_user
161
+ message = update.effective_message
162
 
163
+ user_id, reason = await extract_user_and_text(message, context, args)
164
+ reply = await check_user(user_id, bot, chat)
 
 
165
 
166
+ if reply:
167
+ await message.reply_text(reply)
168
+ return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
+ member = await chat.get_member(user_id)
171
 
172
+ if not reason:
173
+ await message.reply_text("You haven't specified a time to mute this user for!")
174
+ return ""
175
 
176
+ split_reason = reason.split(None, 1)
 
 
 
 
 
 
177
 
178
+ time_val = split_reason[0].lower()
179
+ if len(split_reason) > 1:
180
+ reason = split_reason[1]
 
181
  else:
182
+ reason = ""
 
 
 
183
 
184
+ mutetime = await extract_time(message, time_val)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
+ if not mutetime:
187
+ return ""
188
+
189
+ log = (
190
+ f"<b>{html.escape(chat.title)}:</b>\n"
191
+ f"#TEMP MUTED\n"
192
+ f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n"
193
+ f"<b>User:</b> {mention_html(member.user.id, member.user.first_name)}\n"
194
+ f"<b>Time:</b> {time_val}"
195
+ )
196
+ if reason:
197
+ log += f"\n<b>Reason:</b> {reason}"
198
 
199
  try:
200
+ if member.status in [ChatMember.RESTRICTED, ChatMember.MEMBER]:
201
+ chat_permissions = ChatPermissions(can_send_messages=False)
202
+ await bot.restrict_chat_member(
203
+ chat.id,
204
+ user_id,
205
+ chat_permissions,
206
+ until_date=mutetime,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  )
208
+ await bot.sendMessage(
209
+ chat.id,
210
+ f"Muted <b>{html.escape(member.user.first_name)}</b> for {time_val}!",
211
+ parse_mode=ParseMode.HTML,
212
+ message_thread_id=message.message_thread_id if chat.is_forum else None,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  )
214
+ return log
215
+ else:
216
+ await message.reply_text("This user is already muted.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
 
218
+ except BadRequest as excp:
219
+ if excp.message == "Reply message not found":
220
+ # Do not reply
221
+ await message.reply_text(f"Muted for {time_val}!", quote=False)
222
+ return log
223
+ else:
224
+ LOGGER.warning(update)
225
+ LOGGER.exception(
226
+ "ERROR muting user %s in chat %s (%s) due to %s",
227
+ user_id,
228
+ chat.title,
229
+ chat.id,
230
+ excp.message,
231
+ )
232
+ await message.reply_text("Well damn, I can't mute that user.")
233
 
234
+ return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
 
237
  # <=================================================== HELP ====================================================>
238
 
239
 
240
  __help__ = """
241
+ ➠ *Admins only:*
 
 
 
 
 
 
242
 
243
+ » /mute <userhandle>: silences a user. Can also be used as a reply, muting the replied to user.
244
 
245
+ » /tmute <userhandle> x(m/h/d): mutes a user for x time. (via handle, or reply). `m` = `minutes`, `h` = `hours`, `d` = `days`.
246
 
247
+ » /unmute <userhandle>: unmutes a user. Can also be used as a reply, muting the replied to user.
248
+ """
249
 
250
+ # <================================================ HANDLER =======================================================>
251
+ MUTE_HANDLER = CommandHandler("mute", mute, block=False)
252
+ UNMUTE_HANDLER = CommandHandler("unmute", unmute, block=False)
253
+ TEMPMUTE_HANDLER = CommandHandler(["tmute", "tempmute"], temp_mute, block=False)
254
 
255
+ function(MUTE_HANDLER)
256
+ function(UNMUTE_HANDLER)
257
+ function(TEMPMUTE_HANDLER)
258
 
259
  __mod_name__ = "MUTE"
260
+ __handlers__ = [MUTE_HANDLER, UNMUTE_HANDLER, TEMPMUTE_HANDLER]
261
  # <================================================ END =======================================================>