Просмотр исходного кода

Merge pull request #45 from sylphiette/dev

Fixed greeting module
TheHamkerCat 5 лет назад
Родитель
Сommit
534af8929e
1 измененных файлов с 33 добавлено и 29 удалено
  1. 33 29
      wbb/modules/greetings.py

+ 33 - 29
wbb/modules/greetings.py

@@ -15,50 +15,51 @@ from pyrogram import filters
 from wbb import app, WELCOME_DELAY_KICK_SEC
 from wbb import app, WELCOME_DELAY_KICK_SEC
 from wbb.utils.errors import capture_err
 from wbb.utils.errors import capture_err
 from wbb.modules.admin import list_admins
 from wbb.modules.admin import list_admins
-from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
-from pyrogram.types import ChatPermissions
+from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, ChatPermissions, User
 from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
 from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
 
 
 @app.on_message(filters.new_chat_members)
 @app.on_message(filters.new_chat_members)
 @capture_err
 @capture_err
 async def welcome(_, message: Message):
 async def welcome(_, message: Message):
     """Mute new member and send message with button"""
     """Mute new member and send message with button"""
-    new_members = [f"{u.mention}" for u in message.new_chat_members]
-    text = (f"Welcome, {', '.join(new_members)}\n**Are you human?**\n"
+    for member in message.new_chat_members:
+        if member.is_bot:
+            continue # ignore bots
+            
+        text = (f"Welcome, {(member.mention())}\n**Are you human?**\n"
             "You will be removed from this chat if you are not verified "
             "You will be removed from this chat if you are not verified "
             f"in {WELCOME_DELAY_KICK_SEC} seconds")
             f"in {WELCOME_DELAY_KICK_SEC} seconds")
-    if message.from_user.id in await list_admins(message.chat.id):
-        return
-    await message.chat.restrict_member(message.from_user.id, ChatPermissions())
+        await message.chat.restrict_member(member.id, ChatPermissions())
 
 
-    button_message = await message.reply(
-        text,
-        reply_markup=InlineKeyboardMarkup(
-            [
+        button_message = await message.reply(
+            text,
+            reply_markup=InlineKeyboardMarkup(
                 [
                 [
-                    InlineKeyboardButton(
-                        "Press Here to Verify",
-                        callback_data="pressed_button"
-                    )
+                    [
+                        InlineKeyboardButton(
+                            "Press Here to Verify",
+                            callback_data="pressed_button {}".format(member.id) # According to the docs, the user can send arbitrary data in this field, yet, I'm not able to do so, so until there's a db to pass data, imma use it like this so it will be very easy to change it to use a db in the future
+                        )
+                    ]
                 ]
                 ]
-            ]
-        ),
-        quote=True
-    )
-    await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA")
-    await kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message)
-
+            ),
+            quote=True
+        )
+        #await message.reply_animation("CgACAgQAAx0CWIlO9AABATjjYBrlKqQID0SQ7Ey7bkVRKM5gNn8AAl8CAAIm39VRHDx6EoKU6W0eBA") 
+        # wtf is this? "file_id is unique for each individual bot and can't be transferred from one bot to another." source: https://core.telegram.org/bots/api#sending-files
+        #and here is the error I just received -- pyrogram.errors.exceptions.bad_request_400.MediaEmpty: [400 MEDIA_EMPTY]: The media you tried to send is invalid (caused by "messages.SendMedia")
+        asyncio.create_task(kick_restricted_after_delay(WELCOME_DELAY_KICK_SEC, button_message, member))
+        await asyncio.sleep(0.5)
 
 
 @app.on_callback_query(filters.regex("pressed_button"))
 @app.on_callback_query(filters.regex("pressed_button"))
-async def callback_query_welcome_button(_, callback_query):
+async def callback_query_welcome_button(client, callback_query):
     """After the new member presses the button, set his permissions to
     """After the new member presses the button, set his permissions to
     chat permissions, delete button message and join message
     chat permissions, delete button message and join message
     """
     """
     button_message = callback_query.message
     button_message = callback_query.message
-    join_message = button_message.reply_to_message
-    pending_user = join_message.from_user
-    pending_user_id = pending_user.id
+    pending_user = await client.get_users(int(callback_query.data.replace('pressed_button ', '')))
     pressed_user_id = callback_query.from_user.id
     pressed_user_id = callback_query.from_user.id
+    pending_user_id = pending_user.id
     if pending_user_id == pressed_user_id:
     if pending_user_id == pressed_user_id:
         await callback_query.answer("Congrats, captcha passed!")
         await callback_query.answer("Congrats, captcha passed!")
         await button_message.chat.unban_member(pending_user_id)
         await button_message.chat.unban_member(pending_user_id)
@@ -66,15 +67,18 @@ async def callback_query_welcome_button(_, callback_query):
     else:
     else:
         await callback_query.answer("This is not for you")
         await callback_query.answer("This is not for you")
 
 
-
-async def kick_restricted_after_delay(delay, button_message: Message):
+# This code is really dumb
+# If anyone is interested, we should use threads and kill them when the user has verified himself.
+# Also, once there's a db, we should save this data and re-create the threads in case the bot crashed, tho pyrogram doesn't support history messages (obviously)
+# so in such a case we may lose data anyway
+async def kick_restricted_after_delay(delay, button_message: Message, user: User):
     """If the new member is still restricted after the delay, delete
     """If the new member is still restricted after the delay, delete
     button message and join message and then kick him
     button message and join message and then kick him
     """
     """
     await asyncio.sleep(delay)
     await asyncio.sleep(delay)
     join_message = button_message.reply_to_message
     join_message = button_message.reply_to_message
     group_chat = button_message.chat
     group_chat = button_message.chat
-    user_id = join_message.from_user.id
+    user_id = user.id
     await join_message.delete()
     await join_message.delete()
     await button_message.delete()
     await button_message.delete()
     await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)
     await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)