nsfw.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. from wbb import app, arq, IMGBB_API_KEY, MESSAGE_DUMP_CHAT
  2. from wbb.utils.filter_groups import nsfw_detect_group
  3. from wbb.utils.dbfunctions import is_nsfw_on, nsfw_on, nsfw_off
  4. from wbb.modules.admin import member_permissions
  5. from pyrogram import filters
  6. from random import randint
  7. import aiohttp
  8. import aiofiles
  9. import os
  10. @app.on_message((filters.document | filters.photo | filters.sticker) & ~filters.private, group=nsfw_detect_group)
  11. async def detect_nsfw(_, message):
  12. if message.document:
  13. if int(message.document.file_size) > 3145728:
  14. return
  15. mime_type = message.document.mime_type
  16. if mime_type != "image/png" and mime_type != "image/jpeg":
  17. return
  18. if not await is_nsfw_on(message.chat.id):
  19. return
  20. image = await message.download(f"{randint(6666, 9999)}.jpg")
  21. async with aiofiles.open(image, mode='rb') as f:
  22. payload = {
  23. "key": IMGBB_API_KEY,
  24. "image": await f.read(),
  25. "expiration": "60"
  26. }
  27. async with aiohttp.ClientSession() as session:
  28. async with session.post("https://api.imgbb.com/1/upload", data=payload) as resp:
  29. data = await resp.json()
  30. url = data['data']['url']
  31. os.remove(image)
  32. try:
  33. results = await arq.nsfw_scan(url)
  34. except Exception as e:
  35. print(e)
  36. return
  37. hentai = results.data.hentai
  38. sexy = results.data.sexy
  39. porn = results.data.porn
  40. neutral = results.data.neutral
  41. if hentai < 80 and porn < 70 and sexy < 95:
  42. return
  43. if neutral > 30:
  44. return
  45. user_mention = message.from_user.mention
  46. user_id = message.from_user.id
  47. m = await message.forward(MESSAGE_DUMP_CHAT)
  48. try:
  49. await message.delete()
  50. except Exception:
  51. pass
  52. await message.reply_text(f"""
  53. **NSFW [Image]({m.link}) Detected & Deleted Successfully!
  54. ————————————————————————**
  55. **User:** {user_mention} [`{user_id}`]
  56. **Safe:** `{neutral}` %
  57. **Porn:** `{porn}` %
  58. **Adult:** `{sexy}` %
  59. **Hentai:** `{hentai}` %
  60. """)
  61. @app.on_message(filters.command("nsfw_scan"))
  62. async def nsfw_scan_command(_, message):
  63. if not message.reply_to_message:
  64. await message.reply_text("Reply to an image or document to scan it.")
  65. return
  66. reply = message.reply_to_message
  67. if not reply.document and not reply.photo and not reply.sticker:
  68. await message.reply_text("Reply to an image/document/sticker to scan it.")
  69. return
  70. if message.reply_to_message.document:
  71. if int(message.reply_to_message.document.file_size) > 3145728:
  72. return
  73. mime_type = message.reply_to_message.document.mime_type
  74. if mime_type != "image/png" and mime_type != "image/jpeg":
  75. return
  76. m = await message.reply_text("Scanning")
  77. image = await message.reply_to_message.download(f"{randint(6666, 9999)}.jpg")
  78. async with aiofiles.open(image, mode='rb') as f:
  79. payload = {
  80. "key": IMGBB_API_KEY,
  81. "image": await f.read(),
  82. "expiration": "60"
  83. }
  84. async with aiohttp.ClientSession() as session:
  85. async with session.post("https://api.imgbb.com/1/upload", data=payload) as resp:
  86. data = await resp.json()
  87. url = data['data']['url']
  88. os.remove(image)
  89. try:
  90. results = await arq.nsfw_scan(url)
  91. except Exception as e:
  92. print(e)
  93. await m.edit(str(e))
  94. return
  95. hentai = results.data.hentai
  96. sexy = results.data.sexy
  97. porn = results.data.porn
  98. neutral = results.data.neutral
  99. await m.edit(f"""
  100. **Neutral:** `{neutral}`
  101. **Porn:** `{porn}`
  102. **Hentai:** `{hentai}`
  103. **sexy:** `{sexy}`
  104. """)
  105. @app.on_message(filters.command("anti_nsfw") & ~filters.private)
  106. async def nsfw_enable_disable(_, message):
  107. if len(message.command) != 2:
  108. await message.reply_text("Usage: /anti_nsfw [enable | disable]")
  109. return
  110. status = message.text.split(None, 1)[1].strip()
  111. status = status.lower()
  112. chat_id = message.chat.id
  113. user_id = message.from_user.id
  114. permissions = await member_permissions(chat_id, user_id)
  115. if "can_change_info" not in permissions:
  116. await message.reply_text("You don't have enough permissions.")
  117. return
  118. if status == "enable":
  119. await nsfw_on(chat_id)
  120. await message.reply_text("Enabled AntiNSFW System. I will Delete Messages Containing Inappropriate Content.")
  121. elif status == "disable":
  122. await nsfw_off(chat_id)
  123. await message.reply_text("Disabled AntiNSFW System.")
  124. else:
  125. await message.reply_text("Unknown Suffix, Use /anti_nsfw [enable|disable]")