File size: 3,847 Bytes
30a00bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from DragMusic import app
from pyrogram import filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, ChatPermissions

# A dictionary to store the message ID of the join request notification
join_request_message = {}

@app.on_chat_join_request()
async def D_A_X_X(_, message):
    try:
        chat = message.chat
        user = message.from_user
        
        # Check if the bot is an admin in the chat
        bot_member = await app.get_chat_member(chat.id, (await app.get_me()).id)
        if not bot_member.privileges.can_invite_users:
            # Silently ignore if the bot can't manage join requests
            return

        # Send join request notification to the group
        msg = await app.send_message(
            chat.id,
            f"""
ɴᴇᴡ ᴊᴏɪɴ ʀᴇǫᴜᴇsᴛ
            
ᴜsᴇʀ : {user.mention}
ᴜsᴇʀɴᴀᴍᴇ : @{user.username}
ᴜsᴇʀ ɪᴅ : {user.id}
            """,
            reply_markup=InlineKeyboardMarkup(
                [
                    [
                        InlineKeyboardButton(
                            "ᴀᴘᴘʀᴏᴠᴇ", callback_data=f"j_approve_{chat.id}_{user.id}"
                        ),
                        InlineKeyboardButton(
                            "ʀᴇᴊᴇᴄᴛ", callback_data=f"j_reject_{chat.id}_{user.id}"
                        ),
                    ]
                ]
            ),
        )
        join_request_message[user.id] = msg.id

    except Exception as e:
        print(f"Error in chat_join_request handler: {e}")

async def is_admin(chat_id, user_id):
    try:
        member = await app.get_chat_member(chat_id, user_id)
        return member.privileges is not None
    except Exception:
        return False

@app.on_callback_query(filters.regex("j_approve_(.*)"))
async def approve_request(_, query):
    try:
        chat_id, user_id = query.data.split("_")[2:]
        chat_id = int(chat_id)
        user_id = int(user_id)

        user_who_pressed = query.from_user

        if not await is_admin(chat_id, user_who_pressed.id):
            await query.answer("ᴛʜɪs ɪs ɴᴏᴛ ғᴏʀ ʏᴏᴜ.", show_alert=True)
            return

        await app.approve_chat_join_request(chat_id, user_id)
        
        original_message_id = join_request_message.pop(user_id, None)
        if original_message_id:
            original_message = await app.get_messages(chat_id, original_message_id)
            await original_message.edit_text(
                f"{original_message.text}\n\n**sᴛᴀᴛᴜs :** ᴀᴘᴘʀᴏᴠᴇᴅ ✅\n**ʙʏ :** {user_who_pressed.mention}"
            )
    except Exception as e:
        await query.answer(f"An error occurred: {e}", show_alert=True)
        print(f"Error in approve_request handler: {e}")

@app.on_callback_query(filters.regex("j_reject_(.*)"))
async def reject_request(_, query):
    try:
        chat_id, user_id = query.data.split("_")[2:]
        chat_id = int(chat_id)
        user_id = int(user_id)
        
        user_who_pressed = query.from_user

        if not await is_admin(chat_id, user_who_pressed.id):
            await query.answer("ᴛʜɪs ɪs ɴᴏᴛ ғᴏʀ ʏᴏᴜ.", show_alert=True)
            return

        await app.decline_chat_join_request(chat_id, user_id)

        original_message_id = join_request_message.pop(user_id, None)
        if original_message_id:
            original_message = await app.get_messages(chat_id, original_message_id)
            await original_message.edit_text(
                f"{original_message.text}\n\n**sᴛᴀᴛᴜs :** ʀᴇᴊᴇᴄᴛᴇᴅ ❌\n**ʙʏ :** {user_who_pressed.mention}"
            )
    except Exception as e:
        await query.answer(f"An error occurred: {e}", show_alert=True)
        print(f"Error in reject_request handler: {e}")