forked from uniborg/uniborg
Compare commits
No commits in common. "master" and "ninja_fix" have entirely different histories.
|
@ -1,27 +0,0 @@
|
||||||
# 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
|
|
||||||
|
|
||||||
|
|
||||||
_last_messages = {}
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(outgoing=True))
|
|
||||||
async def _(event):
|
|
||||||
_last_messages[event.chat_id] = event.message
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"\.(fix)?reply", outgoing=True))
|
|
||||||
async def _(event):
|
|
||||||
if not event.is_reply or event.chat_id not in _last_messages:
|
|
||||||
return
|
|
||||||
|
|
||||||
message = _last_messages[event.chat_id]
|
|
||||||
chat = await event.get_input_chat()
|
|
||||||
await asyncio.wait([
|
|
||||||
borg.delete_messages(chat, [event.id, message.id]),
|
|
||||||
borg.send_message(chat, message, reply_to=event.reply_to_msg_id)
|
|
||||||
])
|
|
|
@ -1,27 +0,0 @@
|
||||||
# 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 string
|
|
||||||
|
|
||||||
from telethon import events
|
|
||||||
from telethon.tl import types
|
|
||||||
|
|
||||||
msg_cache = {}
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"\.fpost\s+(.*)", outgoing=True))
|
|
||||||
async def _(event):
|
|
||||||
await event.delete()
|
|
||||||
text = event.pattern_match.group(1)
|
|
||||||
destination = await event.get_input_chat()
|
|
||||||
|
|
||||||
for c in text.lower():
|
|
||||||
if c not in string.ascii_lowercase:
|
|
||||||
continue
|
|
||||||
if c not in msg_cache:
|
|
||||||
async for msg in borg.iter_messages(None, search=c):
|
|
||||||
if msg.raw_text.lower() == c and msg.media is None:
|
|
||||||
msg_cache[c] = msg
|
|
||||||
break
|
|
||||||
await borg.forward_messages(destination, msg_cache[c])
|
|
|
@ -1,93 +0,0 @@
|
||||||
# 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/.
|
|
||||||
|
|
||||||
from telethon import events
|
|
||||||
from telethon.utils import add_surrogate
|
|
||||||
from telethon.tl.types import MessageEntityPre
|
|
||||||
from telethon.tl.tlobject import TLObject
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
STR_LEN_MAX = 256
|
|
||||||
BYTE_LEN_MAX = 64
|
|
||||||
|
|
||||||
|
|
||||||
def parse_pre(text):
|
|
||||||
text = text.strip()
|
|
||||||
return (
|
|
||||||
text,
|
|
||||||
[MessageEntityPre(offset=0, length=len(add_surrogate(text)), language='')]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def yaml_format(obj, indent=0):
|
|
||||||
"""
|
|
||||||
Pretty formats the given object as a YAML string which is returned.
|
|
||||||
(based on TLObject.pretty_format)
|
|
||||||
"""
|
|
||||||
result = []
|
|
||||||
if isinstance(obj, TLObject):
|
|
||||||
obj = obj.to_dict()
|
|
||||||
|
|
||||||
if isinstance(obj, dict):
|
|
||||||
if not obj:
|
|
||||||
return 'dict:'
|
|
||||||
result.append(obj.get('_', 'dict') + ':')
|
|
||||||
items = obj.items()
|
|
||||||
has_multiple_items = len(items) > 2
|
|
||||||
if has_multiple_items:
|
|
||||||
result.append('\n')
|
|
||||||
indent += 2
|
|
||||||
for k, v in items:
|
|
||||||
if k == '_' or v is None:
|
|
||||||
continue
|
|
||||||
formatted = yaml_format(v, indent)
|
|
||||||
if not formatted.strip():
|
|
||||||
continue
|
|
||||||
result.append(' ' * (indent if has_multiple_items else 1))
|
|
||||||
result.append(f'{k}:')
|
|
||||||
if not formatted[0].isspace():
|
|
||||||
result.append(' ')
|
|
||||||
result.append(f'{formatted}')
|
|
||||||
result.append('\n')
|
|
||||||
result.pop()
|
|
||||||
if has_multiple_items:
|
|
||||||
indent -= 2
|
|
||||||
elif isinstance(obj, str):
|
|
||||||
# truncate long strings and display elipsis
|
|
||||||
result = repr(obj[:STR_LEN_MAX])
|
|
||||||
if len(obj) > STR_LEN_MAX:
|
|
||||||
result += '…'
|
|
||||||
return result
|
|
||||||
elif isinstance(obj, bytes):
|
|
||||||
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
|
|
||||||
if all(0x20 <= c < 0x7f for c in obj):
|
|
||||||
return repr(obj)
|
|
||||||
else:
|
|
||||||
return ('<…>' if len(obj) > BYTE_LEN_MAX else
|
|
||||||
' '.join(f'{b:02X}' for b in obj))
|
|
||||||
elif isinstance(obj, datetime.datetime):
|
|
||||||
# ISO-8601 without timezone offset (telethon dates are always UTC)
|
|
||||||
return obj.strftime('%Y-%m-%d %H:%M:%S')
|
|
||||||
elif hasattr(obj, '__iter__'):
|
|
||||||
# display iterables one after another at the base indentation level
|
|
||||||
result.append('\n')
|
|
||||||
indent += 2
|
|
||||||
for x in obj:
|
|
||||||
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
|
||||||
result.append('\n')
|
|
||||||
result.pop()
|
|
||||||
indent -= 2
|
|
||||||
else:
|
|
||||||
return repr(obj)
|
|
||||||
|
|
||||||
return ''.join(result)
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"\.info", outgoing=True))
|
|
||||||
async def _(event):
|
|
||||||
if not event.message.is_reply:
|
|
||||||
return
|
|
||||||
msg = await event.message.get_reply_message()
|
|
||||||
yaml_text = yaml_format(msg)
|
|
||||||
await event.edit(yaml_text, parse_mode=parse_pre)
|
|
|
@ -31,26 +31,19 @@ def get_tag_parser(tag, entity):
|
||||||
return re.compile(tag + r'(.+?)' + tag, re.DOTALL), tag_parser
|
return re.compile(tag + r'(.+?)' + tag, re.DOTALL), tag_parser
|
||||||
|
|
||||||
|
|
||||||
|
PRINTABLE_ASCII = range(0x21, 0x7f)
|
||||||
def parse_aesthetics(m):
|
def parse_aesthetics(m):
|
||||||
def aesthetify(string):
|
def aesthetify(string):
|
||||||
for c in string:
|
for c in string:
|
||||||
if " " < c <= "~":
|
c = ord(c)
|
||||||
yield chr(ord(c) + 0xFF00 - 0x20)
|
if c in PRINTABLE_ASCII:
|
||||||
elif c == " ":
|
c += 0xFF00 - 0x20
|
||||||
yield "\u3000"
|
elif c == ord(" "):
|
||||||
else:
|
c = 0x3000
|
||||||
yield c
|
yield chr(c)
|
||||||
return "".join(aesthetify(m[1])), None
|
return "".join(aesthetify(m[1])), None
|
||||||
|
|
||||||
|
|
||||||
def parse_strikethrough(m):
|
|
||||||
return ("\u0336".join(m[1]) + "\u0336"), None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_enclosing_circle(m):
|
|
||||||
return ("\u20e0".join(m[1]) + "\u20e0"), None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_subreddit(m):
|
def parse_subreddit(m):
|
||||||
text = '/' + m.group(3)
|
text = '/' + m.group(3)
|
||||||
entity = MessageEntityTextUrl(
|
entity = MessageEntityTextUrl(
|
||||||
|
@ -85,8 +78,6 @@ MATCHERS = [
|
||||||
(get_tag_parser('```', partial(MessageEntityPre, language=''))),
|
(get_tag_parser('```', partial(MessageEntityPre, language=''))),
|
||||||
(get_tag_parser('`', MessageEntityCode)),
|
(get_tag_parser('`', MessageEntityCode)),
|
||||||
(re.compile(r'\+\+(.+?)\+\+'), parse_aesthetics),
|
(re.compile(r'\+\+(.+?)\+\+'), parse_aesthetics),
|
||||||
(re.compile(r'~~(.+?)~~'), parse_strikethrough),
|
|
||||||
(re.compile(r'@@(.+?)@@'), parse_enclosing_circle),
|
|
||||||
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
|
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
|
||||||
(re.compile(r'(!\w+)'), parse_snip)
|
(re.compile(r'(!\w+)'), parse_snip)
|
||||||
]
|
]
|
||||||
|
@ -107,8 +98,6 @@ def parse(message, old_entities=None):
|
||||||
# Skip already existing entities if we're at one
|
# Skip already existing entities if we're at one
|
||||||
if i == e.offset:
|
if i == e.offset:
|
||||||
i += e.length
|
i += e.length
|
||||||
else:
|
|
||||||
after += 1
|
|
||||||
|
|
||||||
# Find the first pattern that matches
|
# Find the first pattern that matches
|
||||||
for pattern, parser in MATCHERS:
|
for pattern, parser in MATCHERS:
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
# 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 random
|
|
||||||
|
|
||||||
from telethon import events, types, functions, utils
|
|
||||||
|
|
||||||
|
|
||||||
def choser(cmd, pack, blacklist={}):
|
|
||||||
docs = None
|
|
||||||
@borg.on(events.NewMessage(pattern=rf'\.{cmd}', outgoing=True))
|
|
||||||
async def handler(event):
|
|
||||||
await event.delete()
|
|
||||||
|
|
||||||
nonlocal docs
|
|
||||||
if docs is None:
|
|
||||||
docs = [
|
|
||||||
utils.get_input_document(x)
|
|
||||||
for x in (await borg(functions.messages.GetStickerSetRequest(types.InputStickerSetShortName(pack)))).documents
|
|
||||||
if x.id not in blacklist
|
|
||||||
]
|
|
||||||
|
|
||||||
await event.respond(file=random.choice(docs))
|
|
||||||
|
|
||||||
|
|
||||||
choser('brain', 'supermind')
|
|
||||||
choser('dab', 'DabOnHaters', {
|
|
||||||
1653974154589768377,
|
|
||||||
1653974154589768312,
|
|
||||||
1653974154589767857,
|
|
||||||
1653974154589768311,
|
|
||||||
1653974154589767816,
|
|
||||||
1653974154589767939,
|
|
||||||
1653974154589767944,
|
|
||||||
1653974154589767912,
|
|
||||||
1653974154589767911,
|
|
||||||
1653974154589767910,
|
|
||||||
1653974154589767909,
|
|
||||||
1653974154589767863,
|
|
||||||
1653974154589767852,
|
|
||||||
1653974154589768677
|
|
||||||
})
|
|
|
@ -5,8 +5,6 @@ import regex
|
||||||
from telethon import events, utils
|
from telethon import events, utils
|
||||||
from telethon.tl import types, functions
|
from telethon.tl import types, functions
|
||||||
|
|
||||||
from uniborg import util
|
|
||||||
|
|
||||||
HEADER = "「sed」\n"
|
HEADER = "「sed」\n"
|
||||||
KNOWN_RE_BOTS = re.compile(
|
KNOWN_RE_BOTS = re.compile(
|
||||||
r'(regex|moku|BananaButler_|rgx|l4mR)bot',
|
r'(regex|moku|BananaButler_|rgx|l4mR)bot',
|
||||||
|
@ -19,7 +17,6 @@ KNOWN_RE_BOTS = re.compile(
|
||||||
last_msgs = defaultdict(lambda: deque(maxlen=10))
|
last_msgs = defaultdict(lambda: deque(maxlen=10))
|
||||||
|
|
||||||
|
|
||||||
@util.sync_timeout(1)
|
|
||||||
def doit(chat_id, match, original):
|
def doit(chat_id, match, original):
|
||||||
fr = match.group(1)
|
fr = match.group(1)
|
||||||
to = match.group(2)
|
to = match.group(2)
|
||||||
|
@ -103,7 +100,7 @@ async def on_regex(event):
|
||||||
if m is not None:
|
if m is not None:
|
||||||
s = f"{HEADER}{s}"
|
s = f"{HEADER}{s}"
|
||||||
out = await borg.send_message(
|
out = await borg.send_message(
|
||||||
await event.get_input_chat(), s, reply_to=m.id, parse_mode=None
|
await event.get_input_chat(), s, reply_to=m.id
|
||||||
)
|
)
|
||||||
last_msgs[chat_id].appendleft(out)
|
last_msgs[chat_id].appendleft(out)
|
||||||
elif s is not None:
|
elif s is not None:
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
# 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,18 +14,17 @@ TYPE_DOCUMENT = 2
|
||||||
snips = storage.snips or {}
|
snips = storage.snips or {}
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'(?:\.snip +|!)(\w+)$', 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:
|
||||||
return
|
await on_snip_save(event)
|
||||||
|
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'])
|
||||||
elif snip['type'] == TYPE_DOCUMENT:
|
elif snip['type'] == TYPE_DOCUMENT:
|
||||||
media = types.InputDocument(snip['id'], snip['hash'], file_reference=b'')
|
media = types.InputDocument(snip['id'], snip['hash'])
|
||||||
else:
|
else:
|
||||||
media = None
|
media = None
|
||||||
|
|
||||||
|
@ -35,15 +32,14 @@ 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 not msg:
|
if 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:
|
||||||
|
@ -61,24 +57,27 @@ 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()
|
||||||
|
|
|
@ -1,21 +1,12 @@
|
||||||
# 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 html
|
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon import utils
|
from telethon import utils
|
||||||
from telethon.tl import types
|
from telethon.tl import types
|
||||||
|
|
||||||
|
|
||||||
def get_who_string(who):
|
|
||||||
who_string = html.escape(utils.get_display_name(who))
|
|
||||||
if isinstance(who, (types.User, types.Channel)) and who.username:
|
|
||||||
who_string += f" <i>(@{who.username})</i>"
|
|
||||||
who_string += f", <a href='tg://user?id={who.id}'>#{who.id}</a>"
|
|
||||||
return who_string
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"\.who", outgoing=True))
|
@borg.on(events.NewMessage(pattern=r"\.who", outgoing=True))
|
||||||
async def _(event):
|
async def _(event):
|
||||||
if not event.message.is_reply:
|
if not event.message.is_reply:
|
||||||
|
@ -23,30 +14,14 @@ async def _(event):
|
||||||
else:
|
else:
|
||||||
msg = await event.message.get_reply_message()
|
msg = await event.message.get_reply_message()
|
||||||
if msg.forward:
|
if msg.forward:
|
||||||
# FIXME forward privacy memes
|
|
||||||
who = await borg.get_entity(
|
who = await borg.get_entity(
|
||||||
msg.forward.from_id or msg.forward.channel_id)
|
msg.forward.from_id or msg.forward.channel_id)
|
||||||
else:
|
else:
|
||||||
who = await msg.get_sender()
|
who = await msg.get_sender()
|
||||||
|
|
||||||
await event.edit(get_who_string(who), parse_mode='html')
|
who_string = utils.get_display_name(who)
|
||||||
|
if isinstance(who, (types.User, types.Channel)) and who.username:
|
||||||
|
who_string += f" (@{who.username})"
|
||||||
|
who_string += f", #{who.id}"
|
||||||
|
|
||||||
|
await event.edit(who_string)
|
||||||
@borg.on(events.NewMessage(pattern=r"\.members", outgoing=True))
|
|
||||||
async def _(event):
|
|
||||||
members = []
|
|
||||||
async for member in borg.iter_participants(event.chat_id):
|
|
||||||
messages = await borg.get_messages(
|
|
||||||
event.chat_id,
|
|
||||||
from_user=member,
|
|
||||||
limit=0
|
|
||||||
)
|
|
||||||
members.append((
|
|
||||||
messages.total,
|
|
||||||
f"{messages.total} - {get_who_string(member)}"
|
|
||||||
))
|
|
||||||
members = (
|
|
||||||
m[1] for m in sorted(members, key=lambda m: m[0], reverse=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
await event.edit("\n".join(members), parse_mode='html')
|
|
||||||
|
|
|
@ -10,14 +10,14 @@ from uniborg import util
|
||||||
DELETE_TIMEOUT = 2
|
DELETE_TIMEOUT = 2
|
||||||
|
|
||||||
|
|
||||||
@borg.on(util.admin_cmd(r"^\.(?:re)?load (?P<shortname>\w+)$"))
|
@borg.on(util.admin_cmd(r"^\.load (?P<shortname>\w+)$"))
|
||||||
async def load_reload(event):
|
async def load_reload(event):
|
||||||
await event.delete()
|
await event.delete()
|
||||||
shortname = event.pattern_match["shortname"]
|
shortname = event.pattern_match["shortname"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if shortname in borg._plugins:
|
if shortname in borg._plugins:
|
||||||
await borg.remove_plugin(shortname)
|
borg.remove_plugin(shortname)
|
||||||
borg.load_plugin(shortname)
|
borg.load_plugin(shortname)
|
||||||
|
|
||||||
msg = await event.respond(
|
msg = await event.respond(
|
||||||
|
@ -39,7 +39,7 @@ async def remove(event):
|
||||||
if shortname == "_core":
|
if shortname == "_core":
|
||||||
msg = await event.respond(f"Not removing {shortname}")
|
msg = await event.respond(f"Not removing {shortname}")
|
||||||
elif shortname in borg._plugins:
|
elif shortname in borg._plugins:
|
||||||
await borg.remove_plugin(shortname)
|
borg.remove_plugin(shortname)
|
||||||
msg = await event.respond(f"Removed plugin {shortname}")
|
msg = await event.respond(f"Removed plugin {shortname}")
|
||||||
else:
|
else:
|
||||||
msg = await event.respond(f"Plugin {shortname} is not loaded")
|
msg = await event.respond(f"Plugin {shortname} is not loaded")
|
||||||
|
|
|
@ -48,6 +48,6 @@ class Storage:
|
||||||
|
|
||||||
def _save(self):
|
def _save(self):
|
||||||
if not self._root.is_dir():
|
if not self._root.is_dir():
|
||||||
self._root.mkdir(parents=True, exist_ok=True)
|
self._root(parents=True, exist_ok=True)
|
||||||
with open(self._root / FILE_NAME, 'w') as fp:
|
with open(self._root / FILE_NAME, 'w') as fp:
|
||||||
json.dump(self._data, fp)
|
json.dump(self._data, fp)
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
# 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
|
import asyncio
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import inspect
|
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -72,7 +71,7 @@ class Uniborg(TelegramClient):
|
||||||
self._plugins[shortname] = mod
|
self._plugins[shortname] = mod
|
||||||
self._logger.info(f"Successfully loaded plugin {shortname}")
|
self._logger.info(f"Successfully loaded plugin {shortname}")
|
||||||
|
|
||||||
async def remove_plugin(self, shortname):
|
def remove_plugin(self, shortname):
|
||||||
name = self._plugins[shortname].__name__
|
name = self._plugins[shortname].__name__
|
||||||
|
|
||||||
for i in reversed(range(len(self._event_builders))):
|
for i in reversed(range(len(self._event_builders))):
|
||||||
|
@ -80,16 +79,7 @@ class Uniborg(TelegramClient):
|
||||||
if cb.__module__ == name:
|
if cb.__module__ == name:
|
||||||
del self._event_builders[i]
|
del self._event_builders[i]
|
||||||
|
|
||||||
plugin = self._plugins.pop(shortname)
|
del self._plugins[shortname]
|
||||||
if callable(getattr(plugin, 'unload', None)):
|
|
||||||
try:
|
|
||||||
unload = plugin.unload()
|
|
||||||
if inspect.isawaitable(unload):
|
|
||||||
await unload
|
|
||||||
except Exception:
|
|
||||||
self._logger.exception(f'Unhandled exception unloading {shortname}')
|
|
||||||
|
|
||||||
del plugin
|
|
||||||
self._logger.info(f"Removed plugin {shortname}")
|
self._logger.info(f"Removed plugin {shortname}")
|
||||||
|
|
||||||
def await_event(self, event_matcher, filter=None):
|
def await_event(self, event_matcher, filter=None):
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
# 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 functools
|
|
||||||
import re
|
import re
|
||||||
import signal
|
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon.tl.functions.messages import GetPeerDialogsRequest
|
from telethon.tl.functions.messages import GetPeerDialogsRequest
|
||||||
|
@ -30,20 +28,3 @@ async def is_read(borg, entity, message, is_out=None):
|
||||||
dialog = (await borg(GetPeerDialogsRequest([entity]))).dialogs[0]
|
dialog = (await borg(GetPeerDialogsRequest([entity]))).dialogs[0]
|
||||||
max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id
|
max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id
|
||||||
return message_id <= max_id
|
return message_id <= max_id
|
||||||
|
|
||||||
def _handle_timeout(signum, frame):
|
|
||||||
raise TimeoutError("Execution took too long")
|
|
||||||
|
|
||||||
def sync_timeout(seconds):
|
|
||||||
def decorator(func):
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
signal.signal(signal.SIGALRM, _handle_timeout)
|
|
||||||
signal.setitimer(signal.ITIMER_REAL, seconds)
|
|
||||||
try:
|
|
||||||
r = func(*args, **kwargs)
|
|
||||||
finally:
|
|
||||||
signal.alarm(0)
|
|
||||||
return r
|
|
||||||
return wrapper
|
|
||||||
return decorator
|
|
||||||
|
|
Loading…
Reference in New Issue