uniborg/stdplugins/ninja.py

55 lines
1.6 KiB
Python
Raw Permalink 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):
2018-06-22 08:46:39 +00:00
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
return await event.get_reply_message()
async for message in borg.iter_messages(
await event.get_input_chat(), limit=20):
2018-04-08 12:43:23 +00:00
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-06-22 08:46:39 +00:00
return (read_event.chat_id == chat
2018-04-08 14:44:09 +00:00
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:
2018-06-22 08:46:39 +00:00
chat = await event.get_input_chat()
2018-04-08 12:43:23 +00:00
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)