nsfw.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from os import remove
  2. from pyrogram import filters
  3. from wbb import app, arq
  4. from wbb.core.decorators.errors import capture_err
  5. from wbb.core.decorators.permissions import adminsOnly
  6. from wbb.utils.dbfunctions import is_nsfw_on, nsfw_off, nsfw_on
  7. from wbb.utils.filter_groups import nsfw_detect_group
  8. __MODULE__ = "NSFW"
  9. __HELP__ = """
  10. /nsfw_scan - Manually Scan An Image/Sticker/Document.
  11. /anti_nsfw [ENABLE | DISABLE] - Turn This Module On/Off
  12. """
  13. async def get_file_id_from_message(message):
  14. file_id = None
  15. if message.document:
  16. if int(message.document.file_size) > 3145728:
  17. return
  18. mime_type = message.document.mime_type
  19. if mime_type != "image/png" and mime_type != "image/jpeg":
  20. return
  21. file_id = message.document.file_id
  22. if message.sticker:
  23. if message.sticker.is_animated:
  24. if not message.sticker.thumbs:
  25. return
  26. file_id = message.sticker.thumbs[0].file_id
  27. else:
  28. file_id = message.sticker.file_id
  29. if message.photo:
  30. file_id = message.photo.file_id
  31. if message.animation:
  32. if not message.animation.thumbs:
  33. return
  34. file_id = message.animation.thumbs[0].file_id
  35. if message.video:
  36. if not message.video.thumbs:
  37. return
  38. file_id = message.video.thumbs[0].file_id
  39. return file_id
  40. @app.on_message(
  41. (
  42. filters.document
  43. | filters.photo
  44. | filters.sticker
  45. | filters.animation
  46. | filters.video
  47. )
  48. & ~filters.private,
  49. group=nsfw_detect_group,
  50. )
  51. @capture_err
  52. async def detect_nsfw(_, message):
  53. if not await is_nsfw_on(message.chat.id):
  54. return
  55. if not message.from_user:
  56. return
  57. file_id = await get_file_id_from_message(message)
  58. if not file_id:
  59. return
  60. file = await app.download_media(file_id)
  61. try:
  62. results = await arq.nsfw_scan(file=file)
  63. except Exception:
  64. return
  65. if not results.ok:
  66. return
  67. results = results.result
  68. remove(file)
  69. nsfw = results.is_nsfw
  70. if not nsfw:
  71. return
  72. try:
  73. await message.delete()
  74. except Exception:
  75. return
  76. await message.reply_text(
  77. f"""
  78. **NSFW Image Detected & Deleted Successfully!
  79. ————————————————————————**
  80. **User:** {message.from_user.mention} [`{message.from_user.id}`]
  81. **Safe:** `{results.neutral} %`
  82. **Porn:** `{results.porn} %`
  83. **Adult:** `{results.sexy} %`
  84. **Hentai:** `{results.hentai} %`
  85. **Drawings:** `{results.drawings} %`
  86. **————————————————————————**
  87. __Use `/anti_nsfw disable` to disable this.__
  88. """
  89. )
  90. @app.on_message(filters.command("nsfw_scan"))
  91. @capture_err
  92. async def nsfw_scan_command(_, message):
  93. if not message.reply_to_message:
  94. await message.reply_text(
  95. "Reply to an image/document/sticker/animation to scan it."
  96. )
  97. return
  98. reply = message.reply_to_message
  99. if (
  100. not reply.document
  101. and not reply.photo
  102. and not reply.sticker
  103. and not reply.animation
  104. and not reply.video
  105. ):
  106. await message.reply_text(
  107. "Reply to an image/document/sticker/animation to scan it."
  108. )
  109. return
  110. m = await message.reply_text("Scanning")
  111. file_id = await get_file_id_from_message(reply)
  112. if not file_id:
  113. return await m.edit("Something wrong happened.")
  114. file = await app.download_media(file_id)
  115. try:
  116. results = await arq.nsfw_scan(file=file)
  117. except Exception:
  118. return
  119. remove(file)
  120. if not results.ok:
  121. return await m.edit(results.result)
  122. results = results.result
  123. await m.edit(
  124. f"""
  125. **Neutral:** `{results.neutral} %`
  126. **Porn:** `{results.porn} %`
  127. **Hentai:** `{results.hentai} %`
  128. **Sexy:** `{results.sexy} %`
  129. **Drawings:** `{results.drawings} %`
  130. **NSFW:** `{results.is_nsfw}`
  131. """
  132. )
  133. @app.on_message(filters.command("anti_nsfw") & ~filters.private)
  134. @adminsOnly("can_change_info")
  135. async def nsfw_enable_disable(_, message):
  136. if len(message.command) != 2:
  137. await message.reply_text(
  138. "Usage: /anti_nsfw [enable | disable]"
  139. )
  140. return
  141. status = message.text.split(None, 1)[1].strip()
  142. status = status.lower()
  143. chat_id = message.chat.id
  144. if status == "enable":
  145. await nsfw_on(chat_id)
  146. await message.reply_text(
  147. "Enabled AntiNSFW System. I will Delete Messages Containing Inappropriate Content."
  148. )
  149. elif status == "disable":
  150. await nsfw_off(chat_id)
  151. await message.reply_text("Disabled AntiNSFW System.")
  152. else:
  153. await message.reply_text(
  154. "Unknown Suffix, Use /anti_nsfw [enable|disable]"
  155. )