Partial port of ninja.py from kateborg

This commit is contained in:
Dan Elkouby 2018-04-08 15:43:23 +03:00
parent 1ec12184fe
commit 3660e22036
3 changed files with 57 additions and 9 deletions

40
stdplugins/ninja.py Normal file
View File

@ -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)

View File

@ -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)

View File

@ -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