flash / Mikobot /plugins /antinsfw.py
Karma
Add files via upload
c7dfe8b
raw
history blame
5.6 kB
# <============================================== IMPORTS =========================================================>
from os import remove
from pyrogram import filters
from Database.mongodb.toggle_mongo import is_nsfw_on, nsfw_off, nsfw_on
from Mikobot import BOT_USERNAME, DRAGONS, app
from Mikobot.state import arq
from Mikobot.utils.can_restrict import can_restrict
from Mikobot.utils.errors import capture_err
# <=======================================================================================================>
# <================================================ FUNCTION =======================================================>
async def get_file_id_from_message(message):
file_id = None
if message.document:
if int(message.document.file_size) > 3145728:
return
mime_type = message.document.mime_type
if mime_type not in ("image/png", "image/jpeg"):
return
file_id = message.document.file_id
if message.sticker:
if message.sticker.is_animated:
if not message.sticker.thumbs:
return
file_id = message.sticker.thumbs[0].file_id
else:
file_id = message.sticker.file_id
if message.photo:
file_id = message.photo.file_id
if message.animation:
if not message.animation.thumbs:
return
file_id = message.animation.thumbs[0].file_id
if message.video:
if not message.video.thumbs:
return
file_id = message.video.thumbs[0].file_id
return file_id
@app.on_message(
(
filters.document
| filters.photo
| filters.sticker
| filters.animation
| filters.video
)
& ~filters.private,
group=8,
)
@capture_err
async def detect_nsfw(_, message):
if not await is_nsfw_on(message.chat.id):
return
if not message.from_user:
return
file_id = await get_file_id_from_message(message)
if not file_id:
return
file = await _.download_media(file_id)
try:
results = await arq.nsfw_scan(file=file)
except Exception:
return
if not results.ok:
return
results = results.result
remove(file)
nsfw = results.is_nsfw
if message.from_user.id in DRAGONS:
return
if not nsfw:
return
try:
await message.delete()
except Exception:
return
await message.reply_text(
f"""
**πŸ”ž NSFW Image Detected & Deleted Successfully!**
**βœͺ User:** {message.from_user.mention} [`{message.from_user.id}`]
**βœͺ Safe:** `{results.neutral} %`
**βœͺ Porn:** `{results.porn} %`
**βœͺ Adult:** `{results.sexy} %`
**βœͺ Hentai:** `{results.hentai} %`
**βœͺ Drawings:** `{results.drawings} %`
"""
)
@app.on_message(filters.command(["nsfwscan", f"nsfwscan@{BOT_USERNAME}"]))
@capture_err
async def nsfw_scan_command(_, message):
if not message.reply_to_message:
await message.reply_text(
"Reply to an image/document/sticker/animation to scan it."
)
return
reply = message.reply_to_message
if (
not reply.document
and not reply.photo
and not reply.sticker
and not reply.animation
and not reply.video
):
await message.reply_text(
"Reply to an image/document/sticker/animation to scan it."
)
return
m = await message.reply_text("Scanning")
file_id = await get_file_id_from_message(reply)
if not file_id:
return await m.edit("Something wrong happened.")
file = await _.download_media(file_id)
try:
results = await arq.nsfw_scan(file=file)
except Exception:
return
remove(file)
if not results.ok:
return await m.edit(results.result)
results = results.result
await m.edit(
f"""
**➒ Neutral:** `{results.neutral} %`
**➒ Porn:** `{results.porn} %`
**➒ Hentai:** `{results.hentai} %`
**➒ Sexy:** `{results.sexy} %`
**➒ Drawings:** `{results.drawings} %`
**➒ NSFW:** `{results.is_nsfw}`
"""
)
@app.on_message(
filters.command(["antinsfw", f"antinsfw@{BOT_USERNAME}"]) & ~filters.private
)
@can_restrict
async def nsfw_enable_disable(_, message):
if len(message.command) != 2:
await message.reply_text("Usage: /antinsfw [on/off]")
return
status = message.text.split(None, 1)[1].strip()
status = status.lower()
chat_id = message.chat.id
if status in ("on", "yes"):
if await is_nsfw_on(chat_id):
await message.reply_text("Antinsfw is already enabled.")
return
await nsfw_on(chat_id)
await message.reply_text(
"Enabled AntiNSFW System. I will Delete Messages Containing Inappropriate Content."
)
elif status in ("off", "no"):
if not await is_nsfw_on(chat_id):
await message.reply_text("Antinsfw is already disabled.")
return
await nsfw_off(chat_id)
await message.reply_text("Disabled AntiNSFW System.")
else:
await message.reply_text("Unknown Suffix, Use /antinsfw [on/off]")
# <=================================================== HELP ====================================================>
__mod_name__ = "ANTI-NSFW"
__help__ = """
*πŸ”ž Helps in detecting NSFW material and removing it*.
➠ *Usage:*
Β» /antinsfw [on/off]: Enables Anti-NSFW system.
Β» /nsfwscan <reply to message>: Scans the file replied to.
"""
# <================================================ END =======================================================>