taslim19 commited on
Commit
b7445bc
·
1 Parent(s): 6aeada8

trying to fix admins can only use it

Browse files
Files changed (1) hide show
  1. DragMusic/plugins/plugins/nsfw.py +31 -15
DragMusic/plugins/plugins/nsfw.py CHANGED
@@ -5,6 +5,7 @@ from PIL import Image, UnidentifiedImageError
5
  import torch
6
  import torch.nn.functional as F
7
  from pyrogram import Client, filters
 
8
  from pyrogram.types import Message
9
  from transformers import AutoModelForImageClassification, ViTImageProcessor
10
  from DragMusic import app
@@ -35,48 +36,65 @@ 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
- # 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.")
 
55
 
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,7 +117,6 @@ async def scan_media(media_msg: Message, reply_msg: Message):
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(
@@ -121,7 +138,6 @@ async def scan_media(media_msg: Message, reply_msg: Message):
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()
 
5
  import torch
6
  import torch.nn.functional as F
7
  from pyrogram import Client, filters
8
+ from pyrogram.enums import ChatMemberStatus
9
  from pyrogram.types import Message
10
  from transformers import AutoModelForImageClassification, ViTImageProcessor
11
  from DragMusic import app
 
36
  async def disable_nsfw(chat_id: int):
37
  await nsfw_chats_collection.delete_one({"chat_id": chat_id})
38
 
39
+ # Admin check using official ChatMemberStatus
40
  async def is_admin(client: Client, chat_id: int, user_id: int) -> bool:
41
  try:
42
  member = await client.get_chat_member(chat_id, user_id)
43
+ print(f"[DEBUG] is_admin user_id={user_id}, status={member.status}")
44
+ return member.status in [ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER, "administrator", "creator"]
45
  except Exception as e:
46
+ print(f"[Admin Check Error] {e}")
47
  return False
48
 
49
+ # Command: /nsfw enable / disable
50
  @app.on_message(filters.command("nsfw") & filters.group)
51
  async def toggle_nsfw_scan(client: Client, message: Message):
52
  if len(message.command) != 2:
53
+ return await message.reply("Usage:\n`/nsfw enable`\n`/nsfw disable`", parse_mode="markdown")
54
 
55
+ user_is_admin = await is_admin(client, message.chat.id, message.from_user.id)
56
+ if not user_is_admin:
57
+ return await message.reply("❌ Only group admins or the group owner can use this command.")
58
 
59
  arg = message.command[1].lower()
60
  if arg == "enable":
61
  await enable_nsfw(message.chat.id)
62
+ await message.reply("✅ NSFW scanning has been *enabled* for this group.", parse_mode="markdown")
63
  elif arg == "disable":
64
  await disable_nsfw(message.chat.id)
65
+ await message.reply("🚫 NSFW scanning has been *disabled* for this group.", parse_mode="markdown")
66
  else:
67
+ await message.reply("⚠️ Invalid option. Use `/nsfw enable` or `/nsfw disable`", parse_mode="markdown")
68
 
69
+ # Debug command: /whoami
70
+ @app.on_message(filters.command("whoami") & filters.group)
71
+ async def whoami(client: Client, message: Message):
72
+ try:
73
+ member = await client.get_chat_member(message.chat.id, message.from_user.id)
74
+ await message.reply_text(
75
+ f"👤 Your user ID: `{message.from_user.id}`\n"
76
+ f"📢 Chat ID: `{message.chat.id}`\n"
77
+ f"🛡️ Your status: `{member.status}`",
78
+ quote=True,
79
+ parse_mode="markdown"
80
+ )
81
+ except Exception as e:
82
+ await message.reply_text(f"❌ Failed to fetch status: `{e}`", parse_mode="markdown")
83
+
84
+ # Command: /scan (manual)
85
  @app.on_message(filters.command("scan") & filters.reply)
86
  async def scan_command(_: Client, message: Message):
87
  if not message.reply_to_message:
88
  return await message.reply("Reply to an image, video, or sticker to scan.")
89
  await scan_media(message.reply_to_message, message)
90
 
91
+ # Auto scan media
92
  @app.on_message(filters.group & (filters.photo | filters.video | filters.sticker))
93
  async def auto_nsfw_scan(client: Client, message: Message):
94
  if await is_nsfw_enabled(message.chat.id):
95
  await scan_media(message, message)
96
 
97
+ # NSFW scan handler
98
  async def scan_media(media_msg: Message, reply_msg: Message):
99
  media = media_msg.photo or media_msg.video or media_msg.sticker
100
  path = None
 
117
  else:
118
  return await reply_msg.reply("Unable to download media.")
119
 
 
120
  if path.endswith((".webm", ".mp4", ".webp")):
121
  image_path = path + ".png"
122
  subprocess.run(
 
138
  confidence = round(probs[0][1].item() * 100, 2)
139
  label = "NSFW" if confidence > 50 else "SFW"
140
 
 
141
  if label == "NSFW":
142
  try:
143
  await media_msg.delete()