autoapprove.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. """
  2. MIT License
  3. Copyright (c) 2024 SI_NN_ER_LS
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. from pyrogram import filters
  21. from pyrogram.enums import ChatMembersFilter
  22. from pyrogram.types import (
  23. CallbackQuery,
  24. Chat,
  25. ChatJoinRequest,
  26. InlineKeyboardButton,
  27. InlineKeyboardMarkup,
  28. Message,
  29. )
  30. from wbb import SUDOERS, app, db
  31. from wbb.core.decorators.permissions import adminsOnly
  32. from wbb.core.keyboard import ikb
  33. from wbb.modules.admin import member_permissions
  34. from wbb.modules.greetings import handle_new_member, send_welcome_message
  35. approvaldb = db.autoapprove
  36. __MODULE__ = "Autoapprove"
  37. __HELP__ = """
  38. command: /autoapprove
  39. This module helps to automatically accept chat join request send by a user through invitation link of your group
  40. **Modes:**
  41. ¤ Automatic - Automatically accepts chat join request.
  42. ¤ Manual - A message will be send to the chat by tagging the admins. The admins can accept or decline the requests.
  43. Use: /clear_pending Command to remove all pending user ID from DB. This will allow the user to send request again.
  44. """
  45. @app.on_message(filters.command("autoapprove") & filters.group)
  46. @adminsOnly("can_change_info")
  47. async def approval_command(client, message):
  48. chat_id = message.chat.id
  49. chat = await approvaldb.find_one({"chat_id": chat_id})
  50. if chat:
  51. mode = chat.get("mode", "")
  52. if not mode:
  53. mode = "automatic"
  54. await approvaldb.update_one(
  55. {"chat_id": chat_id},
  56. {"$set": {"mode": mode}},
  57. upsert=True,
  58. )
  59. if mode == "automatic":
  60. switch = "manual"
  61. else:
  62. switch = "automatic"
  63. buttons = {
  64. "Turn OFF": "approval_off",
  65. f"{(mode.upper())}": f"approval_{switch}",
  66. }
  67. keyboard = ikb(buttons, 1)
  68. await message.reply(
  69. "**Autoapproval for this chat: Enabled.**", reply_markup=keyboard
  70. )
  71. else:
  72. buttons = {"Turn ON": "approval_on"}
  73. keyboard = ikb(buttons, 1)
  74. await message.reply(
  75. "**Autoapproval for this chat: Disabled.**", reply_markup=keyboard
  76. )
  77. @app.on_callback_query(filters.regex("approval(.*)"))
  78. async def approval_cb(client, cb):
  79. chat_id = cb.message.chat.id
  80. from_user = cb.from_user
  81. permissions = await member_permissions(chat_id, from_user.id)
  82. permission = "can_restrict_members"
  83. if permission not in permissions:
  84. if from_user.id not in SUDOERS:
  85. return await cb.answer(
  86. f"You don't have the required permission.\n Permission: {permission}",
  87. show_alert=True,
  88. )
  89. command_parts = cb.data.split("_", 1)
  90. option = command_parts[1]
  91. if option == "off":
  92. if await approvaldb.count_documents({"chat_id": chat_id}) > 0:
  93. approvaldb.delete_one({"chat_id": chat_id})
  94. buttons = {"Turn ON": "approval_on"}
  95. keyboard = ikb(buttons, 1)
  96. return await cb.edit_message_text(
  97. "**Autoapproval for this chat: Disabled.**",
  98. reply_markup=keyboard,
  99. )
  100. if option == "on":
  101. switch = "manual"
  102. mode = "automatic"
  103. if option == "automatic":
  104. switch = "manual"
  105. mode = option
  106. if option == "manual":
  107. switch = "automatic"
  108. mode = option
  109. await approvaldb.update_one(
  110. {"chat_id": chat_id},
  111. {"$set": {"mode": mode}},
  112. upsert=True,
  113. )
  114. chat = await approvaldb.find_one({"chat_id": chat_id})
  115. mode = chat["mode"].upper()
  116. buttons = {"Turn OFF": "approval_off", f"{mode}": f"approval_{switch}"}
  117. keyboard = ikb(buttons, 1)
  118. await cb.edit_message_text(
  119. "**Autoapproval for this chat: Enabled.**", reply_markup=keyboard
  120. )
  121. @app.on_message(filters.command("clear_pending") & filters.group)
  122. @adminsOnly("can_restrict_members")
  123. async def clear_pending_command(client, message):
  124. chat_id = message.chat.id
  125. result = await approvaldb.update_one(
  126. {"chat_id": chat_id},
  127. {"$set": {"pending_users": []}},
  128. )
  129. if result.modified_count > 0:
  130. await message.reply_text("Cleared pending users.")
  131. else:
  132. await message.reply_text("No pending users to clear.")
  133. @app.on_chat_join_request(filters.group)
  134. async def accept(client, message: ChatJoinRequest):
  135. chat = message.chat
  136. user = message.from_user
  137. chat_id = await approvaldb.find_one({"chat_id": chat.id})
  138. if chat_id:
  139. mode = chat_id["mode"]
  140. if mode == "automatic":
  141. await app.approve_chat_join_request(
  142. chat_id=chat.id, user_id=user.id
  143. )
  144. return await handle_new_member(user, chat)
  145. if mode == "manual":
  146. is_user_in_pending = await approvaldb.count_documents(
  147. {"chat_id": chat.id, "pending_users": int(user.id)}
  148. )
  149. if is_user_in_pending == 0:
  150. await approvaldb.update_one(
  151. {"chat_id": chat.id},
  152. {"$addToSet": {"pending_users": int(user.id)}},
  153. upsert=True,
  154. )
  155. buttons = {
  156. "accept": f"manual_approve_{user.id}",
  157. "Decline": f"manual_decline_{user.id}",
  158. }
  159. keyboard = ikb(buttons, int(2))
  160. text = f"**User: {user.mention} has send a request to join our group. Any admins can accept or decline it.**"
  161. admin_data = [
  162. i
  163. async for i in app.get_chat_members(
  164. chat_id=message.chat.id,
  165. filter=ChatMembersFilter.ADMINISTRATORS,
  166. )
  167. ]
  168. for admin in admin_data:
  169. if admin.user.is_bot or admin.user.is_deleted:
  170. continue
  171. text += f"[\u2063](tg://user?id={admin.user.id})"
  172. return await app.send_message(
  173. chat.id, text, reply_markup=keyboard
  174. )
  175. @app.on_callback_query(filters.regex("manual_(.*)"))
  176. async def manual(app, cb):
  177. chat = cb.message.chat
  178. from_user = cb.from_user
  179. permissions = await member_permissions(chat.id, from_user.id)
  180. permission = "can_restrict_members"
  181. if permission not in permissions:
  182. if from_user.id not in SUDOERS:
  183. return await cb.answer(
  184. f"You don't have the required permission.\n Permission: {permission}",
  185. show_alert=True,
  186. )
  187. datas = cb.data.split("_", 2)
  188. dis = datas[1]
  189. id = datas[2]
  190. if dis == "approve":
  191. await app.approve_chat_join_request(chat_id=chat.id, user_id=id)
  192. # No need to verify user as admin is the one who accept the request
  193. if dis == "decline":
  194. await app.decline_chat_join_request(chat_id=chat.id, user_id=id)
  195. await approvaldb.update_one(
  196. {"chat_id": chat.id},
  197. {"$pull": {"pending_users": int(id)}},
  198. )
  199. return await cb.message.delete()