uniborg/uniborg/util.py

31 lines
1.1 KiB
Python
Raw Normal View History

# 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 re
from telethon import events
2018-04-08 12:43:23 +00:00
from telethon.tl.functions.messages import GetPeerDialogsRequest
2018-04-08 14:44:09 +00:00
def admin_cmd(pattern):
return events.NewMessage(outgoing=True, pattern=re.compile(pattern))
2018-04-08 12:43:23 +00:00
2018-04-08 14:44:09 +00:00
2018-04-08 12:43:23 +00:00
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
"""
2018-04-08 14:44:09 +00:00
is_out = getattr(message, "out", is_out)
2018-04-08 12:43:23 +00:00
if not isinstance(is_out, bool):
2018-04-08 14:44:09 +00:00
raise ValueError(
"Message was id but is_out not provided or not a bool")
message_id = getattr(message, "id", message)
2018-04-08 12:43:23 +00:00
if not isinstance(message_id, int):
2018-04-08 14:44:09 +00:00
raise ValueError("Failed to extract id from message")
2018-04-08 12:43:23 +00:00
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