From 3e5b6fddb3dcf1cdd81af2dd134a71bee8e96e09 Mon Sep 17 00:00:00 2001 From: udf Date: Wed, 21 Nov 2018 21:05:33 +0200 Subject: [PATCH] Add superblock plugin --- stdplugins/superblock.py | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 stdplugins/superblock.py diff --git a/stdplugins/superblock.py b/stdplugins/superblock.py new file mode 100644 index 0000000..ed4849e --- /dev/null +++ b/stdplugins/superblock.py @@ -0,0 +1,53 @@ +import asyncio +import time +from telethon import events +import telethon.tl.functions as tlf +from telethon.tl.types import InputPeerChannel, UpdateUserBlocked +from telethon.tl.functions.contacts import GetBlockedRequest + + +# How often to fetch the full list of blocked users +REFETCH_TIME = 60 +blocked_user_ids = set() + + +@borg.on(events.NewMessage(incoming=True, func=lambda e: e.message.mentioned)) +async def on_mentioned(event): + if not event.message.from_id: # Channel messages don't have a from_id + return + if event.from_id not in blocked_user_ids: + return + + peer = await borg.get_input_entity(event.chat_id) + if isinstance(peer, InputPeerChannel): + o = tlf.channels.ReadMessageContentsRequest(peer, [event.message.id]) + else: + o = tlf.messages.ReadMessageContentsRequest([event.message.id]) + await borg(o) + + +@borg.on(events.Raw(types=UpdateUserBlocked)) +async def on_blocked(event): + if event.blocked: + blocked_user_ids.add(event.user_id) + else: + blocked_user_ids.discard(event.user_id) + + +async def fetch_blocked_users(): + global blocked_user_ids + while 1: + offset = 0 + blocked_ids = set() + while 1: + blocked = await borg(GetBlockedRequest(offset=offset, limit=100)) + offset += 100 + for contact in blocked.blocked: + blocked_ids.add(contact.user_id) + if not blocked.blocked: + break + blocked_user_ids = blocked_ids + await asyncio.sleep(REFETCH_TIME) + + +asyncio.ensure_future(fetch_blocked_users())