2020-03-16 21:02:05 -04:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-05-16 17:52:29 -04:00
|
|
|
import asyncio
|
2023-10-16 09:46:31 -04:00
|
|
|
from re import escape, search
|
|
|
|
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
|
|
|
|
class CommandHandler():
|
2020-04-16 16:26:05 -04:00
|
|
|
def __init__(self, client, logger, settings):
|
2020-04-24 10:57:54 -04:00
|
|
|
self.username = client.loop.run_until_complete(client.get_me()).username
|
2020-04-28 14:57:08 -04:00
|
|
|
self.pattern_template = "(?is)^{0}{1}(?: |$|_|@{2}(?: |$|_))(.*)"
|
2023-10-16 09:46:31 -04:00
|
|
|
self.incoming_commands = {}
|
2020-05-16 17:52:29 -04:00
|
|
|
self.inline_photo_commands = {}
|
2023-10-16 09:46:31 -04:00
|
|
|
self.logger = logger
|
2020-04-16 16:26:05 -04:00
|
|
|
self.settings = settings
|
2023-10-16 09:46:31 -04:00
|
|
|
client.add_event_handler(self.handle_incoming, events.NewMessage(incoming=True))
|
2020-05-16 17:52:29 -04:00
|
|
|
client.add_event_handler(self.handle_inline_photo, events.InlineQuery())
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-04-16 16:26:05 -04:00
|
|
|
async def handle_incoming(self, event):
|
2020-04-17 16:47:19 -04:00
|
|
|
if event.via_bot_id:
|
|
|
|
return
|
|
|
|
|
2020-04-16 16:26:05 -04:00
|
|
|
prefix = escape(self.settings.get_config("cmd_prefix") or '.')
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-04-16 16:26:05 -04:00
|
|
|
for key, value in self.incoming_commands.items():
|
2020-04-24 10:57:54 -04:00
|
|
|
pattern_match = search(self.pattern_template.format("" if value["noprefix"] else prefix, key, self.username), event.text)
|
2020-04-16 16:26:05 -04:00
|
|
|
|
2020-04-17 16:47:19 -04:00
|
|
|
if pattern_match:
|
2020-04-17 16:54:42 -04:00
|
|
|
if value["sudo"] and str(event.from_id) not in self.settings.get_config("owner_id").split(","):
|
2020-04-16 16:26:05 -04:00
|
|
|
print(f"Attempted sudo command ({event.text}) from ID {event.from_id}")
|
2020-04-17 16:47:19 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
event.pattern_match = pattern_match
|
2020-05-02 16:40:26 -04:00
|
|
|
event.args = pattern_match.groups()[-1]
|
2020-05-05 19:51:55 -04:00
|
|
|
event.extras = value["extras"]
|
2023-10-16 09:46:31 -04:00
|
|
|
|
|
|
|
try:
|
2020-04-16 16:26:05 -04:00
|
|
|
await value["function"](event)
|
2023-10-16 09:46:31 -04:00
|
|
|
except Exception as exception:
|
2020-04-16 16:26:05 -04:00
|
|
|
self.logger.warn(f"{value['function'].__name__} - {exception}")
|
|
|
|
await event.reply(f"`An error occurred in {value['function'].__name__}: {exception}`")
|
2020-02-23 15:04:44 -05:00
|
|
|
raise exception
|
2020-05-16 17:52:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
async def handle_inline_photo(self, event):
|
|
|
|
builder = event.builder
|
|
|
|
result_list = []
|
|
|
|
fetch_coros = []
|
|
|
|
|
|
|
|
async def exec_coro(coro):
|
|
|
|
this_url = await coro
|
|
|
|
|
|
|
|
if this_url:
|
|
|
|
try:
|
|
|
|
return await builder.photo(this_url)
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
try:
|
|
|
|
await event.answer(result_list)
|
|
|
|
except:
|
|
|
|
pass
|