Don't save unexisting snips on .snip

This commit is contained in:
Lonami 2018-12-28 22:27:23 +01:00
parent 319eb84c5a
commit 03e922ed86
1 changed files with 35 additions and 34 deletions

View File

@ -1,9 +1,11 @@
# This Source Code Form is subject to the terms of the Mozilla Public # 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 # 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/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import asyncio
from telethon import events, utils from telethon import events, utils
from telethon.tl import types from telethon.tl import types
loop = asyncio.get_event_loop()
TYPE_TEXT = 0 TYPE_TEXT = 0
TYPE_PHOTO = 1 TYPE_PHOTO = 1
@ -16,10 +18,11 @@ snips = storage.snips or {}
@borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True)) @borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True))
async def on_snip(event): async def on_snip(event):
loop.create_task(event.delete())
name = event.pattern_match.group(1) name = event.pattern_match.group(1)
if name not in snips: if name not in snips:
await on_snip_save(event) return
else:
snip = snips[name] snip = snips[name]
if snip['type'] == TYPE_PHOTO: if snip['type'] == TYPE_PHOTO:
media = types.InputPhoto(snip['id'], snip['hash'], file_reference=b'') media = types.InputPhoto(snip['id'], snip['hash'], file_reference=b'')
@ -32,14 +35,15 @@ async def on_snip(event):
file=media, file=media,
reply_to=event.message.reply_to_msg_id) reply_to=event.message.reply_to_msg_id)
await event.delete()
@borg.on(events.NewMessage(pattern=r'\.snips (\S+)', outgoing=True)) @borg.on(events.NewMessage(pattern=r'\.snips (\S+)', outgoing=True))
async def on_snip_save(event): async def on_snip_save(event):
loop.create_task(event.delete())
name = event.pattern_match.group(1) name = event.pattern_match.group(1)
msg = await event.get_reply_message() msg = await event.get_reply_message()
if msg: if not msg:
return
snips.pop(name, None) snips.pop(name, None)
snip = {'type': TYPE_TEXT, 'text': msg.message or ''} snip = {'type': TYPE_TEXT, 'text': msg.message or ''}
if msg.media: if msg.media:
@ -57,27 +61,24 @@ async def on_snip_save(event):
snips[name] = snip snips[name] = snip
storage.snips = snips storage.snips = snips
await event.delete()
@borg.on(events.NewMessage(pattern=r'\.snipl', outgoing=True)) @borg.on(events.NewMessage(pattern=r'\.snipl', outgoing=True))
async def on_snip_list(event): async def on_snip_list(event):
loop.create_task(event.delete())
await event.respond('available snips: ' + ', '.join(snips.keys())) await event.respond('available snips: ' + ', '.join(snips.keys()))
await event.delete()
@borg.on(events.NewMessage(pattern=r'\.snipd (\S+)', outgoing=True)) @borg.on(events.NewMessage(pattern=r'\.snipd (\S+)', outgoing=True))
async def on_snip_delete(event): async def on_snip_delete(event):
loop.create_task(event.delete())
snips.pop(event.pattern_match.group(1), None) snips.pop(event.pattern_match.group(1), None)
storage.snips = snips storage.snips = snips
await event.delete()
@borg.on(events.NewMessage(pattern=r'\.snipr (\S+)\s+(\S+)', outgoing=True)) @borg.on(events.NewMessage(pattern=r'\.snipr (\S+)\s+(\S+)', outgoing=True))
async def on_snip_rename(event): async def on_snip_rename(event):
loop.create_task(event.delete())
snip = snips.pop(event.pattern_match.group(1), None) snip = snips.pop(event.pattern_match.group(1), None)
if snip: if snip:
snips[event.pattern_match.group(2)] = snip snips[event.pattern_match.group(2)] = snip
storage.snips = snips storage.snips = snips
await event.delete()