PagerMaid-Pyro/pagermaid/modules/prune.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

189 lines
5.8 KiB
Python
Raw Normal View History

2022-05-23 12:40:30 +00:00
""" Module to automate message deletion. """
import contextlib
2022-05-23 12:40:30 +00:00
from asyncio import sleep
2022-08-01 16:04:45 +00:00
from pagermaid.enums import Client, Message
from pagermaid.listener import listener
2022-08-01 16:04:45 +00:00
from pagermaid.utils import lang
from pagermaid.utils.bot_utils import log
2022-06-20 13:55:14 +00:00
2022-05-23 12:40:30 +00:00
2023-03-12 03:56:01 +00:00
@listener(
is_plugin=False,
outgoing=True,
command="prune",
need_admin=True,
description=lang("prune_des"),
)
2022-09-01 08:35:19 +00:00
async def prune(client: Client, message: Message):
2023-03-12 03:56:01 +00:00
"""Purge every single message after the message you replied to."""
2022-05-23 12:40:30 +00:00
if not message.reply_to_message:
2023-03-12 03:56:01 +00:00
await message.edit(lang("not_reply"))
2022-05-23 12:40:30 +00:00
return
input_chat = message.chat.id
messages = []
count = 0
2023-01-09 12:50:29 +00:00
limit = message.id - message.reply_to_message.id + 1
2024-02-04 07:33:01 +00:00
if message.message_thread_id:
2023-03-12 03:56:01 +00:00
func = client.get_discussion_replies(
2024-02-04 07:33:01 +00:00
input_chat, message.message_thread_id, limit=limit
2023-03-12 03:56:01 +00:00
)
2023-01-09 12:50:29 +00:00
else:
func = client.get_chat_history(input_chat, limit=limit)
async for msg in func:
2022-05-23 12:40:30 +00:00
if msg.id < message.reply_to_message.id:
break
messages.append(msg.id)
count += 1
if msg.reply_to_message:
messages.append(msg.reply_to_message.id)
if len(messages) == 100:
2022-09-01 08:35:19 +00:00
await client.delete_messages(input_chat, messages)
2022-05-23 12:40:30 +00:00
messages = []
if messages:
2022-09-01 08:35:19 +00:00
await client.delete_messages(input_chat, messages)
2022-05-23 12:40:30 +00:00
await log(f"{lang('prune_hint1')} {str(count)} {lang('prune_hint2')}")
2022-09-01 08:35:19 +00:00
notification = await send_prune_notify(client, message, count, count)
2022-05-23 12:40:30 +00:00
await sleep(1)
await notification.delete()
2023-03-12 03:56:01 +00:00
@listener(
is_plugin=False,
outgoing=True,
command="selfprune",
need_admin=True,
description=lang("sp_des"),
parameters=lang("sp_parameters"),
)
2022-08-01 16:04:45 +00:00
async def self_prune(bot: Client, message: Message):
2023-03-12 03:56:01 +00:00
"""Deletes specific amount of messages you sent."""
2022-05-23 12:40:30 +00:00
msgs = []
count_buffer = 0
2022-08-01 16:04:45 +00:00
offset = 0
2022-06-20 13:55:14 +00:00
if len(message.parameter) != 1:
2022-05-23 12:40:30 +00:00
if not message.reply_to_message:
2023-03-12 03:56:01 +00:00
return await message.edit(lang("arg_error"))
2022-08-01 16:04:45 +00:00
offset = message.reply_to_message.id
2022-05-23 12:40:30 +00:00
try:
count = int(message.parameter[0])
await message.delete()
except ValueError:
2023-03-12 03:56:01 +00:00
await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
return
2022-08-01 16:04:45 +00:00
async for msg in bot.get_chat_history(message.chat.id, limit=100):
if count_buffer == count:
break
if msg.from_user and msg.from_user.is_self:
msgs.append(msg.id)
count_buffer += 1
if len(msgs) == 100:
2022-09-01 08:35:19 +00:00
await bot.delete_messages(message.chat.id, msgs)
2022-08-01 16:04:45 +00:00
msgs = []
2023-03-12 03:56:01 +00:00
async for msg in bot.search_messages(
message.chat.id, from_user="me", offset=offset
):
2022-05-23 12:40:30 +00:00
if count_buffer == count:
break
msgs.append(msg.id)
count_buffer += 1
if len(msgs) == 100:
2022-09-01 08:35:19 +00:00
await bot.delete_messages(message.chat.id, msgs)
2022-05-23 12:40:30 +00:00
msgs = []
if msgs:
2022-09-01 08:35:19 +00:00
await bot.delete_messages(message.chat.id, msgs)
2022-06-20 13:55:14 +00:00
await log(
f"{lang('prune_hint1')}{lang('sp_hint')} {str(count_buffer)} / {count} {lang('prune_hint2')}"
)
with contextlib.suppress(ValueError):
2022-09-01 08:35:19 +00:00
notification = await send_prune_notify(bot, message, count_buffer, count)
2022-05-23 12:40:30 +00:00
await sleep(1)
await notification.delete()
2023-03-12 03:56:01 +00:00
@listener(
is_plugin=False,
outgoing=True,
command="yourprune",
need_admin=True,
description=lang("yp_des"),
parameters=lang("sp_parameters"),
)
2022-08-01 16:04:45 +00:00
async def your_prune(bot: Client, message: Message):
2023-03-12 03:56:01 +00:00
"""Deletes specific amount of messages someone sent."""
2022-05-23 12:40:30 +00:00
if not message.reply_to_message:
2023-03-12 03:56:01 +00:00
return await message.edit(lang("not_reply"))
2022-05-23 12:40:30 +00:00
target = message.reply_to_message
if not target.from_user:
2023-03-12 03:56:01 +00:00
return await message.edit(lang("not_reply"))
2022-06-20 13:55:14 +00:00
if len(message.parameter) != 1:
2023-03-12 03:56:01 +00:00
return await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
count = 0
try:
count = int(message.parameter[0])
await message.delete()
except ValueError:
2023-03-12 03:56:01 +00:00
return await message.edit(lang("arg_error"))
2022-05-23 12:40:30 +00:00
except Exception: # noqa
pass
count_buffer = 0
2022-08-01 16:04:45 +00:00
msgs = []
async for msg in bot.get_chat_history(message.chat.id, limit=100):
if count_buffer == count:
break
if msg.from_user and msg.from_user.id == target.from_user.id:
msgs.append(msg.id)
count_buffer += 1
if len(msgs) == 100:
2022-09-01 08:35:19 +00:00
await bot.delete_messages(message.chat.id, msgs)
2022-08-01 16:04:45 +00:00
msgs = []
2023-03-12 03:56:01 +00:00
async for msg in bot.search_messages(
message.chat.id, from_user=target.from_user.id
):
2022-05-23 12:40:30 +00:00
if count_buffer == count:
break
count_buffer += 1
2022-08-01 16:04:45 +00:00
msgs.append(msg.id)
if len(msgs) == 100:
2022-09-01 08:35:19 +00:00
await bot.delete_messages(message.chat.id, msgs)
2022-08-01 16:04:45 +00:00
msgs = []
if msgs:
2022-09-01 08:35:19 +00:00
await bot.delete_messages(message.chat.id, msgs)
2022-06-20 13:55:14 +00:00
await log(
f"{lang('prune_hint1')}{lang('yp_hint')} {str(count_buffer)} / {count} {lang('prune_hint2')}"
)
2022-08-01 16:04:45 +00:00
with contextlib.suppress(ValueError):
2022-09-01 08:35:19 +00:00
notification = await send_prune_notify(bot, message, count_buffer, count)
2022-08-01 16:04:45 +00:00
await sleep(1)
await notification.delete()
2022-05-23 12:40:30 +00:00
2023-03-12 03:56:01 +00:00
@listener(
is_plugin=False,
outgoing=True,
command="del",
need_admin=True,
description=lang("del_des"),
)
2022-06-20 13:55:14 +00:00
async def delete(message: Message):
2023-03-12 03:56:01 +00:00
"""Deletes the message you replied to."""
2022-06-20 13:55:14 +00:00
if target := message.reply_to_message:
with contextlib.suppress(Exception):
2022-05-23 12:40:30 +00:00
await target.delete()
await message.delete()
2023-03-12 03:56:01 +00:00
await log(lang("del_notification"))
2022-05-23 12:40:30 +00:00
else:
await message.delete()
2022-09-01 08:35:19 +00:00
async def send_prune_notify(bot: Client, message: Message, count_buffer, count):
return await bot.send_message(
2022-05-23 12:40:30 +00:00
message.chat.id,
2022-06-20 13:55:14 +00:00
f"{lang('spn_deleted')} {str(count_buffer)} / {str(count)} {lang('prune_hint2')}",
2024-02-04 07:33:01 +00:00
message_thread_id=message.message_thread_id,
2022-05-23 12:40:30 +00:00
)