Skip to content

Commit

Permalink
now purge works in PMs and basic groups (UsergeTeam#168)
Browse files Browse the repository at this point in the history
1) now works in PMs and basic groups
2) remove redundant codes
3) added more bugs πŸ€ͺπŸ˜†πŸ˜†
  • Loading branch information
SpEcHiDe authored Oct 10, 2020
1 parent b068461 commit 791f5fb
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions userge/plugins/admin/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
'usage': "reply {tr}purge to the start message to purge.\n"
"use {tr}purge [user_id | user_name] to purge messages from that user or use flags",
'examples': ['{tr}purge', '{tr}purge -u', '{tr}purge [user_id | user_name]']},
allow_bots=False, allow_private=False, del_pre=True)
allow_bots=False, del_pre=True)
async def purge_(message: Message):
await message.edit("`purging ...`")
from_user_id = 0
from_user_id = None
if message.filtered_input_str:
from_user_id = (await message.client.get_users(message.filtered_input_str)).id
start_message = 0
Expand All @@ -40,22 +40,26 @@ async def purge_(message: Message):
return
start_t = datetime.now()
message_ids = range(start_message, message.message_id)
list_of_messages = await message.client.get_messages(chat_id=message.chat.id,
message_ids=message_ids,
replies=0)
list_of_messages_to_delete = []
list_of_messages = []
purged_messages_count = 0
for a_message in list_of_messages:
if len(list_of_messages_to_delete) == 100:
await message.client.delete_messages(chat_id=message.chat.id,
message_ids=list_of_messages_to_delete)
purged_messages_count += len(list_of_messages_to_delete)
list_of_messages_to_delete.clear()
if from_user_id:
if a_message.from_user and from_user_id == a_message.from_user.id:
list_of_messages_to_delete.append(a_message.message_id)
else:
list_of_messages_to_delete.append(a_message.message_id)
async for a_message in message.client.iter_history(
chat_id=message.chat.id,
limit=None,
offset_id=start_message,
reverse=True
):
if from_user_id and a_message and a_message.from_user and a_message.from_user.id == from_user_id:
list_of_messages.append(a_message.message_id)
if not from_user_id:
list_of_messages.append(a_message.message_id)
if len(list_of_messages) >= 100:
await message.client.delete_messages(
chat_id=message.chat.id,
message_ids=list_of_messages
)
purged_messages_count += len(list_of_messages)
list_of_messages = []

if list_of_messages_to_delete:
await message.client.delete_messages(chat_id=message.chat.id,
message_ids=list_of_messages_to_delete)
Expand Down

0 comments on commit 791f5fb

Please sign in to comment.