This commit is contained in:
Dan Elkouby 2018-04-08 17:44:09 +03:00
parent 3cdb595b55
commit cd36ccf552
5 changed files with 27 additions and 12 deletions

View File

@ -9,6 +9,7 @@ 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
@ -16,11 +17,13 @@ async def get_target_message(event):
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)
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):
@ -29,6 +32,7 @@ async def await_read(chat, message):
await fut
@borg.on(util.admin_cmd(r"^\.del(ete)?$"))
async def delete(event):
await event.delete()

View File

@ -9,6 +9,7 @@ from uniborg import util
DELETE_TIMEOUT = 2
@borg.on(util.admin_cmd(r"^\.load (?P<shortname>\w+)$"))
async def load_reload(event):
await event.delete()
@ -19,7 +20,8 @@ async def load_reload(event):
borg.remove_plugin(shortname)
borg.load_plugin(shortname)
msg = await event.respond(f"Successfully (re)loaded plugin {shortname}")
msg = await event.respond(
f"Successfully (re)loaded plugin {shortname}")
await asyncio.sleep(DELETE_TIMEOUT)
await borg.delete_messages(msg.to_id, msg)
@ -28,6 +30,7 @@ async def load_reload(event):
logger.warn(f"Failed to (re)load plugin {shortname}: {tb}")
await event.respond(f"Failed to (re)load plugin {shortname}: {e}")
@borg.on(util.admin_cmd(r"^\.remove (?P<shortname>\w+)$"))
async def remove(event):
await event.delete()

View File

@ -2,6 +2,7 @@
# 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/.
class ReverseList(list):
def __iter__(self):
return reversed(self)

View File

@ -13,16 +13,20 @@ import telethon.events
from . import hacks
class Uniborg(TelegramClient):
def __init__(self, session, *, plugin_path="plugins", bot_token=None, **kwargs):
def __init__(
self, session, *, plugin_path="plugins",
bot_token=None, **kwargs):
# TODO: handle non-string session
self._name = session
self._logger = logging.getLogger(session)
self._plugins = {}
self._plugin_path = plugin_path
super().__init__(session,
17349, "344583e45741c457fe1862106095a5eb", # yarr
super().__init__(
session,
17349, "344583e45741c457fe1862106095a5eb", # yarr
**kwargs)
# This is a hack, please avert your eyes
@ -87,7 +91,7 @@ class Uniborg(TelegramClient):
fut.set_result(event)
raise telethon.events.StopPropagation
fut.add_done_callback(lambda _:
self.remove_event_handler(cb, event_matcher))
fut.add_done_callback(
lambda _: self.remove_event_handler(cb, event_matcher))
return fut

View File

@ -7,20 +7,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)
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)
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')
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