pmpermit.py 6.7 KB

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