This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues/pull-requests.
uniborg/stdplugins/chain.py

49 lines
1.4 KiB
Python
Raw Normal View History

2018-11-02 19:15:37 +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/.
2020-02-24 15:08:04 +00:00
"""
Counts how long a reply .chain is
"""
2020-01-11 23:47:30 +00:00
from collections import defaultdict
2018-11-02 19:15:37 +00:00
from telethon import events
from telethon.tl.functions.messages import SaveDraftRequest
2020-01-11 23:47:30 +00:00
def intify(d):
for k, v in d.items():
if isinstance(v, dict):
v = dict(intify(v))
yield int(k), v
global_cache = defaultdict(lambda: {}, intify(storage.cache or {}))
2018-11-02 19:15:37 +00:00
@borg.on(events.NewMessage(pattern=r"\.chain", outgoing=True))
async def _(event):
2020-01-11 23:47:30 +00:00
cache = global_cache[event.chat_id]
message = event.reply_to_msg_id
count = 0
while message is not None:
reply = cache.get(message, -1)
if reply == -1:
reply = None
2020-02-13 17:09:45 +00:00
if m := await borg.get_messages(event.chat_id, ids=message):
2020-01-11 23:47:30 +00:00
reply = m.reply_to_msg_id
cache[message] = reply
if len(cache) % 10 == 0:
await event.edit(f"Counting... ({len(cache)} cached entries)")
2018-11-02 19:15:37 +00:00
if reply is None:
await borg(SaveDraftRequest(
await event.get_input_chat(),
"",
2020-01-11 23:47:30 +00:00
reply_to_msg_id=message
2018-11-02 19:15:37 +00:00
))
2020-01-11 23:47:30 +00:00
2018-11-02 19:15:37 +00:00
message = reply
count += 1
2020-01-11 23:47:30 +00:00
if count:
storage.cache = global_cache
2018-11-02 19:15:37 +00:00
await event.edit(f"Chain length: {count}")