Captain Ezio commited on
Commit
1fbd78c
·
1 Parent(s): 183e3a8

Looks good

Browse files
Files changed (1) hide show
  1. Powers/plugins/flood.py +157 -13
Powers/plugins/flood.py CHANGED
@@ -1,18 +1,23 @@
1
  import time
 
 
2
 
3
  from pyrogram import filters
4
  from pyrogram.enums import ChatMemberStatus as CMS
5
  from pyrogram.enums import ChatType as CT
6
- from pyrogram.errors import RPCError
7
- from pyrogram.types import (CallbackQuery, InlineKeyboardButton,
8
- InlineKeyboardMarkup, Message)
 
9
 
10
- from Powers import SUPPORT_STAFF
11
  from Powers.bot_class import Gojo
12
  from Powers.database.approve_db import Approve
13
  from Powers.database.flood_db import Floods
14
  from Powers.utils.custom_filters import admin_filter, command
 
15
  from Powers.utils.kbhelpers import ikb
 
16
 
17
  Flood = Floods()
18
 
@@ -91,6 +96,10 @@ limit_kb = InlineKeyboardMarkup(
91
 
92
  @Gojo.on_message(command(['floodaction','actionflood']) & admin_filter)
93
  async def flood_action(c: Gojo, m: Message):
 
 
 
 
94
  if m.chat.type == CT.PRIVATE:
95
  await m.reply_text("Use this command in group")
96
  return
@@ -108,6 +117,10 @@ async def flood_action(c: Gojo, m: Message):
108
 
109
  @Gojo.on_message(command(['setflood', 'flood']) & ~filters.bot & admin_filter)
110
  async def flood_set(c: Gojo, m: Message):
 
 
 
 
111
  if m.chat.type == CT.PRIVATE:
112
  return await m.reply_text("This command is ment to be used in groups.")
113
  split = m.text.split(None, 1)
@@ -139,8 +152,8 @@ async def flood_set(c: Gojo, m: Message):
139
  async def callbacks(c: Gojo, q: CallbackQuery):
140
  data = q.data
141
  if data == "close":
 
142
  await q.message.delete()
143
- q.answer("Closed")
144
  return
145
  c_id = q.message.chat.id
146
  is_flood = Flood.is_chat(c_id)
@@ -153,7 +166,7 @@ async def callbacks(c: Gojo, q: CallbackQuery):
153
  if data in ["mute", "ban", "kick"]:
154
  Flood.save_flood(c_id, slimit, swithin, data)
155
  await q.answer("Updated action", show_alert=True)
156
- q.edit_message_caption(
157
  f"Set the limit of message after the flood protection will be activated\n **CURRENT LIMIT** {slimit} messages",
158
  reply_markup=limit_kb
159
  )
@@ -162,7 +175,7 @@ async def callbacks(c: Gojo, q: CallbackQuery):
162
  change = int(data.split("_")[1])
163
  Flood.save_flood(c_id, change, swithin, saction)
164
  await q.answer("Updated limit", show_alert=True)
165
- q.edit_message_caption(
166
  f"Set the time with the number of message recived treated as flood\n **CUURENT TIME** {swithin}",
167
  reply_markup=within_kb
168
  )
@@ -171,7 +184,7 @@ async def callbacks(c: Gojo, q: CallbackQuery):
171
  change = int(data)
172
  Flood.save_flood(c_id, slimit, change, saction)
173
  await q.answer("Updated", show_alert=True)
174
- q.edit_message_caption(
175
  "Flood protection setting has been updated",
176
  reply_markup=close_kb
177
  )
@@ -181,12 +194,13 @@ async def callbacks(c: Gojo, q: CallbackQuery):
181
  "You don't have enough permission to do this!\nStay in your limits!",
182
  show_alert=True,
183
  )
 
184
 
185
  @Gojo.on_callback_query(filters.regex("^un_"))
186
  async def reverse_callbacks(c: Gojo, q: CallbackQuery):
187
  data = q.data.split("_")
188
  action = data[1]
189
- user_id = int(data[2])
190
  if action == "ban":
191
  user = await q.message.chat.get_member(q.from_user.id)
192
  if not user.privileges.can_restrict_members and q.from_user.id in SUPPORT_STAFF:
@@ -208,7 +222,7 @@ async def reverse_callbacks(c: Gojo, q: CallbackQuery):
208
  if action == "mute":
209
  user = await q.message.chat.get_member(q.from_user.id)
210
 
211
- if not user.privileges.can_restrict_members and user.id in SUPPORT_STAFF:
212
  await q.answer(
213
  "You don't have enough permission to do this!\nStay in your limits!",
214
  show_alert=True,
@@ -242,7 +256,137 @@ async def flood_watcher(c: Gojo, m: Message):
242
  dic = {}
243
  for i in str(dic.keys()):
244
  if str(c_id) != i:
245
- z = {c_id : set()}
246
  dic.update(z)
247
- dic[c_id].add(u_id)
248
- # to be continued
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import time
2
+ from random import choice
3
+ from traceback import format_exc
4
 
5
  from pyrogram import filters
6
  from pyrogram.enums import ChatMemberStatus as CMS
7
  from pyrogram.enums import ChatType as CT
8
+ from pyrogram.errors import RPCError, UserAdminInvalid
9
+ from pyrogram.types import (CallbackQuery, ChatPermissions,
10
+ InlineKeyboardButton, InlineKeyboardMarkup,
11
+ Message)
12
 
13
+ from Powers import LOGGER, SUPPORT_GROUP, SUPPORT_STAFF
14
  from Powers.bot_class import Gojo
15
  from Powers.database.approve_db import Approve
16
  from Powers.database.flood_db import Floods
17
  from Powers.utils.custom_filters import admin_filter, command
18
+ from Powers.utils.extras import BAN_GIFS, KICK_GIFS, MUTE_GIFS
19
  from Powers.utils.kbhelpers import ikb
20
+ from Powers.vars import Config
21
 
22
  Flood = Floods()
23
 
 
96
 
97
  @Gojo.on_message(command(['floodaction','actionflood']) & admin_filter)
98
  async def flood_action(c: Gojo, m: Message):
99
+ bot = await c.get_chat_member(m.chat.id, Config.BOT_ID)
100
+ status = bot.status
101
+ if not status in [CMS.OWNER, CMS.ADMINISTRATOR] and not bot.privileges.can_restrict_members:
102
+ return await m.reply_text("Give me permission to restict member first")
103
  if m.chat.type == CT.PRIVATE:
104
  await m.reply_text("Use this command in group")
105
  return
 
117
 
118
  @Gojo.on_message(command(['setflood', 'flood']) & ~filters.bot & admin_filter)
119
  async def flood_set(c: Gojo, m: Message):
120
+ bot = await c.get_chat_member(m.chat.id, Config.BOT_ID)
121
+ status = bot.status
122
+ if not status in [CMS.OWNER, CMS.ADMINISTRATOR] and not bot.privileges.can_restrict_members:
123
+ return await m.reply_text("Give me permission to restict member first")
124
  if m.chat.type == CT.PRIVATE:
125
  return await m.reply_text("This command is ment to be used in groups.")
126
  split = m.text.split(None, 1)
 
152
  async def callbacks(c: Gojo, q: CallbackQuery):
153
  data = q.data
154
  if data == "close":
155
+ await q.answer("Closed")
156
  await q.message.delete()
 
157
  return
158
  c_id = q.message.chat.id
159
  is_flood = Flood.is_chat(c_id)
 
166
  if data in ["mute", "ban", "kick"]:
167
  Flood.save_flood(c_id, slimit, swithin, data)
168
  await q.answer("Updated action", show_alert=True)
169
+ await q.edit_message_caption(
170
  f"Set the limit of message after the flood protection will be activated\n **CURRENT LIMIT** {slimit} messages",
171
  reply_markup=limit_kb
172
  )
 
175
  change = int(data.split("_")[1])
176
  Flood.save_flood(c_id, change, swithin, saction)
177
  await q.answer("Updated limit", show_alert=True)
178
+ await q.edit_message_caption(
179
  f"Set the time with the number of message recived treated as flood\n **CUURENT TIME** {swithin}",
180
  reply_markup=within_kb
181
  )
 
184
  change = int(data)
185
  Flood.save_flood(c_id, slimit, change, saction)
186
  await q.answer("Updated", show_alert=True)
187
+ await q.edit_message_caption(
188
  "Flood protection setting has been updated",
189
  reply_markup=close_kb
190
  )
 
194
  "You don't have enough permission to do this!\nStay in your limits!",
195
  show_alert=True,
196
  )
197
+ return
198
 
199
  @Gojo.on_callback_query(filters.regex("^un_"))
200
  async def reverse_callbacks(c: Gojo, q: CallbackQuery):
201
  data = q.data.split("_")
202
  action = data[1]
203
+ user_id = int(q.data.split("=")[1])
204
  if action == "ban":
205
  user = await q.message.chat.get_member(q.from_user.id)
206
  if not user.privileges.can_restrict_members and q.from_user.id in SUPPORT_STAFF:
 
222
  if action == "mute":
223
  user = await q.message.chat.get_member(q.from_user.id)
224
 
225
+ if not user.privileges.can_restrict_members and q.from_user.id in SUPPORT_STAFF:
226
  await q.answer(
227
  "You don't have enough permission to do this!\nStay in your limits!",
228
  show_alert=True,
 
256
  dic = {}
257
  for i in str(dic.keys()):
258
  if str(c_id) != i:
259
+ z = {c_id : {}}
260
  dic.update(z)
261
+ for i in str(dic[c_id].keys()):
262
+ if str(u_id) != i:
263
+ z = {u_id : [[],[]]}
264
+ dic[c_id].update(z) # make the dic something like {c_id : {u_id : [[],[]]}}
265
+ sec = round(time.time())
266
+ try:
267
+ dic[c_id][u_id][0].append(sec)
268
+ dic[c_id][u_id][1].append("x")
269
+ except KeyError:
270
+ dic[c_id].update({u_id : [[sec], ["x"]]})
271
+ if len(dic[c_id][u_id][1]) == limit and len(dic[c_id][u_id][0]) == within:
272
+ x = int(dic[c_id][u_id][0][0])
273
+ y = int(dic[c_id][u_id][0][-1])
274
+ if y-x <= within:
275
+ if action == "ban":
276
+ try:
277
+ await m.chat.ban_member(u_id)
278
+ keyboard = InlineKeyboardMarkup(
279
+ [
280
+ [
281
+ InlineKeyboardButton(
282
+ "Unban",
283
+ callback_data=f"un_ban_={u_id}",
284
+ ),
285
+ ],
286
+ ],
287
+ )
288
+ txt = "Don't dare to spam here if I am around!"
289
+ await m.reply_animation(
290
+ animation=choice(BAN_GIFS),
291
+ caption=txt,
292
+ reply_markup=keyboard,
293
+ )
294
+ dic[c_id][u_id][1].clear()
295
+ dic[c_id][u_id][0].clear()
296
+ return
297
+
298
+ except UserAdminInvalid:
299
+ await m.reply_text(
300
+ "I can't protect this chat from this user",
301
+ )
302
+ dic[c_id][u_id][1].clear()
303
+ dic[c_id][u_id][0].clear()
304
+ return
305
+ except RPCError as ef:
306
+ await m.reply_text(
307
+ text=f"""Some error occured, report to @{SUPPORT_GROUP}
308
+
309
+ <b>Error:</b> <code>{ef}</code>"""
310
+ )
311
+ LOGGER.error(ef)
312
+ LOGGER.error(format_exc())
313
+ dic[c_id][u_id][1].clear()
314
+ dic[c_id][u_id][0].clear()
315
+ return
316
+
317
+ elif action == "kick":
318
+ try:
319
+ await m.chat.ban_member(u_id)
320
+ txt = "Don't dare to spam here if I am around!"
321
+ await m.reply_animation(
322
+ animation=choice(KICK_GIFS),
323
+ caption=txt,
324
+ )
325
+ await m.chat.unban_member(u_id)
326
+ dic[c_id][u_id][1].clear()
327
+ dic[c_id][u_id][0].clear()
328
+ return
329
+ except UserAdminInvalid:
330
+ await m.reply_text(
331
+ "I can't protect this chat from this user",
332
+ )
333
+ dic[c_id][u_id][1].clear()
334
+ dic[c_id][u_id][0].clear()
335
+ return
336
+ except RPCError as ef:
337
+ await m.reply_text(
338
+ text=f"""Some error occured, report to @{SUPPORT_GROUP}
339
+
340
+ <b>Error:</b> <code>{ef}</code>"""
341
+ )
342
+ LOGGER.error(ef)
343
+ LOGGER.error(format_exc())
344
+ dic[c_id][u_id][1].clear()
345
+ dic[c_id][u_id][0].clear()
346
+ return
347
+ elif action == "mute":
348
+ try:
349
+ await m.chat.restrict_member(
350
+ u_id,
351
+ ChatPermissions(),
352
+ )
353
+ keyboard = InlineKeyboardMarkup(
354
+ [
355
+ [
356
+ InlineKeyboardButton(
357
+ "Unmute",
358
+ callback_data=f"un_mute_={u_id}",
359
+ ),
360
+ ],
361
+ ],
362
+ )
363
+ txt = "Don't dare to spam here if I am around!"
364
+ await m.reply_animation(
365
+ animation=choice(MUTE_GIFS),
366
+ caption=txt,
367
+ reply_markup=keyboard,
368
+ )
369
+ dic[c_id][u_id][1].clear()
370
+ dic[c_id][u_id][0].clear()
371
+ return
372
+ except UserAdminInvalid:
373
+ await m.reply_text(
374
+ "I can't protect this chat from this user",
375
+ )
376
+ dic[c_id][u_id][1].clear()
377
+ dic[c_id][u_id][0].clear()
378
+ return
379
+ except RPCError as ef:
380
+ await m.reply_text(
381
+ text=f"""Some error occured, report to @{SUPPORT_GROUP}
382
+
383
+ <b>Error:</b> <code>{ef}</code>"""
384
+ )
385
+ LOGGER.error(ef)
386
+ LOGGER.error(format_exc())
387
+ dic[c_id][u_id][1].clear()
388
+ dic[c_id][u_id][0].clear()
389
+ return
390
+ else:
391
+ return
392
+