uniborg/stdplugins/ninja.py

54 lines
1.6 KiB
Python
Raw Normal View History

2018-04-08 12:43:23 +00:00
# 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
2018-04-08 12:43:23 +00:00
async def get_target_message(event):
if event.is_reply and (await event.reply_message).from_id == borg.uid:
2018-06-02 19:18:59 +00:00
return await event.reply_message
2018-04-08 12:43:23 +00:00
async for message in borg.iter_messages(await event.input_chat, limit=20):
if message.out:
return message
2018-04-08 14:44:09 +00:00
2018-04-08 12:43:23 +00:00
async def await_read(chat, message):
chat = telethon.utils.get_peer_id(chat)
2018-04-08 14:44:09 +00:00
2018-04-08 12:43:23 +00:00
async def read_filter(read_event):
2018-04-08 14:44:09 +00:00
return (telethon.utils.get_peer_id(await read_event.input_chat) == chat
and read_event.is_read(message))
2018-04-08 12:43:23 +00:00
fut = borg.await_event(events.MessageRead(inbox=False), read_filter)
if await util.is_read(borg, chat, message):
fut.cancel()
return
await fut
2018-04-08 14:44:09 +00:00
@borg.on(util.admin_cmd(r"^\.(del)(?:ete)?$"))
2018-04-08 15:07:05 +00:00
@borg.on(util.admin_cmd(r"^\.(edit)(?:\s+(.*))?$"))
2018-04-08 12:43:23 +00:00
async def delete(event):
await event.delete()
command = event.pattern_match.group(1)
if command == 'edit':
2018-04-08 17:07:12 +00:00
text = event.pattern_match.group(2)
if not text:
return
2018-04-08 12:43:23 +00:00
target = await get_target_message(event)
if target:
chat = await event.input_chat
await await_read(chat, target)
await asyncio.sleep(.5)
if command == 'edit':
await borg.edit_message(chat, target, text)
else:
await borg.delete_messages(chat, target)