nsfw.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 wbb.core.decorators.errors import capture_err
  6. from pyrogram import filters
  7. from random import randint
  8. import aiohttp
  9. import aiofiles
  10. import os
  11. __MODULE__ = "NSFW"
  12. __HELP__ = """
  13. /nsfw_scan - Manually Scan An Image/Sticker/Document.
  14. /anti_nsfw [ENABLE | DISABLE] - Turn This Module On/Off
  15. """
  16. def calculate_horny(hentai_value, neutral_value, porn_value, sexy_value):
  17. weight_hentai = 0.18
  18. weight_neutral = 0.32
  19. weight_sexy = 0.22
  20. weight_porn = 0.28
  21. prominent = max (hentai_value, porn_value, sexy_value)
  22. if neutral < prominent:
  23. factor_calculation = weight_hentai * hentai_value - weight_neutral * neutral_value + weight_sexy * sexy_value + weight_porn * porn_value
  24. horny_factor = ((1 - factor_calculation/prominent) * 100)
  25. return horny_factor
  26. else:
  27. return -1 * neutral_value
  28. @app.on_message((filters.document | filters.photo | filters.sticker) & ~filters.private, group=nsfw_detect_group)
  29. @capture_err
  30. async def detect_nsfw(_, message):
  31. if message.document:
  32. if int(message.document.file_size) > 3145728:
  33. return
  34. mime_type = message.document.mime_type
  35. if mime_type != "image/png" and mime_type != "image/jpeg":
  36. return
  37. if not await is_nsfw_on(message.chat.id):
  38. return
  39. image = await message.download(f"{randint(6666, 9999)}.jpg")
  40. async with aiofiles.open(image, mode='rb') as f:
  41. payload = {
  42. "key": IMGBB_API_KEY,
  43. "image": await f.read(),
  44. "expiration": "60"
  45. }
  46. async with aiohttp.ClientSession() as session:
  47. async with session.post("https://api.imgbb.com/1/upload", data=payload) as resp:
  48. data = await resp.json()
  49. try:
  50. url = data['data']['url']
  51. except KeyError:
  52. return
  53. os.remove(image)
  54. try:
  55. results = await arq.nsfw_scan(url)
  56. except Exception as e:
  57. print(e)
  58. return
  59. hentai = results.data.hentai
  60. sexy = results.data.sexy
  61. porn = results.data.porn
  62. drawings = results.data.drawings
  63. neutral = results.data.neutral
  64. hornyfactor = calculate_horny(hentai,neutral,porn,sexy)
  65. if neutral >= 25:
  66. return
  67. if hornyfactor >= 70:
  68. pass
  69. elif hornyfactor >= 50:
  70. # The reason its like this is because later on the plan is to
  71. # Allow the moderator to decide the maximum allowed nsfw content
  72. pass
  73. elif hornyfactor >= 30:
  74. # This should be okay but still returning True
  75. # For now only strict checking is implemented
  76. pass
  77. else:
  78. return
  79. user_mention = message.from_user.mention
  80. user_id = message.from_user.id
  81. m = await message.forward(MESSAGE_DUMP_CHAT)
  82. try:
  83. await message.delete()
  84. except Exception:
  85. pass
  86. await message.reply_text(f"""
  87. **NSFW [Image]({m.link}) Detected & Deleted Successfully!
  88. ————————————————————————**
  89. **User:** {user_mention} [`{user_id}`]
  90. **Safe:** `{neutral} %`
  91. **Porn:** `{porn} %`
  92. **Adult:** `{sexy} %`
  93. **Hentai:** `{hentai} %`
  94. **Drawings:** `{drawings} %`
  95. **————————————————————————**
  96. __Use '/anti_nsfw disable' to disable this.__
  97. """)
  98. @app.on_message(filters.command("nsfw_scan"))
  99. @capture_err
  100. async def nsfw_scan_command(_, message):
  101. if not message.reply_to_message:
  102. await message.reply_text("Reply to an image or document to scan it.")
  103. return
  104. reply = message.reply_to_message
  105. if not reply.document and not reply.photo and not reply.sticker:
  106. await message.reply_text("Reply to an image/document/sticker to scan it.")
  107. return
  108. if message.reply_to_message.document:
  109. if int(message.reply_to_message.document.file_size) > 3145728:
  110. return
  111. mime_type = message.reply_to_message.document.mime_type
  112. if mime_type != "image/png" and mime_type != "image/jpeg":
  113. return
  114. m = await message.reply_text("Scanning")
  115. image = await message.reply_to_message.download(f"{randint(6666, 9999)}.jpg")
  116. async with aiofiles.open(image, mode='rb') as f:
  117. payload = {
  118. "key": IMGBB_API_KEY,
  119. "image": await f.read(),
  120. "expiration": "60"
  121. }
  122. async with aiohttp.ClientSession() as session:
  123. async with session.post("https://api.imgbb.com/1/upload", data=payload) as resp:
  124. data = await resp.json()
  125. try:
  126. url = data['data']['url']
  127. except KeyError:
  128. await m.edit("Failed to upload this to api server.")
  129. os.remove(image)
  130. try:
  131. results = await arq.nsfw_scan(url)
  132. except Exception as e:
  133. print(e)
  134. await m.edit(str(e))
  135. return
  136. hentai = results.data.hentai
  137. sexy = results.data.sexy
  138. porn = results.data.porn
  139. drawings = results.data.drawings
  140. neutral = results.data.neutral
  141. await m.edit(f"""
  142. **Neutral:** `{neutral} %`
  143. **Porn:** `{porn} %`
  144. **Hentai:** `{hentai} %`
  145. **Sexy:** `{sexy} %`
  146. **Drawings:** `{drawings} %`
  147. """)
  148. @app.on_message(filters.command("anti_nsfw") & ~filters.private)
  149. @capture_err
  150. async def nsfw_enable_disable(_, message):
  151. if len(message.command) != 2:
  152. await message.reply_text("Usage: /anti_nsfw [enable | disable]")
  153. return
  154. status = message.text.split(None, 1)[1].strip()
  155. status = status.lower()
  156. chat_id = message.chat.id
  157. user_id = message.from_user.id
  158. permissions = await member_permissions(chat_id, user_id)
  159. if "can_change_info" not in permissions:
  160. await message.reply_text("You don't have enough permissions.")
  161. return
  162. if status == "enable":
  163. await nsfw_on(chat_id)
  164. await message.reply_text("Enabled AntiNSFW System. I will Delete Messages Containing Inappropriate Content.")
  165. elif status == "disable":
  166. await nsfw_off(chat_id)
  167. await message.reply_text("Disabled AntiNSFW System.")
  168. else:
  169. await message.reply_text("Unknown Suffix, Use /anti_nsfw [enable|disable]")