mirror of
https://github.com/Nick80835/microbot
synced 2025-08-29 05:29:16 +00:00
simplify command handling
This commit is contained in:
parent
11aeebc7f4
commit
fba27e799b
@ -8,31 +8,30 @@ from telethon import events
|
||||
class CommandHandler():
|
||||
def __init__(self, client, logger, settings):
|
||||
self.pattern_template = "(?is)^{0}{1}(?: |$)(.*)"
|
||||
self.outgoing_commands = {}
|
||||
self.incoming_commands = {}
|
||||
self.logger = logger
|
||||
self.settings = settings
|
||||
client.add_event_handler(self.handle_incoming, events.NewMessage(incoming=True))
|
||||
|
||||
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():
|
||||
pattern = self.pattern_template.format("" if value["noprefix"] else prefix, key)
|
||||
|
||||
if search(pattern, event.text):
|
||||
event.pattern_match = search(pattern, event.text)
|
||||
pattern_match = search(self.pattern_template.format("" if value["noprefix"] else prefix, key), event.text)
|
||||
|
||||
if pattern_match:
|
||||
if value["sudo"] and str(event.from_id) != self.settings.get_config("owner_id"):
|
||||
print(f"Attempted sudo command ({event.text}) from ID {event.from_id}")
|
||||
return
|
||||
continue
|
||||
|
||||
event.pattern_match = pattern_match
|
||||
|
||||
try:
|
||||
await value["function"](event)
|
||||
return
|
||||
except Exception as exception:
|
||||
self.logger.warn(f"{value['function'].__name__} - {exception}")
|
||||
await event.reply(f"`An error occurred in {value['function'].__name__}: {exception}`")
|
||||
raise exception
|
||||
|
||||
break
|
||||
|
@ -27,7 +27,6 @@ class Loader():
|
||||
self.loaded_modules.append(import_module("ubot.modules." + module_name))
|
||||
|
||||
def reload_all_modules(self):
|
||||
self.command_handler.outgoing_commands = {}
|
||||
self.command_handler.incoming_commands = {}
|
||||
|
||||
errors = ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user