Spaces:
Paused
Paused
taslim19
commited on
Commit
·
cfd8710
1
Parent(s):
eb85327
feat: Add comprehensive moderation plugin
Browse files
DragMusic/plugins/management/bans.py
ADDED
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
from pyrogram import filters
|
4 |
+
from pyrogram.errors import PeerIdInvalid
|
5 |
+
from pyrogram.types import Message, ChatPermissions
|
6 |
+
|
7 |
+
from DragMusic import app
|
8 |
+
from DragMusic.utils.decorators.admins import AdminActual
|
9 |
+
from DragMusic.utils.formatters import parse_time
|
10 |
+
|
11 |
+
|
12 |
+
# A helper function to extract user and time from a message
|
13 |
+
async def extract_user_and_time(message: Message, client):
|
14 |
+
user = None
|
15 |
+
time_val = None
|
16 |
+
|
17 |
+
if message.reply_to_message:
|
18 |
+
user = message.reply_to_message.from_user
|
19 |
+
if len(message.command) > 1:
|
20 |
+
time_val = message.command[1]
|
21 |
+
elif len(message.command) > 1:
|
22 |
+
identifier = message.command[1]
|
23 |
+
try:
|
24 |
+
user = await client.get_users(int(identifier))
|
25 |
+
except (ValueError, PeerIdInvalid):
|
26 |
+
try:
|
27 |
+
user = await client.get_users(identifier)
|
28 |
+
except PeerIdInvalid:
|
29 |
+
await message.reply_text("Invalid user ID or username.")
|
30 |
+
return None, None
|
31 |
+
|
32 |
+
if len(message.command) > 2:
|
33 |
+
time_val = message.command[2]
|
34 |
+
else:
|
35 |
+
await message.reply_text("Reply to a user or provide a username/user ID.")
|
36 |
+
return None, None
|
37 |
+
|
38 |
+
return user, time_val
|
39 |
+
|
40 |
+
@app.on_message(filters.command("ban") & filters.group)
|
41 |
+
@AdminActual
|
42 |
+
async def ban_command(client, message: Message, _):
|
43 |
+
target_user, _ = await extract_user_and_time(message, client)
|
44 |
+
if not target_user:
|
45 |
+
return
|
46 |
+
|
47 |
+
try:
|
48 |
+
await client.ban_chat_member(message.chat.id, target_user.id)
|
49 |
+
await message.reply_text(f"Banned {target_user.mention}.")
|
50 |
+
except Exception as e:
|
51 |
+
await message.reply_text(f"Failed to ban user. Error: {e}")
|
52 |
+
|
53 |
+
@app.on_message(filters.command("tban") & filters.group)
|
54 |
+
@AdminActual
|
55 |
+
async def tban_command(client, message: Message, _):
|
56 |
+
target_user, time_val = await extract_user_and_time(message, client)
|
57 |
+
if not target_user:
|
58 |
+
return
|
59 |
+
|
60 |
+
if not time_val:
|
61 |
+
return await message.reply_text("Usage: /tban <user> <duration> (e.g., 1h, 3d).")
|
62 |
+
|
63 |
+
duration = parse_time(time_val)
|
64 |
+
if not duration:
|
65 |
+
return await message.reply_text("Invalid time format. Use d, h, m, s (e.g., 3d, 10m).")
|
66 |
+
|
67 |
+
try:
|
68 |
+
until_date = datetime.now() + duration
|
69 |
+
await client.ban_chat_member(message.chat.id, target_user.id, until_date=until_date)
|
70 |
+
await message.reply_text(f"Banned {target_user.mention} for {time_val}.")
|
71 |
+
except Exception as e:
|
72 |
+
await message.reply_text(f"Failed to ban user. Error: {e}")
|
73 |
+
|
74 |
+
@app.on_message(filters.command("unban") & filters.group)
|
75 |
+
@AdminActual
|
76 |
+
async def unban_command(client, message: Message, _):
|
77 |
+
target_user, _ = await extract_user_and_time(message, client)
|
78 |
+
if not target_user:
|
79 |
+
return
|
80 |
+
|
81 |
+
try:
|
82 |
+
await client.unban_chat_member(message.chat.id, target_user.id)
|
83 |
+
await message.reply_text(f"Unbanned {target_user.mention}.")
|
84 |
+
except Exception as e:
|
85 |
+
await message.reply_text(f"Failed to unban user. Error: {e}")
|
86 |
+
|
87 |
+
@app.on_message(filters.command("mute") & filters.group)
|
88 |
+
@AdminActual
|
89 |
+
async def mute_command(client, message: Message, _):
|
90 |
+
target_user, _ = await extract_user_and_time(message, client)
|
91 |
+
if not target_user:
|
92 |
+
return
|
93 |
+
|
94 |
+
try:
|
95 |
+
await client.restrict_chat_member(message.chat.id, target_user.id, ChatPermissions())
|
96 |
+
await message.reply_text(f"Muted {target_user.mention}.")
|
97 |
+
except Exception as e:
|
98 |
+
await message.reply_text(f"Failed to mute user. Error: {e}")
|
99 |
+
|
100 |
+
@app.on_message(filters.command("tmute") & filters.group)
|
101 |
+
@AdminActual
|
102 |
+
async def tmute_command(client, message: Message, _):
|
103 |
+
target_user, time_val = await extract_user_and_time(message, client)
|
104 |
+
if not target_user:
|
105 |
+
return
|
106 |
+
|
107 |
+
if not time_val:
|
108 |
+
return await message.reply_text("Usage: /tmute <user> <duration> (e.g., 1h, 3d).")
|
109 |
+
|
110 |
+
duration = parse_time(time_val)
|
111 |
+
if not duration:
|
112 |
+
return await message.reply_text("Invalid time format. Use d, h, m, s (e.g., 3d, 10m).")
|
113 |
+
|
114 |
+
try:
|
115 |
+
until_date = datetime.now() + duration
|
116 |
+
await client.restrict_chat_member(message.chat.id, target_user.id, ChatPermissions(), until_date=until_date)
|
117 |
+
await message.reply_text(f"Muted {target_user.mention} for {time_val}.")
|
118 |
+
except Exception as e:
|
119 |
+
await message.reply_text(f"Failed to mute user. Error: {e}")
|
120 |
+
|
121 |
+
@app.on_message(filters.command("unmute") & filters.group)
|
122 |
+
@AdminActual
|
123 |
+
async def unmute_command(client, message: Message, _):
|
124 |
+
target_user, _ = await extract_user_and_time(message, client)
|
125 |
+
if not target_user:
|
126 |
+
return
|
127 |
+
|
128 |
+
try:
|
129 |
+
await client.unban_chat_member(message.chat.id, target_user.id) # Un-restricting is done with unban_chat_member
|
130 |
+
await message.reply_text(f"Unmuted {target_user.mention}.")
|
131 |
+
except Exception as e:
|
132 |
+
await message.reply_text(f"Failed to unmute user. Error: {e}")
|
133 |
+
|
134 |
+
@app.on_message(filters.command("kick") & filters.group)
|
135 |
+
@AdminActual
|
136 |
+
async def kick_command(client, message: Message, _):
|
137 |
+
target_user, _ = await extract_user_and_time(message, client)
|
138 |
+
if not target_user:
|
139 |
+
return
|
140 |
+
|
141 |
+
try:
|
142 |
+
await client.ban_chat_member(message.chat.id, target_user.id)
|
143 |
+
await client.unban_chat_member(message.chat.id, target_user.id)
|
144 |
+
await message.reply_text(f"Kicked {target_user.mention}.")
|
145 |
+
except Exception as e:
|
146 |
+
await message.reply_text(f"Failed to kick user. Error: {e}")
|
147 |
+
|
148 |
+
@app.on_message(filters.command("kickme") & filters.group)
|
149 |
+
async def kickme_command(client, message: Message):
|
150 |
+
try:
|
151 |
+
await client.ban_chat_member(message.chat.id, message.from_user.id)
|
152 |
+
await client.unban_chat_member(message.chat.id, message.from_user.id)
|
153 |
+
await message.reply_text("As you wish.")
|
154 |
+
except Exception as e:
|
155 |
+
await message.reply_text(f"I can't kick you. Maybe I'm not an admin? Error: {e}")
|
156 |
+
|
157 |
+
# Delete variants
|
158 |
+
@app.on_message(filters.command(["dban", "sban", "dmute", "smute", "dkick", "skick"]) & filters.group)
|
159 |
+
@AdminActual
|
160 |
+
async def action_variants(client, message: Message, _):
|
161 |
+
command = message.command[0].lower()
|
162 |
+
|
163 |
+
# Silent commands should only work via reply
|
164 |
+
if command.startswith('s') and not message.reply_to_message:
|
165 |
+
target_user, time_val = await extract_user_and_time(message, client)
|
166 |
+
else:
|
167 |
+
target_user, time_val = await extract_user_and_time(message, client)
|
168 |
+
|
169 |
+
if not target_user:
|
170 |
+
return
|
171 |
+
|
172 |
+
# Delete the command message for silent actions
|
173 |
+
if command.startswith('s'):
|
174 |
+
try:
|
175 |
+
await message.delete()
|
176 |
+
except Exception:
|
177 |
+
pass # Ignore if it fails
|
178 |
+
|
179 |
+
# Delete the replied-to message for 'd' actions
|
180 |
+
if command.startswith('d') and message.reply_to_message:
|
181 |
+
try:
|
182 |
+
await message.reply_to_message.delete()
|
183 |
+
except Exception:
|
184 |
+
pass # Ignore if it fails
|
185 |
+
|
186 |
+
action_text = ""
|
187 |
+
# Perform the action
|
188 |
+
try:
|
189 |
+
if 'ban' in command:
|
190 |
+
duration = parse_time(time_val) if time_val else None
|
191 |
+
until_date = datetime.now() + duration if duration else None
|
192 |
+
await client.ban_chat_member(message.chat.id, target_user.id, until_date=until_date)
|
193 |
+
action_text = f"Banned {target_user.mention}"
|
194 |
+
if time_val:
|
195 |
+
action_text += f" for {time_val}"
|
196 |
+
action_text += "."
|
197 |
+
elif 'mute' in command:
|
198 |
+
duration = parse_time(time_val) if time_val else None
|
199 |
+
until_date = datetime.now() + duration if duration else None
|
200 |
+
await client.restrict_chat_member(message.chat.id, target_user.id, ChatPermissions(), until_date=until_date)
|
201 |
+
action_text = f"Muted {target_user.mention}"
|
202 |
+
if time_val:
|
203 |
+
action_text += f" for {time_val}"
|
204 |
+
action_text += "."
|
205 |
+
elif 'kick' in command:
|
206 |
+
await client.ban_chat_member(message.chat.id, target_user.id)
|
207 |
+
await client.unban_chat_member(message.chat.id, target_user.id)
|
208 |
+
action_text = f"Kicked {target_user.mention}."
|
209 |
+
|
210 |
+
# Send confirmation only for 'd' commands, as 's' commands are silent
|
211 |
+
if command.startswith('d'):
|
212 |
+
await message.reply_text(action_text)
|
213 |
+
|
214 |
+
except Exception as e:
|
215 |
+
if not command.startswith('s'): # Don't send error for silent commands
|
216 |
+
await message.reply_text(f"Failed to perform action. Error: {e}")
|