From 3660e220363f546679d4c632a161c1c4827a26ce Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 8 Apr 2018 15:43:23 +0300 Subject: [PATCH] Partial port of ninja.py from kateborg --- stdplugins/ninja.py | 40 ++++++++++++++++++++++++++++++++++++++++ stdplugins/testplugin.py | 9 --------- uniborg/util.py | 17 +++++++++++++++++ 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 stdplugins/ninja.py delete mode 100644 stdplugins/testplugin.py diff --git a/stdplugins/ninja.py b/stdplugins/ninja.py new file mode 100644 index 0000000..68db5a7 --- /dev/null +++ b/stdplugins/ninja.py @@ -0,0 +1,40 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import asyncio + +from telethon import events +import telethon.utils + +from uniborg import util + +async def get_target_message(event): + if event.is_reply and (await event.reply_message).from_id == borg.uid: + return event.reply_message + async for message in borg.iter_messages(await event.input_chat, limit=20): + if message.out: + return message + +async def await_read(chat, message): + chat = telethon.utils.get_peer_id(chat) + async def read_filter(read_event): + return telethon.utils.get_peer_id(await read_event.input_chat) == chat \ + and read_event.is_read(message) + fut = borg.await_event(events.MessageRead(inbox=False), read_filter) + + if await util.is_read(borg, chat, message): + fut.cancel() + return + + await fut + +@borg.on(util.admin_cmd(r"^\.del(ete)?$")) +async def delete(event): + await event.delete() + target = await get_target_message(event) + if target: + chat = await event.input_chat + await await_read(chat, target) + await asyncio.sleep(.5) + await borg.delete_messages(chat, target) diff --git a/stdplugins/testplugin.py b/stdplugins/testplugin.py deleted file mode 100644 index a7e8189..0000000 --- a/stdplugins/testplugin.py +++ /dev/null @@ -1,9 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -from telethon import events - -@borg.on(events.NewMessage) -async def asdf(e): - print(e.raw_text) diff --git a/uniborg/util.py b/uniborg/util.py index 0056e8e..7c06fb8 100644 --- a/uniborg/util.py +++ b/uniborg/util.py @@ -5,6 +5,23 @@ import re from telethon import events +from telethon.tl.functions.messages import GetPeerDialogsRequest def admin_cmd(pattern): return events.NewMessage(outgoing=True, pattern=re.compile(pattern)) + +async def is_read(borg, entity, message, is_out=None): + """ + Returns True if the given message (or id) has been read + if a id is given, is_out needs to be a bool + """ + is_out = getattr(message, 'out', is_out) + if not isinstance(is_out, bool): + raise ValueError('Message was id but is_out not provided or not a bool') + message_id = getattr(message, 'id', message) + if not isinstance(message_id, int): + raise ValueError('Failed to extract id from message') + + dialog = (await borg(GetPeerDialogsRequest([entity]))).dialogs[0] + max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id + return message_id <= max_id