forked from uniborg/uniborg
Compare commits
3 Commits
319eb84c5a
...
573f36bf57
Author | SHA1 | Date |
---|---|---|
udf | 573f36bf57 | |
udf | b21f5ec104 | |
Lonami | 03e922ed86 |
|
@ -37,7 +37,7 @@ def yaml_format(obj, indent=0):
|
||||||
has_multiple_items = len(items) > 2
|
has_multiple_items = len(items) > 2
|
||||||
if has_multiple_items:
|
if has_multiple_items:
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
indent += 2
|
indent += 2
|
||||||
for k, v in items:
|
for k, v in items:
|
||||||
if k == '_' or v is None:
|
if k == '_' or v is None:
|
||||||
continue
|
continue
|
||||||
|
@ -45,11 +45,14 @@ def yaml_format(obj, indent=0):
|
||||||
if not formatted.strip():
|
if not formatted.strip():
|
||||||
continue
|
continue
|
||||||
result.append(' ' * (indent if has_multiple_items else 1))
|
result.append(' ' * (indent if has_multiple_items else 1))
|
||||||
result.append(f'{k}: {formatted}')
|
result.append(f'{k}:')
|
||||||
|
if not formatted[0].isspace():
|
||||||
|
result.append(' ')
|
||||||
|
result.append(f'{formatted}')
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
indent -= 2
|
if has_multiple_items:
|
||||||
result.append(' ' * indent)
|
indent -= 2
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
# truncate long strings and display elipsis
|
# truncate long strings and display elipsis
|
||||||
result = repr(obj[:STR_LEN_MAX])
|
result = repr(obj[:STR_LEN_MAX])
|
||||||
|
@ -75,7 +78,6 @@ def yaml_format(obj, indent=0):
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
indent -= 2
|
indent -= 2
|
||||||
result.append(' ' * indent)
|
|
||||||
else:
|
else:
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
|
|
||||||
|
|
|
@ -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,68 +18,67 @@ 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
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
snip = snips[name]
|
media = None
|
||||||
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
|
|
||||||
|
|
||||||
await borg.send_message(await event.get_input_chat(), snip['text'],
|
await borg.send_message(await event.get_input_chat(), snip['text'],
|
||||||
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:
|
||||||
snips.pop(name, None)
|
return
|
||||||
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
|
|
||||||
|
|
||||||
snips[name] = snip
|
snips.pop(name, None)
|
||||||
storage.snips = snips
|
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))
|
@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()
|
|
||||||
|
|
Loading…
Reference in New Issue