mirror of
https://github.com/Nick80835/microbot
synced 2025-09-01 15:05:48 +00:00
rework inline photos significantly
This commit is contained in:
@@ -10,6 +10,7 @@ class CommandHandler():
|
||||
def __init__(self, client, logger, settings):
|
||||
self.username = client.loop.run_until_complete(client.get_me()).username
|
||||
self.pattern_template = "(?is)^{0}{1}(?: |$|_|@{2}(?: |$|_))(.*)"
|
||||
self.inline_pattern_template = "(?is)^{0}(?: |$|_)(.*)"
|
||||
self.incoming_commands = {}
|
||||
self.inline_photo_commands = {}
|
||||
self.logger = logger
|
||||
@@ -18,9 +19,6 @@ class CommandHandler():
|
||||
client.add_event_handler(self.handle_inline_photo, events.InlineQuery())
|
||||
|
||||
async def handle_incoming(self, event):
|
||||
if event.via_bot_id:
|
||||
return
|
||||
|
||||
prefix = escape(self.settings.get_config("cmd_prefix") or '.')
|
||||
|
||||
for key, value in self.incoming_commands.items():
|
||||
@@ -44,29 +42,44 @@ class CommandHandler():
|
||||
|
||||
|
||||
async def handle_inline_photo(self, event):
|
||||
builder = event.builder
|
||||
result_list = []
|
||||
fetch_coros = []
|
||||
pattern_match = None
|
||||
|
||||
async def exec_coro(coro):
|
||||
this_url = await coro
|
||||
for key, value in self.inline_photo_commands.items():
|
||||
pattern_match = search(self.inline_pattern_template.format(key), event.text)
|
||||
|
||||
if pattern_match:
|
||||
builder = event.builder
|
||||
event.pattern_match = pattern_match
|
||||
event.args = pattern_match.groups()[-1]
|
||||
|
||||
url_list = await value["function"](event)
|
||||
|
||||
if not url_list:
|
||||
continue
|
||||
|
||||
photo_coros = []
|
||||
|
||||
for url in url_list:
|
||||
try:
|
||||
photo_coros += [self.try_coro(builder.photo(url))]
|
||||
except:
|
||||
pass
|
||||
|
||||
if photo_coros:
|
||||
photos = await asyncio.gather(*photo_coros)
|
||||
else:
|
||||
continue
|
||||
|
||||
if this_url:
|
||||
try:
|
||||
return await builder.photo(this_url)
|
||||
await event.answer([i for i in photos if i])
|
||||
except:
|
||||
return
|
||||
else:
|
||||
return
|
||||
pass
|
||||
|
||||
for _, value in self.inline_photo_commands.items():
|
||||
fetch_coros += [exec_coro(value["function"](event.text))]
|
||||
|
||||
for result in await asyncio.gather(*fetch_coros):
|
||||
if result:
|
||||
result_list += [result]
|
||||
if not pattern_match:
|
||||
await event.answer([await event.builder.article(title=key, text=f"{self.settings.get_config('cmd_prefix') or '.'}{value['default']}") for key, value in self.inline_photo_commands.items() if value["default"]])
|
||||
|
||||
async def try_coro(self, coro):
|
||||
try:
|
||||
await event.answer(result_list)
|
||||
return await coro
|
||||
except:
|
||||
pass
|
||||
return
|
||||
|
Reference in New Issue
Block a user