File size: 3,517 Bytes
9513370
 
 
 
 
 
 
61e01ea
ecf02cb
88c612d
 
 
 
 
61e01ea
9513370
070fe77
 
 
9513370
 
 
 
 
 
 
 
 
 
7eff50c
9513370
 
88c612d
9513370
 
 
 
 
 
 
 
88c612d
9513370
7ec7f9a
e253896
 
8b0e484
 
9513370
 
 
7ec7f9a
 
ae016a6
 
88c612d
7ec7f9a
9513370
 
88c612d
9513370
 
61e01ea
 
9513370
88c612d
c0529a8
9513370
 
 
 
 
 
 
88c612d
 
 
9513370
ae016a6
 
88c612d
 
 
 
 
 
9513370
88c612d
 
 
 
ae016a6
 
88c612d
ae016a6
88c612d
 
9513370
88c612d
9513370
88c612d
 
61e01ea
88c612d
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
import asyncio
import logging
import os
import re
from datetime import datetime, timedelta

from telethon import TelegramClient, events
from telethon.tl.custom import Button
from telethon.tl.types import ChatAdminRights, ChatBannedRights, ChannelParticipantsAdmins
import re
from telethon import events
from telethon.tl.functions.channels import EditBannedRequest
from telethon.tl.types import ChatBannedRights
from telethon.tl.types import MessageEntityMentionName


API_ID = os.getenv("API_ID")
API_HASH = os.getenv("API_HASH")
BOT_TOKEN = os.getenv("BOT_TOKEN")

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("TruesAutoBlacklister")
logger.info("Logging has been set up.")

client = TelegramClient("TruesBlacklister", API_ID, API_HASH).start(bot_token=BOT_TOKEN)
logger.info("Initialized Telethon client")

blacklist = ["counterfeit", "meth", "shard", "heroin", "ghb"]

async def check_blacklist(event):

    if any(
        re.search(rf"\b{re.escape(word)}\b", event.raw_text, re.IGNORECASE)
        for word in blacklist
    ):
        logger.info("Message contains a blacklisted word.")

        user = await event.get_sender()
        if user:
            logger.info(f"User id: {user.id}, username: {user.username}")

        # Check if the user is an admin
        chat_admins = await client.get_participants(event.chat_id, filter=ChannelParticipantsAdmins)
        is_admin = any(admin.id == user.id for admin in chat_admins)

        if not is_admin:
            try:
                await event.delete()
                logger.info("User is not an admin. Deleted the message.")

                # Mute the user by revoking their ability to send messages
                await event.client.edit_permissions(
                    event.chat_id,
                    user.id,
                    send_messages=False
                )
            except Exception as e:
                logger.error(f"Failed to mute the user: {e}")

            buttons = [
                [Button.inline("Unmute", b"unmute")],
                [Button.inline("Ban", b"ban")],
            ]
            await event.respond(
                f"@{user.username}, Your message has been deleted due to a blacklisted word. Please select an action:",
                buttons=buttons
            )
        else:
            logger.info("User is an admin. Skipping action.")
    else:
        logger.info("Message does not contain a blacklisted word.")

@client.on(events.CallbackQuery(data="unmute"))
async def unmute_user(event):
    user_id = event.data_match.group(1)
    try:
        await event.client.edit_permissions(
            event.chat_id,
            entity=int(user_id),
            send_messages=True
        )
        await event.respond("User has been unmuted.")
    except Exception as e:
        await event.respond(f"Failed to unmute user: {e}")

@client.on(events.CallbackQuery(data="ban"))
async def ban_user(event):
    user_id = event.data_match.group(1)
    try:
        await event.client.edit_permissions(
            event.chat_id,
            entity=int(user_id),
            send_messages=False
        )
        await event.respond("User has been banned.")
    except Exception as e:
        await event.respond(f"Failed to ban user: {e}")

# Register the check_blacklist function to listen for new messages
client.add_event_handler(check_blacklist, events.NewMessage(incoming=True))

client.run_until_disconnected()