File size: 4,901 Bytes
7a8e1f3
 
 
 
974d731
 
 
 
 
 
 
 
 
 
0f2302c
637d139
974d731
 
 
 
 
 
 
 
 
60469fc
 
974d731
 
 
 
 
 
 
c1b9c94
 
60469fc
 
 
 
 
974d731
f25b6b5
974d731
 
 
 
30768cc
 
 
 
974d731
 
 
 
 
 
 
 
 
30768cc
 
 
 
974d731
e9a5530
974d731
 
 
 
 
 
 
e9a5530
 
974d731
 
 
 
 
ce7437e
974d731
 
 
 
 
 
 
 
 
b34cf28
0da8bc5
974d731
 
 
 
 
 
 
 
30768cc
974d731
 
0f2302c
 
 
 
 
 
 
974d731
0f2302c
 
 
974d731
0f2302c
 
 
 
974d731
7c02a44
0f2302c
 
 
974d731
0f2302c
ce7437e
0f2302c
 
7a8e1f3
974d731
 
 
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
#By @TrueSaiyan
#Simply create a new file add the code to the file fill in the enviromental details(bot token etc) save the 
#file as a whateveryoulike.py then using python run the code

import asyncio
import logging
import os
import re
from datetime import datetime, timedelta

import pyrogram
from pyrogram import Client, filters
from pyrogram.enums import ChatMemberStatus
from pyrogram.types import ChatPermissions, InlineKeyboardButton, InlineKeyboardMarkup
from pyrogram.errors.exceptions.bad_request_400 import UserAdminInvalid
from telegramhandler import CustHand

API_ID = os.environ["API_ID"]
API_HASH = os.environ["API_HASH"]
BOT_TOKEN = os.environ["BOT_TOKEN"]

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("TruesAutoBlacklister")

logger.info("Server Side Logging has been set up.")

app = Client(
    "TruesBlacklister",
    api_id=int(API_ID), #Get API_ID from my.telegram.org
    api_hash=API_HASH, #As above
    bot_token=BOT_TOKEN, #Get bot_token from @botfather
)
logger.info("Initialized Pyrogram client 1")

telegram_handler = TelegramHandler(app, -1002032198491)
logger.addHandler(telegram_handler)
logger.info("Logging has been set up.")


# Define the list of blacklisted words
blacklist = ["counterfeit", "meth", "shard", "heroin", "ghb"]


@app.on_message(filters.text)
async def check_blacklist(client, message):
    if message is None or message.chat is None or message.from_user is None:
        logger.error("Received message or its attributes are None. Skipping processing.")
        return

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

        chat_member = await client.get_chat_member(
            message.chat.id, message.from_user.id
        )
        if chat_member is None or chat_member.user is None:
            logger.error("Chat member or user attributes are None. Skipping processing.")
            return

        logger.info(
            f"User status: {chat_member.status}, user id: {chat_member.user.id}, username: @{chat_member.user.username}"
        )

        if chat_member.status not in [
            ChatMemberStatus.ADMINISTRATOR,
            ChatMemberStatus.OWNER,
        ]:
            try:
                await message.delete()
                logger.info("User is not an admin. Deleted the message.")
                await client.restrict_chat_member(
                    chat_id=message.chat.id,
                    user_id=message.from_user.id,
                    permissions=ChatPermissions(can_send_messages=False),
                )
            except UserAdminInvalid as e:
                logger.error("Failed to mute the user. Skipping action.")

            buttons = [
                [InlineKeyboardButton("Unmute", callback_data="unmute")],
                [InlineKeyboardButton("Ban", callback_data="ban")],
            ]
            reply_markup = InlineKeyboardMarkup(buttons)
            await client.send_message(
                chat_id=message.chat.id,
                text=f"@{message.from_user.username}, Your message has been deleted due to a blacklisted word. Please select an action:",
                reply_to_message_id=message.id,
                reply_markup=reply_markup,
            )
        else:
            logger.info("User is an admin. Skipping action.")
    else:
        logger.info("Message does not contain a blacklisted word.")



@app.on_callback_query()
async def handle_button(client, callback_query):
    try:
        if callback_query.data == "unmute":
            await client.restrict_chat_member(
                chat_id=callback_query.message.chat.id,
                user_id=callback_query.from_user.id,
                permissions=ChatPermissions(can_send_messages=True),
            )

            await callback_query.message.edit_text(f"@{callback_query.from_user.username} has been unmuted.")
            await asyncio.sleep(10)  # Pause for 10 seconds
            await callback_query.message.delete()

        elif callback_query.data == "ban":
            await client.ban_chat_member(
                chat_id=callback_query.message.chat.id, user_id=callback_query.from_user.id
            )

            logger.info(f"@{callback_query.from_user.username} has banned themselves.")
            await callback_query.message.edit_text(f"@{callback_query.from_user.username} has been banned.")
            await asyncio.sleep(10)  # Pause for 10 seconds
            await callback_query.message.delete()

    except UserAdminInvalid as e:
#        logger.info(f"Caught UserAdminInvalid error: {e}")
        await callback_query.answer("This button is not for you!")
    except Exception as e:
        logger.error(f"An error occurred: {e}")


app.run()