pmpermit.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  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 wbb import BOT_ID, SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2
  22. from wbb.core.decorators.errors import capture_err
  23. from wbb.modules.userbot import edit_or_reply
  24. from wbb.utils.dbfunctions import (approve_pmpermit,
  25. disapprove_pmpermit,
  26. is_pmpermit_approved)
  27. flood = {}
  28. @app2.on_message(
  29. filters.private
  30. & filters.incoming
  31. & ~filters.service
  32. & ~filters.edited
  33. & ~filters.me
  34. & ~filters.bot
  35. & ~filters.via_bot
  36. & ~filters.user(SUDOERS)
  37. )
  38. @capture_err
  39. async def pmpermit_func(_, message):
  40. user_id = message.from_user.id
  41. if await is_pmpermit_approved(user_id):
  42. return
  43. async for m in app2.iter_history(user_id, limit=6):
  44. if m.reply_markup:
  45. await m.delete()
  46. if str(user_id) in flood:
  47. flood[str(user_id)] += 1
  48. else:
  49. flood[str(user_id)] = 1
  50. if flood[str(user_id)] > 5:
  51. await message.reply_text(
  52. "SPAM DETECTED, BLOCKED USER AUTOMATICALLY!"
  53. )
  54. return await app2.block_user(user_id)
  55. results = await app2.get_inline_bot_results(
  56. BOT_ID, f"pmpermit {user_id}"
  57. )
  58. await app2.send_inline_bot_result(
  59. user_id,
  60. results.query_id,
  61. results.results[0].id,
  62. hide_via=True,
  63. )
  64. @app2.on_message(
  65. filters.command("approve", prefixes=USERBOT_PREFIX)
  66. & filters.user(SUDOERS)
  67. & ~filters.via_bot
  68. )
  69. @capture_err
  70. async def pm_approve(_, message):
  71. if not message.reply_to_message:
  72. return await edit_or_reply(
  73. message, text="Reply to a user's message to approve."
  74. )
  75. user_id = message.reply_to_message.from_user.id
  76. if await is_pmpermit_approved(user_id):
  77. return await edit_or_reply(
  78. message, text="User is already approved to pm"
  79. )
  80. await approve_pmpermit(user_id)
  81. await edit_or_reply(message, text="User is approved to pm")
  82. @app2.on_message(
  83. filters.command("disapprove", prefixes=USERBOT_PREFIX)
  84. & filters.user(SUDOERS)
  85. & ~filters.via_bot
  86. )
  87. async def pm_disapprove(_, message):
  88. if not message.reply_to_message:
  89. return await edit_or_reply(
  90. message, text="Reply to a user's message to approve."
  91. )
  92. user_id = message.reply_to_message.from_user.id
  93. if not await is_pmpermit_approved(user_id):
  94. await edit_or_reply(
  95. message, text="User is already disapproved to pm"
  96. )
  97. async for m in app2.iter_history(user_id, limit=6):
  98. if m.reply_markup:
  99. try:
  100. await m.delete()
  101. except Exception:
  102. pass
  103. return
  104. await disapprove_pmpermit(user_id)
  105. await edit_or_reply(message, text="User is disapproved to pm")
  106. @app2.on_message(
  107. filters.command("block", prefixes=USERBOT_PREFIX)
  108. & filters.user(SUDOERS)
  109. & ~filters.via_bot
  110. )
  111. @capture_err
  112. async def block_user_func(_, message):
  113. if not message.reply_to_message:
  114. return await edit_or_reply(
  115. message, text="Reply to a user's message to block."
  116. )
  117. user_id = message.reply_to_message.from_user.id
  118. # Blocking user after editing the message so that other person can get the update.
  119. await edit_or_reply(message, text="Successfully blocked the user")
  120. await app2.block_user(user_id)
  121. @app2.on_message(
  122. filters.command("unblock", prefixes=USERBOT_PREFIX)
  123. & filters.user(SUDOERS)
  124. & ~filters.via_bot
  125. )
  126. async def unblock_user_func(_, message):
  127. if not message.reply_to_message:
  128. return await edit_or_reply(
  129. message, text="Reply to a user's message to unblock."
  130. )
  131. user_id = message.reply_to_message.from_user.id
  132. await app2.unblock_user(user_id)
  133. await edit_or_reply(
  134. message, text="Successfully Unblocked the user"
  135. )
  136. """ CALLBACK QUERY HANDLER """
  137. flood2 = {}
  138. @app.on_callback_query(filters.regex("pmpermit"))
  139. async def pmpermit_cq(_, cq):
  140. user_id = cq.from_user.id
  141. data, victim = (
  142. cq.data.split(None, 2)[1],
  143. cq.data.split(None, 2)[2],
  144. )
  145. if data == "approve":
  146. if user_id != USERBOT_ID:
  147. return await cq.answer("This Button Is Not For You")
  148. await approve_pmpermit(int(victim))
  149. return await app.edit_inline_text(
  150. cq.inline_message_id, "User Has Been Approved To PM."
  151. )
  152. if data == "block":
  153. if user_id != USERBOT_ID:
  154. return await cq.answer("This Button Is Not For You")
  155. await app.edit_inline_text(
  156. cq.inline_message_id, "Successfully blocked the user."
  157. )
  158. return await app2.block_user(int(victim))
  159. if user_id == USERBOT_ID:
  160. return await cq.answer("It's For The Other Person.")
  161. if data == "to_scam_you":
  162. async for m in app2.iter_history(user_id, limit=6):
  163. if m.reply_markup:
  164. await m.delete()
  165. await app2.send_message(
  166. user_id, "Blocked, Go scam someone else."
  167. )
  168. await app2.block_user(user_id)
  169. elif data == "approve_me":
  170. if str(user_id) in flood2:
  171. flood2[str(user_id)] += 1
  172. else:
  173. flood2[str(user_id)] = 1
  174. if flood2[str(user_id)] > 5:
  175. await app2.send_message(
  176. user_id, "SPAM DETECTED, USER BLOCKED."
  177. )
  178. return await app2.block_user(user_id)
  179. await app2.send_message(
  180. user_id,
  181. "I'm busy right now, will approve you shortly, DO NOT SPAM.",
  182. )