From 03e922ed86b74d67be3c9fb1ea7fb8109a45923c Mon Sep 17 00:00:00 2001 From: Lonami Date: Fri, 28 Dec 2018 22:27:23 +0100 Subject: [PATCH] Don't save unexisting snips on .snip --- stdplugins/snip.py | 69 +++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/stdplugins/snip.py b/stdplugins/snip.py index fceb4da..d78c645 100644 --- a/stdplugins/snip.py +++ b/stdplugins/snip.py @@ -1,9 +1,11 @@ # 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 from telethon import events, utils from telethon.tl import types +loop = asyncio.get_event_loop() TYPE_TEXT = 0 TYPE_PHOTO = 1 @@ -16,68 +18,67 @@ snips = storage.snips or {} @borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True)) async def on_snip(event): + loop.create_task(event.delete()) name = event.pattern_match.group(1) if name not in snips: - await on_snip_save(event) + return + + snip = snips[name] + if snip['type'] == TYPE_PHOTO: + media = types.InputPhoto(snip['id'], snip['hash'], file_reference=b'') + elif snip['type'] == TYPE_DOCUMENT: + media = types.InputDocument(snip['id'], snip['hash'], file_reference=b'') else: - snip = snips[name] - if snip['type'] == TYPE_PHOTO: - media = types.InputPhoto(snip['id'], snip['hash'], file_reference=b'') - elif snip['type'] == TYPE_DOCUMENT: - media = types.InputDocument(snip['id'], snip['hash'], file_reference=b'') - else: - media = None + media = None - await borg.send_message(await event.get_input_chat(), snip['text'], - file=media, - reply_to=event.message.reply_to_msg_id) - - await event.delete() + await borg.send_message(await event.get_input_chat(), snip['text'], + file=media, + reply_to=event.message.reply_to_msg_id) @borg.on(events.NewMessage(pattern=r'\.snips (\S+)', outgoing=True)) async def on_snip_save(event): + loop.create_task(event.delete()) name = event.pattern_match.group(1) msg = await event.get_reply_message() - if msg: - snips.pop(name, None) - snip = {'type': TYPE_TEXT, 'text': msg.message or ''} - if msg.media: - media = None - if isinstance(msg.media, types.MessageMediaPhoto): - media = utils.get_input_photo(msg.media.photo) - snip['type'] = TYPE_PHOTO - elif isinstance(msg.media, types.MessageMediaDocument): - media = utils.get_input_document(msg.media.document) - snip['type'] = TYPE_DOCUMENT - if media: - snip['id'] = media.id - snip['hash'] = media.access_hash + if not msg: + return - snips[name] = snip - storage.snips = snips + snips.pop(name, None) + snip = {'type': TYPE_TEXT, 'text': msg.message or ''} + if msg.media: + media = None + if isinstance(msg.media, types.MessageMediaPhoto): + media = utils.get_input_photo(msg.media.photo) + snip['type'] = TYPE_PHOTO + elif isinstance(msg.media, types.MessageMediaDocument): + media = utils.get_input_document(msg.media.document) + snip['type'] = TYPE_DOCUMENT + if media: + snip['id'] = media.id + snip['hash'] = media.access_hash - await event.delete() + snips[name] = snip + storage.snips = snips @borg.on(events.NewMessage(pattern=r'\.snipl', outgoing=True)) async def on_snip_list(event): + loop.create_task(event.delete()) await event.respond('available snips: ' + ', '.join(snips.keys())) - await event.delete() @borg.on(events.NewMessage(pattern=r'\.snipd (\S+)', outgoing=True)) async def on_snip_delete(event): + loop.create_task(event.delete()) snips.pop(event.pattern_match.group(1), None) storage.snips = snips - await event.delete() @borg.on(events.NewMessage(pattern=r'\.snipr (\S+)\s+(\S+)', outgoing=True)) async def on_snip_rename(event): + loop.create_task(event.delete()) snip = snips.pop(event.pattern_match.group(1), None) if snip: snips[event.pattern_match.group(2)] = snip storage.snips = snips - - await event.delete()