2020-02-19 15:31:39 +00:00
|
|
|
""" Module to automate message deletion. """
|
|
|
|
|
|
|
|
from asyncio import sleep
|
|
|
|
from telethon.errors.rpcbaseerrors import BadRequestError
|
|
|
|
from pagermaid import log
|
|
|
|
from pagermaid.listener import listener
|
2021-06-15 04:31:05 +00:00
|
|
|
from pagermaid.utils import lang, alias_command
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command('prune'),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('prune_des'))
|
2020-02-19 15:31:39 +00:00
|
|
|
async def prune(context):
|
|
|
|
""" Purge every single message after the message you replied to. """
|
|
|
|
if not context.reply_to_msg_id:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('not_reply'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
input_chat = await context.get_input_chat()
|
|
|
|
messages = []
|
|
|
|
count = 0
|
|
|
|
async for message in context.client.iter_messages(input_chat, min_id=context.reply_to_msg_id):
|
|
|
|
messages.append(message)
|
|
|
|
count += 1
|
|
|
|
messages.append(context.reply_to_msg_id)
|
|
|
|
if len(messages) == 100:
|
|
|
|
await context.client.delete_messages(input_chat, messages)
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
if messages:
|
|
|
|
await context.client.delete_messages(input_chat, messages)
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"{lang('prune_hint1')} {str(count)} {lang('prune_hint2')}")
|
2020-08-07 12:05:50 +00:00
|
|
|
notification = await send_prune_notify(context, count, count)
|
2021-06-15 16:51:34 +00:00
|
|
|
await sleep(1)
|
2020-02-19 15:31:39 +00:00
|
|
|
await notification.delete()
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("selfprune"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('sp_des'),
|
|
|
|
parameters=lang('sp_parameters'))
|
2020-02-19 15:31:39 +00:00
|
|
|
async def selfprune(context):
|
|
|
|
""" Deletes specific amount of messages you sent. """
|
2021-06-15 16:51:34 +00:00
|
|
|
msgs = []
|
|
|
|
count_buffer = 0
|
2020-02-19 15:31:39 +00:00
|
|
|
if not len(context.parameter) == 1:
|
2021-06-15 16:51:34 +00:00
|
|
|
if not context.reply_to_msg_id:
|
|
|
|
await context.edit(lang('arg_error'))
|
|
|
|
return
|
|
|
|
async for msg in context.client.iter_messages(
|
|
|
|
context.chat_id,
|
|
|
|
from_user="me",
|
|
|
|
min_id=context.reply_to_msg_id,
|
|
|
|
):
|
|
|
|
msgs.append(msg)
|
|
|
|
count_buffer += 1
|
|
|
|
if len(msgs) == 100:
|
|
|
|
await context.client.delete_messages(context.chat_id, msgs)
|
|
|
|
msgs = []
|
|
|
|
if msgs:
|
|
|
|
await context.client.delete_messages(context.chat_id, msgs)
|
|
|
|
if count_buffer == 0:
|
|
|
|
await context.delete()
|
|
|
|
count_buffer += 1
|
|
|
|
await log(f"{lang('prune_hint1')}{lang('sp_hint')} {str(count_buffer)} {lang('prune_hint2')}")
|
|
|
|
notification = await send_prune_notify(context, count_buffer, count_buffer)
|
|
|
|
await sleep(1)
|
|
|
|
await notification.delete()
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
count = int(context.parameter[0])
|
2020-08-08 00:54:35 +00:00
|
|
|
await context.delete()
|
2020-02-19 15:31:39 +00:00
|
|
|
except ValueError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-02-19 15:31:39 +00:00
|
|
|
return
|
|
|
|
async for message in context.client.iter_messages(context.chat_id, from_user="me"):
|
|
|
|
if count_buffer == count:
|
|
|
|
break
|
2021-06-15 16:51:34 +00:00
|
|
|
msgs.append(message)
|
2020-02-19 15:31:39 +00:00
|
|
|
count_buffer += 1
|
2021-06-15 16:51:34 +00:00
|
|
|
if len(msgs) == 100:
|
|
|
|
await context.client.delete_messages(context.chat_id, msgs)
|
|
|
|
msgs = []
|
|
|
|
if msgs:
|
|
|
|
await context.client.delete_messages(context.chat_id, msgs)
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"{lang('prune_hint1')}{lang('sp_hint')} {str(count_buffer)} / {str(count)} {lang('prune_hint2')}")
|
2021-08-16 14:34:37 +00:00
|
|
|
try:
|
|
|
|
notification = await send_prune_notify(context, count_buffer, count)
|
2021-11-25 09:44:38 +00:00
|
|
|
await sleep(1)
|
|
|
|
await notification.delete()
|
2021-08-16 14:34:37 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2020-02-19 15:31:39 +00:00
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("yourprune"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('yp_des'),
|
|
|
|
parameters=lang('sp_parameters'))
|
2020-04-05 10:02:47 +00:00
|
|
|
async def yourprune(context):
|
|
|
|
""" Deletes specific amount of messages someone sent. """
|
|
|
|
if not context.reply_to_msg_id:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('not_reply'))
|
2020-04-05 10:02:47 +00:00
|
|
|
return
|
|
|
|
target = await context.get_reply_message()
|
|
|
|
if not len(context.parameter) == 1:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-04-05 10:02:47 +00:00
|
|
|
return
|
2021-11-25 09:44:38 +00:00
|
|
|
count = 0
|
2020-04-05 10:02:47 +00:00
|
|
|
try:
|
|
|
|
count = int(context.parameter[0])
|
2020-05-02 14:15:23 +00:00
|
|
|
await context.delete()
|
2020-04-05 10:02:47 +00:00
|
|
|
except ValueError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('arg_error'))
|
2020-04-05 10:02:47 +00:00
|
|
|
return
|
2020-05-02 14:15:23 +00:00
|
|
|
except:
|
|
|
|
pass
|
2020-04-05 10:02:47 +00:00
|
|
|
count_buffer = 0
|
|
|
|
async for message in context.client.iter_messages(context.chat_id, from_user=target.from_id):
|
|
|
|
if count_buffer == count:
|
|
|
|
break
|
|
|
|
await message.delete()
|
|
|
|
count_buffer += 1
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(f"{lang('prune_hint1')}{lang('yp_hint')} {str(count_buffer)} / {str(count)} {lang('prune_hint2')}")
|
2020-08-07 12:05:50 +00:00
|
|
|
notification = await send_prune_notify(context, count_buffer, count)
|
2021-06-15 16:51:34 +00:00
|
|
|
await sleep(1)
|
2020-04-05 10:02:47 +00:00
|
|
|
await notification.delete()
|
|
|
|
|
|
|
|
|
2021-06-15 04:31:05 +00:00
|
|
|
@listener(is_plugin=False, outgoing=True, command=alias_command("del"),
|
2021-04-12 16:25:32 +00:00
|
|
|
description=lang('del_des'))
|
2020-02-19 15:31:39 +00:00
|
|
|
async def delete(context):
|
|
|
|
""" Deletes the message you replied to. """
|
|
|
|
target = await context.get_reply_message()
|
|
|
|
if context.reply_to_msg_id:
|
|
|
|
try:
|
2021-07-02 12:13:22 +00:00
|
|
|
if target is None:
|
|
|
|
await context.delete()
|
|
|
|
return
|
2020-02-19 15:31:39 +00:00
|
|
|
await target.delete()
|
|
|
|
await context.delete()
|
2021-04-12 16:25:32 +00:00
|
|
|
await log(lang('del_notification'))
|
2020-02-19 15:31:39 +00:00
|
|
|
except BadRequestError:
|
2021-04-12 16:25:32 +00:00
|
|
|
await context.edit(lang('del_BadRequestError'))
|
2020-02-19 15:31:39 +00:00
|
|
|
else:
|
|
|
|
await context.delete()
|
|
|
|
|
|
|
|
|
2020-08-07 12:05:50 +00:00
|
|
|
async def send_prune_notify(context, count_buffer, count):
|
2020-02-19 15:31:39 +00:00
|
|
|
return await context.client.send_message(
|
|
|
|
context.chat_id,
|
2022-02-02 17:15:24 +00:00
|
|
|
"%s %s / %s %s" % (lang('spn_deleted'), str(count_buffer), str(count), lang('prune_hint2'))
|
2020-02-19 15:31:39 +00:00
|
|
|
)
|