Ufoptg commited on
Commit
88c612d
1 Parent(s): 7ec7f9a

Update code2.py

Browse files
Files changed (1) hide show
  1. code2.py +41 -53
code2.py CHANGED
@@ -7,7 +7,12 @@ from datetime import datetime, timedelta
7
  from telethon import TelegramClient, events
8
  from telethon.tl.custom import Button
9
  from telethon.tl.types import ChatAdminRights, ChatBannedRights, ChannelParticipantsAdmins
10
-
 
 
 
 
 
11
 
12
 
13
  API_ID = os.getenv("API_ID")
@@ -23,19 +28,10 @@ logger.info("Logging has been set up.")
23
  client = TelegramClient("TruesBlacklister", API_ID, API_HASH).start(bot_token=BOT_TOKEN)
24
  logger.info("Initialized Telethon client")
25
 
26
- # Define the list of blacklisted words
27
  blacklist = ["counterfeit", "meth", "shard", "heroin", "ghb"]
28
 
29
- @client.on(events.NewMessage(pattern='/start'))
30
- async def start(event):
31
- await event.respond('I am alive!')
32
-
33
- @client.on(events.NewMessage(pattern='/help'))
34
- async def help(event):
35
- await event.respond('I can blacklist certain words. Just type them in a chat I am in!')
36
-
37
- @client.on(events.NewMessage(incoming=True))
38
  async def check_blacklist(event):
 
39
  if any(
40
  re.search(rf"\b{re.escape(word)}\b", event.raw_text, re.IGNORECASE)
41
  for word in blacklist
@@ -44,17 +40,11 @@ async def check_blacklist(event):
44
 
45
  user = await event.get_sender()
46
  if user:
47
- logger.info(
48
- f"User id: {user.id}, username: {user.username}"
49
- )
50
 
51
  # Check if the user is an admin
52
- try:
53
- chat_admins = await client.get_participants(event.chat_id, filter=ChannelParticipantsAdmins)
54
- is_admin = any(admin.id == user.id for admin in chat_admins)
55
- except Exception as e:
56
- logger.error(f"Failed to check user admin status: {e}")
57
- is_admin = False
58
 
59
  if not is_admin:
60
  try:
@@ -62,21 +52,19 @@ async def check_blacklist(event):
62
  logger.info("User is not an admin. Deleted the message.")
63
 
64
  # Mute the user by revoking their ability to send messages
65
- await client.edit_permissions(
66
- entity=event.chat_id,
67
- user_id=user.id,
68
  send_messages=False
69
  )
70
  except Exception as e:
71
- logger.error("Failed to mute the user. Skipping action.")
72
 
73
  buttons = [
74
  [Button.inline("Unmute", b"unmute")],
75
  [Button.inline("Ban", b"ban")],
76
  ]
77
- await client.send_message(
78
- entity=event.chat_id,
79
- message=f"{user.username}, Your message has been deleted due to a blacklisted word. Please select an action:",
80
  buttons=buttons
81
  )
82
  else:
@@ -84,34 +72,34 @@ async def check_blacklist(event):
84
  else:
85
  logger.info("Message does not contain a blacklisted word.")
86
 
87
-
88
-
89
- @client.on(events.CallbackQuery())
90
- async def handle_button(event):
91
- user = await event.get_sender()
92
  try:
93
- if event.data.decode("utf-8") == "unmute":
94
- await client.edit_permissions(
95
- entity=event.chat_id,
96
- user_id=user.id,
97
- view_messages=True
98
- )
99
- await event.respond(f"{user.username} has been unmuted.")
100
- await asyncio.sleep(10) # Pause for 10 seconds
101
- await event.delete()
102
-
103
- elif event.data.decode("utf-8") == "ban":
104
- await client.edit_permissions(
105
- entity=event.chat_id,
106
- user_id=user.id,
107
- view_messages=False
108
- )
109
- await event.respond(f"{user.username} has been banned.")
110
- await asyncio.sleep(10) # Pause for 10 seconds
111
- await event.delete()
112
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  except Exception as e:
114
- logger.error(f"An error occurred: {e}")
115
 
116
- client.run_until_disconnected()
 
117
 
 
 
7
  from telethon import TelegramClient, events
8
  from telethon.tl.custom import Button
9
  from telethon.tl.types import ChatAdminRights, ChatBannedRights, ChannelParticipantsAdmins
10
+ import re
11
+ from telethon import events
12
+ from telethon.errors.rpcerrorlist import YouCannotBanYourselfError
13
+ from telethon.tl.functions.channels import EditBannedRequest
14
+ from telethon.tl.types import ChatBannedRights
15
+ from telethon.tl.types import MessageEntityMentionName
16
 
17
 
18
  API_ID = os.getenv("API_ID")
 
28
  client = TelegramClient("TruesBlacklister", API_ID, API_HASH).start(bot_token=BOT_TOKEN)
29
  logger.info("Initialized Telethon client")
30
 
 
31
  blacklist = ["counterfeit", "meth", "shard", "heroin", "ghb"]
32
 
 
 
 
 
 
 
 
 
 
33
  async def check_blacklist(event):
34
+
35
  if any(
36
  re.search(rf"\b{re.escape(word)}\b", event.raw_text, re.IGNORECASE)
37
  for word in blacklist
 
40
 
41
  user = await event.get_sender()
42
  if user:
43
+ logger.info(f"User id: {user.id}, username: {user.username}")
 
 
44
 
45
  # Check if the user is an admin
46
+ chat_admins = await event.get_chat_administrators()
47
+ is_admin = any(admin.user.id == user.id for admin in chat_admins)
 
 
 
 
48
 
49
  if not is_admin:
50
  try:
 
52
  logger.info("User is not an admin. Deleted the message.")
53
 
54
  # Mute the user by revoking their ability to send messages
55
+ await event.chat.edit_permissions(
56
+ user.id,
 
57
  send_messages=False
58
  )
59
  except Exception as e:
60
+ logger.error(f"Failed to mute the user: {e}")
61
 
62
  buttons = [
63
  [Button.inline("Unmute", b"unmute")],
64
  [Button.inline("Ban", b"ban")],
65
  ]
66
+ await event.respond(
67
+ f"{user.mention}, Your message has been deleted due to a blacklisted word. Please select an action:",
 
68
  buttons=buttons
69
  )
70
  else:
 
72
  else:
73
  logger.info("Message does not contain a blacklisted word.")
74
 
75
+ @client.on(events.CallbackQuery(data="unmute"))
76
+ async def unmute_user(event):
77
+ user_id = event.data_match.group(1)
 
 
78
  try:
79
+ await event.chat.edit_permissions(
80
+ entity=int(user_id),
81
+ send_messages=True
82
+ )
83
+ await event.respond("User has been unmuted.")
84
+ except Exception as e:
85
+ await event.respond(f"Failed to unmute user: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ @client.on(events.CallbackQuery(data="ban"))
88
+ async def ban_user(event):
89
+ user_id = event.data_match.group(1)
90
+ try:
91
+ await event.chat.edit_permissions(
92
+ entity=int(user_id),
93
+ send_messages=False,
94
+ until_date=None # Ban the user permanently
95
+ )
96
+ await event.respond("User has been banned.")
97
+ except YouCannotBanYourselfError:
98
+ await event.respond("You cannot ban yourself!")
99
  except Exception as e:
100
+ await event.respond(f"Failed to ban user: {e}")
101
 
102
+ # Register the check_blacklist function to listen for new messages
103
+ client.add_event_handler(check_blacklist, events.NewMessage(incoming=True))
104
 
105
+ client.run_until_disconnected()