From 54468c493fd901128027daf6269d064b3a755954 Mon Sep 17 00:00:00 2001 From: udf Date: Mon, 9 Apr 2018 01:56:42 +0200 Subject: [PATCH] port axe from kateborg this more of an exercise to learn asyncio than it is an actual plugin --- stdplugins/axe.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 stdplugins/axe.py diff --git a/stdplugins/axe.py b/stdplugins/axe.py new file mode 100644 index 0000000..69be537 --- /dev/null +++ b/stdplugins/axe.py @@ -0,0 +1,40 @@ +# 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 +import re + +from telethon import events +from telethon.tl.types import Message + +pattern = re.compile(r'(?i)^a+nd\b') + + +@borg.on(events.NewMessage) +async def and_my_axe(event): + async def next_match(event): + if pattern.match(event.raw_text): + raise events.StopPropagation + return True + + prev_event = None + while pattern.match(event.raw_text): + prev_message = getattr(prev_event, 'message', None) + if event.is_reply: + prev_message = await event.reply_message + if not (isinstance(prev_message, Message) + and pattern.match(prev_message.message)): + return + + if prev_message: + if event.message.from_id != prev_message.from_id: + await event.reply('and my axe!') + return + if event.is_reply: + return + + prev_event = event + event = await borg.await_event( + events.NewMessage(chats=await event.input_chat), next_match + )