taslim19
commited on
Commit
·
6aeada8
1
Parent(s):
49650a3
fix: admins can use the nsfw and non admins can't use the nsfw enable anddisable and add inline button for kang.py
Browse files
DragMusic/plugins/management/kang.py
CHANGED
@@ -166,9 +166,12 @@ async def kang(client, message: Message):
|
|
166 |
break
|
167 |
|
168 |
await msg.edit(
|
169 |
-
f"Sticker Kanged To <a href='https://t.me/addstickers/{packname}'>Pack</a>\nEmoji: {sticker_emoji}",
|
170 |
disable_web_page_preview=True,
|
171 |
-
parse_mode=ParseMode.HTML
|
|
|
|
|
|
|
172 |
)
|
173 |
|
174 |
except (PeerIdInvalid, UserIsBlocked):
|
|
|
166 |
break
|
167 |
|
168 |
await msg.edit(
|
169 |
+
f"Sticker Kanged To <a href='https://t.me/addstickers/{packname}'>Sticker Pack</a>\nEmoji: {sticker_emoji}",
|
170 |
disable_web_page_preview=True,
|
171 |
+
parse_mode=ParseMode.HTML,
|
172 |
+
reply_markup=InlineKeyboardMarkup([
|
173 |
+
[InlineKeyboardButton("Sticker Pack", url=f"https://t.me/addstickers/{packname}")]
|
174 |
+
])
|
175 |
)
|
176 |
|
177 |
except (PeerIdInvalid, UserIsBlocked):
|
DragMusic/plugins/plugins/nsfw.py
CHANGED
@@ -9,22 +9,22 @@ from pyrogram.types import Message
|
|
9 |
from transformers import AutoModelForImageClassification, ViTImageProcessor
|
10 |
from DragMusic import app
|
11 |
from motor.motor_asyncio import AsyncIOMotorClient
|
12 |
-
from pyrogram.errors import PeerIdInvalid, UserNotParticipant
|
13 |
from config import MONGO_DB_URI
|
14 |
|
15 |
-
#
|
16 |
db_client = AsyncIOMotorClient(MONGO_DB_URI)
|
17 |
db = db_client['AnonXDatabase']
|
18 |
nsfw_chats_collection = db['nsfw_chats']
|
19 |
|
20 |
-
# Logger
|
21 |
LOGGER_ID = -1002471976725
|
22 |
LOGGER_TOPIC_ID = 6314
|
23 |
|
24 |
-
# Load
|
25 |
model = AutoModelForImageClassification.from_pretrained("Falconsai/nsfw_image_detection")
|
26 |
processor = ViTImageProcessor.from_pretrained("Falconsai/nsfw_image_detection")
|
27 |
|
|
|
28 |
async def is_nsfw_enabled(chat_id: int) -> bool:
|
29 |
chat = await nsfw_chats_collection.find_one({"chat_id": chat_id})
|
30 |
return bool(chat)
|
@@ -35,18 +35,20 @@ async def enable_nsfw(chat_id: int):
|
|
35 |
async def disable_nsfw(chat_id: int):
|
36 |
await nsfw_chats_collection.delete_one({"chat_id": chat_id})
|
37 |
|
38 |
-
#
|
39 |
-
async def is_admin(client, chat_id, user_id):
|
40 |
try:
|
41 |
member = await client.get_chat_member(chat_id, user_id)
|
42 |
-
return member.status in ["administrator", "
|
43 |
-
except:
|
|
|
44 |
return False
|
45 |
|
|
|
46 |
@app.on_message(filters.command("nsfw") & filters.group)
|
47 |
-
async def toggle_nsfw_scan(client, message: Message):
|
48 |
if len(message.command) != 2:
|
49 |
-
return await message.reply("Usage:
|
50 |
|
51 |
if not await is_admin(client, message.chat.id, message.from_user.id):
|
52 |
return await message.reply("Only group admins and the group owner can use this command.")
|
@@ -54,24 +56,27 @@ async def toggle_nsfw_scan(client, message: Message):
|
|
54 |
arg = message.command[1].lower()
|
55 |
if arg == "enable":
|
56 |
await enable_nsfw(message.chat.id)
|
57 |
-
await message.reply("Enabled NSFW scanning for this group.")
|
58 |
elif arg == "disable":
|
59 |
await disable_nsfw(message.chat.id)
|
60 |
-
await message.reply("Disabled NSFW scanning for this group.")
|
61 |
else:
|
62 |
-
await message.reply("Invalid argument. Use
|
63 |
-
|
|
|
64 |
@app.on_message(filters.command("scan") & filters.reply)
|
65 |
-
async def scan_command(_, message: Message):
|
66 |
if not message.reply_to_message:
|
67 |
return await message.reply("Reply to an image, video, or sticker to scan.")
|
68 |
await scan_media(message.reply_to_message, message)
|
69 |
|
|
|
70 |
@app.on_message(filters.group & (filters.photo | filters.video | filters.sticker))
|
71 |
async def auto_nsfw_scan(client: Client, message: Message):
|
72 |
if await is_nsfw_enabled(message.chat.id):
|
73 |
await scan_media(message, message)
|
74 |
|
|
|
75 |
async def scan_media(media_msg: Message, reply_msg: Message):
|
76 |
media = media_msg.photo or media_msg.video or media_msg.sticker
|
77 |
path = None
|
@@ -94,10 +99,13 @@ async def scan_media(media_msg: Message, reply_msg: Message):
|
|
94 |
else:
|
95 |
return await reply_msg.reply("Unable to download media.")
|
96 |
|
|
|
97 |
if path.endswith((".webm", ".mp4", ".webp")):
|
98 |
image_path = path + ".png"
|
99 |
-
subprocess.run(
|
100 |
-
|
|
|
|
|
101 |
if not os.path.exists(image_path) or os.path.getsize(image_path) == 0:
|
102 |
return await reply_msg.reply("Failed to extract image frame.")
|
103 |
img = Image.open(image_path).convert("RGB")
|
@@ -113,33 +121,33 @@ async def scan_media(media_msg: Message, reply_msg: Message):
|
|
113 |
confidence = round(probs[0][1].item() * 100, 2)
|
114 |
label = "NSFW" if confidence > 50 else "SFW"
|
115 |
|
|
|
116 |
if label == "NSFW":
|
117 |
try:
|
118 |
await media_msg.delete()
|
119 |
except Exception as e:
|
120 |
print(f"[NSFW] Failed to delete message: {e}")
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
129 |
)
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
message_thread_id=LOGGER_TOPIC_ID
|
135 |
-
)
|
136 |
-
except Exception as log_err:
|
137 |
-
print(f"[NSFW Logger Error] {log_err}")
|
138 |
-
elif confidence > 50:
|
139 |
-
await reply_msg.reply(f"Scan Result: {label} ({confidence}%)")
|
140 |
|
141 |
except UnidentifiedImageError:
|
142 |
-
print(f"[NSFW Detection Error]
|
143 |
await reply_msg.reply("Failed to read image for scanning.")
|
144 |
except Exception as e:
|
145 |
print(f"[NSFW Detection Error] {e}")
|
@@ -148,4 +156,4 @@ async def scan_media(media_msg: Message, reply_msg: Message):
|
|
148 |
if path and os.path.exists(path):
|
149 |
os.remove(path)
|
150 |
if image_path and os.path.exists(image_path):
|
151 |
-
os.remove(image_path)
|
|
|
9 |
from transformers import AutoModelForImageClassification, ViTImageProcessor
|
10 |
from DragMusic import app
|
11 |
from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
12 |
from config import MONGO_DB_URI
|
13 |
|
14 |
+
# MongoDB setup
|
15 |
db_client = AsyncIOMotorClient(MONGO_DB_URI)
|
16 |
db = db_client['AnonXDatabase']
|
17 |
nsfw_chats_collection = db['nsfw_chats']
|
18 |
|
19 |
+
# Logger configuration
|
20 |
LOGGER_ID = -1002471976725
|
21 |
LOGGER_TOPIC_ID = 6314
|
22 |
|
23 |
+
# Load the NSFW detection model
|
24 |
model = AutoModelForImageClassification.from_pretrained("Falconsai/nsfw_image_detection")
|
25 |
processor = ViTImageProcessor.from_pretrained("Falconsai/nsfw_image_detection")
|
26 |
|
27 |
+
# Check if NSFW scanning is enabled for a group
|
28 |
async def is_nsfw_enabled(chat_id: int) -> bool:
|
29 |
chat = await nsfw_chats_collection.find_one({"chat_id": chat_id})
|
30 |
return bool(chat)
|
|
|
35 |
async def disable_nsfw(chat_id: int):
|
36 |
await nsfw_chats_collection.delete_one({"chat_id": chat_id})
|
37 |
|
38 |
+
# Admin check function
|
39 |
+
async def is_admin(client: Client, chat_id: int, user_id: int) -> bool:
|
40 |
try:
|
41 |
member = await client.get_chat_member(chat_id, user_id)
|
42 |
+
return member.status in ["administrator", "creator"]
|
43 |
+
except Exception as e:
|
44 |
+
print(f"[is_admin] Error: {e}")
|
45 |
return False
|
46 |
|
47 |
+
# Command to enable/disable NSFW scanning
|
48 |
@app.on_message(filters.command("nsfw") & filters.group)
|
49 |
+
async def toggle_nsfw_scan(client: Client, message: Message):
|
50 |
if len(message.command) != 2:
|
51 |
+
return await message.reply("Usage: `/nsfw enable` or `/nsfw disable`", parse_mode="markdown")
|
52 |
|
53 |
if not await is_admin(client, message.chat.id, message.from_user.id):
|
54 |
return await message.reply("Only group admins and the group owner can use this command.")
|
|
|
56 |
arg = message.command[1].lower()
|
57 |
if arg == "enable":
|
58 |
await enable_nsfw(message.chat.id)
|
59 |
+
await message.reply("✅ Enabled NSFW scanning for this group.")
|
60 |
elif arg == "disable":
|
61 |
await disable_nsfw(message.chat.id)
|
62 |
+
await message.reply("❌ Disabled NSFW scanning for this group.")
|
63 |
else:
|
64 |
+
await message.reply("Invalid argument. Use `/nsfw enable` or `/nsfw disable`.", parse_mode="markdown")
|
65 |
+
|
66 |
+
# Command to manually scan a media reply
|
67 |
@app.on_message(filters.command("scan") & filters.reply)
|
68 |
+
async def scan_command(_: Client, message: Message):
|
69 |
if not message.reply_to_message:
|
70 |
return await message.reply("Reply to an image, video, or sticker to scan.")
|
71 |
await scan_media(message.reply_to_message, message)
|
72 |
|
73 |
+
# Auto-scan media when enabled
|
74 |
@app.on_message(filters.group & (filters.photo | filters.video | filters.sticker))
|
75 |
async def auto_nsfw_scan(client: Client, message: Message):
|
76 |
if await is_nsfw_enabled(message.chat.id):
|
77 |
await scan_media(message, message)
|
78 |
|
79 |
+
# Main media scanner
|
80 |
async def scan_media(media_msg: Message, reply_msg: Message):
|
81 |
media = media_msg.photo or media_msg.video or media_msg.sticker
|
82 |
path = None
|
|
|
99 |
else:
|
100 |
return await reply_msg.reply("Unable to download media.")
|
101 |
|
102 |
+
# Convert to image if needed
|
103 |
if path.endswith((".webm", ".mp4", ".webp")):
|
104 |
image_path = path + ".png"
|
105 |
+
subprocess.run(
|
106 |
+
["ffmpeg", "-i", path, "-frames:v", "1", image_path, "-y"],
|
107 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
108 |
+
)
|
109 |
if not os.path.exists(image_path) or os.path.getsize(image_path) == 0:
|
110 |
return await reply_msg.reply("Failed to extract image frame.")
|
111 |
img = Image.open(image_path).convert("RGB")
|
|
|
121 |
confidence = round(probs[0][1].item() * 100, 2)
|
122 |
label = "NSFW" if confidence > 50 else "SFW"
|
123 |
|
124 |
+
# Handle results
|
125 |
if label == "NSFW":
|
126 |
try:
|
127 |
await media_msg.delete()
|
128 |
except Exception as e:
|
129 |
print(f"[NSFW] Failed to delete message: {e}")
|
130 |
|
131 |
+
await reply_msg.reply(f"🚫 NSFW Detected ({confidence}%) — Message deleted.")
|
132 |
+
log_text = (
|
133 |
+
f"🚨 NSFW Content Detected\n"
|
134 |
+
f"Chat: {reply_msg.chat.title} (`{reply_msg.chat.id}`)\n"
|
135 |
+
f"User: {media_msg.from_user.mention if media_msg.from_user else 'Unknown'}\n"
|
136 |
+
f"Confidence: {confidence}%"
|
137 |
+
)
|
138 |
+
try:
|
139 |
+
await app.send_message(
|
140 |
+
chat_id=LOGGER_ID,
|
141 |
+
text=log_text,
|
142 |
+
message_thread_id=LOGGER_TOPIC_ID
|
143 |
)
|
144 |
+
except Exception as log_err:
|
145 |
+
print(f"[NSFW Logger Error] {log_err}")
|
146 |
+
else:
|
147 |
+
await reply_msg.reply(f"✅ Scan Result: {label} ({confidence}%)")
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
except UnidentifiedImageError:
|
150 |
+
print(f"[NSFW Detection Error] Cannot identify image file '{path or image_path}'")
|
151 |
await reply_msg.reply("Failed to read image for scanning.")
|
152 |
except Exception as e:
|
153 |
print(f"[NSFW Detection Error] {e}")
|
|
|
156 |
if path and os.path.exists(path):
|
157 |
os.remove(path)
|
158 |
if image_path and os.path.exists(image_path):
|
159 |
+
os.remove(image_path)
|