autoapprove.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. """
  2. MIT License
  3. Copyright (c) 2023 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 wbb import app, SUDOERS
  21. from wbb import db
  22. from pyrogram import filters
  23. from pyrogram.enums import ChatMembersFilter
  24. from pyrogram.types import (
  25. InlineKeyboardButton,
  26. InlineKeyboardMarkup,
  27. CallbackQuery,
  28. Message,
  29. Chat,
  30. ChatJoinRequest,
  31. )
  32. from wbb.modules.greetings import handle_new_member, send_welcome_message
  33. from wbb.core.keyboard import ikb
  34. from wbb.core.decorators.permissions import adminsOnly
  35. from wbb.modules.admin import member_permissions
  36. approvaldb = db.autoapprove
  37. __MODULE__ = "Autoapprove"
  38. __HELP__ = """
  39. command: /autoapprove
  40. This module helps to automatically accept chat join request send by a user through invitation link of your group
  41. **Modes:**
  42. ¤ Automatic - Automatically accepts chat join request.
  43. ¤ Manual - A message will be send to the chat by tagging the admins. The admins can accept or decline the requests.
  44. Use: /clear_pending Command to remove all pending user ID from DB. This will allow the user to send request again.
  45. """
  46. @app.on_message(filters.command("autoapprove") & filters.group)
  47. @adminsOnly("can_change_info")
  48. async def approval_command(client, message):
  49. chat_id = message.chat.id
  50. chat = await approvaldb.find_one({"chat_id": chat_id})
  51. if chat:
  52. mode = chat.get("mode", "")
  53. if not mode:
  54. mode = "automatic"
  55. await approvaldb.update_one(
  56. {"chat_id": chat_id},
  57. {"$set": {"mode": mode}},
  58. upsert=True,
  59. )
  60. if mode == "automatic":
  61. switch = "manual"
  62. else:
  63. switch = "automatic"
  64. buttons = {"Turn OFF": "approval_off", f"{(mode.upper())}": f"approval_{switch}"}
  65. keyboard = ikb(buttons, 1)
  66. await message.reply(
  67. "**Autoapproval for this chat: Enabled.**", reply_markup=keyboard
  68. )
  69. else:
  70. buttons = {"Turn ON": "approval_on"}
  71. keyboard = ikb(buttons, 1)
  72. await message.reply(
  73. "**Autoapproval for this chat: Disabled.**", reply_markup=keyboard
  74. )
  75. @app.on_callback_query(filters.regex("approval(.*)"))
  76. async def approval_cb(client, cb):
  77. chat_id = cb.message.chat.id
  78. from_user = cb.from_user
  79. permissions = await member_permissions(chat_id, from_user.id)
  80. permission = "can_restrict_members"
  81. if permission not in permissions:
  82. if from_user.id not in SUDOERS:
  83. return await cb.answer(
  84. f"You don't have the required permission.\n Permission: {permission}",
  85. show_alert=True,
  86. )
  87. command_parts = cb.data.split("_", 1)
  88. option = command_parts[1]
  89. if option == "off":
  90. if await approvaldb.count_documents({"chat_id": chat_id}) > 0:
  91. approvaldb.delete_one({"chat_id": chat_id})
  92. buttons = {"Turn ON": "approval_on"}
  93. keyboard = ikb(buttons, 1)
  94. return await cb.edit_message_text(
  95. "**Autoapproval for this chat: Disabled.**", reply_markup=keyboard
  96. )
  97. if option == "on":
  98. switch = "manual"
  99. mode = "automatic"
  100. if option == "automatic":
  101. switch = "manual"
  102. mode = option
  103. if option == "manual":
  104. switch = "automatic"
  105. mode = option
  106. await approvaldb.update_one(
  107. {"chat_id": chat_id},
  108. {"$set": {"mode": mode}},
  109. upsert=True,
  110. )
  111. chat = await approvaldb.find_one({"chat_id": chat_id})
  112. mode = chat["mode"].upper()
  113. buttons = {"Turn OFF": "approval_off", f"{mode}": f"approval_{switch}"}
  114. keyboard = ikb(buttons, 1)
  115. await cb.edit_message_text(
  116. "**Autoapproval for this chat: Enabled.**", reply_markup=keyboard
  117. )
  118. @app.on_message(filters.command("clear_pending") & filters.group)
  119. @adminsOnly("can_restrict_members")
  120. async def clear_pending_command(client, message):
  121. chat_id = message.chat.id
  122. result = await approvaldb.update_one(
  123. {"chat_id": chat_id},
  124. {"$set": {"pending_users": []}},
  125. )
  126. if result.modified_count > 0:
  127. await message.reply_text("Cleared pending users.")
  128. else:
  129. await message.reply_text("No pending users to clear.")
  130. @app.on_chat_join_request(filters.group)
  131. async def accept(client, message: ChatJoinRequest):
  132. chat = message.chat
  133. user = message.from_user
  134. chat_id = await approvaldb.find_one({"chat_id": chat.id})
  135. if chat_id:
  136. mode = chat_id["mode"]
  137. if mode == "automatic":
  138. await app.approve_chat_join_request(chat_id=chat.id, user_id=user.id)
  139. return await handle_new_member(message, user, chat_id)
  140. if mode == "manual":
  141. is_user_in_pending = await approvaldb.count_documents(
  142. {"chat_id": chat.id, "pending_users": int(user.id)}
  143. )
  144. if is_user_in_pending == 0:
  145. await approvaldb.update_one(
  146. {"chat_id": chat.id},
  147. {"$addToSet": {"pending_users": int(user.id)}},
  148. upsert=True,
  149. )
  150. buttons = {
  151. "accept": f"manual_approve_{user.id}",
  152. "Decline": f"manual_decline_{user.id}",
  153. }
  154. keyboard = ikb(buttons, int(2))
  155. text = f"**User: {user.mention} has send a request to join our group. Any admins can accept or decline it.**"
  156. admin_data = [
  157. i
  158. async for i in app.get_chat_members(
  159. chat_id=message.chat.id, filter=ChatMembersFilter.ADMINISTRATORS
  160. )
  161. ]
  162. for admin in admin_data:
  163. if admin.user.is_bot or admin.user.is_deleted:
  164. continue
  165. text += f"[\u2063](tg://user?id={admin.user.id})"
  166. return await app.send_message(chat.id, text, reply_markup=keyboard)
  167. @app.on_callback_query(filters.regex("manual_(.*)"))
  168. async def manual(app, cb):
  169. chat = cb.message.chat
  170. from_user = cb.from_user
  171. permissions = await member_permissions(chat.id, from_user.id)
  172. permission = "can_restrict_members"
  173. if permission not in permissions:
  174. if from_user.id not in SUDOERS:
  175. return await cb.answer(
  176. f"You don't have the required permission.\n Permission: {permission}",
  177. show_alert=True,
  178. )
  179. datas = cb.data.split("_", 1)[1]
  180. data = datas.split("_", 1)
  181. dis = data[0]
  182. id = data[1]
  183. if dis == "approve":
  184. await app.approve_chat_join_request(chat_id=chat.id, user_id=id)
  185. # No need to verify user as admin is the one who accept the request
  186. await send_welcome_message(chat, id, True)
  187. if dis == "decline":
  188. await app.decline_chat_join_request(chat_id=chat.id, user_id=id)
  189. await approvaldb.update_one(
  190. {"chat_id": chat.id},
  191. {"$pull": {"pending_users": int(id)}},
  192. )
  193. return await cb.message.delete()