aboutsummaryrefslogtreecommitdiff
path: root/commands/handling.py
diff options
context:
space:
mode:
authorromangraef <roman.graef@gmail.com>2018-03-17 22:11:47 +0100
committerromangraef <roman.graef@gmail.com>2018-03-17 22:11:47 +0100
commit50afa23b9dc26f914522a55fe5cbb893d34ad27c (patch)
tree9711c076e4af283523f51fbd72ceb67bde4f5366 /commands/handling.py
parent473d84ec2c82932fa1c09e4b96d6e8198bbb71df (diff)
downloadtelegramuserbot-master.tar.gz
telegramuserbot-master.tar.bz2
telegramuserbot-master.zip
added todos as well as regex based command triggers.HEADmaster
Diffstat (limited to 'commands/handling.py')
-rw-r--r--commands/handling.py35
1 files changed, 30 insertions, 5 deletions
diff --git a/commands/handling.py b/commands/handling.py
index 46cdb66..9c9e864 100644
--- a/commands/handling.py
+++ b/commands/handling.py
@@ -19,9 +19,11 @@ def load_module(module):
for func in functions:
if lib.is_command(func):
lib.register_command(func)
+ if lib.is_match_script(func):
+ lib.register_match_script(func)
-def load_commands(folder='modules'):
+def load_modules(folder='modules'):
for dirname, dirnames, filenames in os.walk(folder):
for filename in filenames:
filename: str
@@ -32,10 +34,14 @@ def load_commands(folder='modules'):
load_module(module)
-def handle_commands(client: pyrogram.Client, update, users, chats):
- if not (isinstance(update, tgtypes.UpdateNewMessage)
+def is_message_update(update):
+ return (isinstance(update, tgtypes.UpdateNewMessage)
or isinstance(update, tgtypes.UpdateNewChannelMessage)
- or isinstance(update, tgtypes.UpdateNewEncryptedMessage)):
+ or isinstance(update, tgtypes.UpdateNewEncryptedMessage))
+
+
+def handle_commands(client: pyrogram.Client, update, users, chats):
+ if not is_message_update(update):
return
update: tgtypes.UpdateNewMessage
message: tgtypes.Message = update.message
@@ -51,7 +57,7 @@ def handle_commands(client: pyrogram.Client, update, users, chats):
return
command = parts[0][1:]
args = parts[1:]
- cmd_func = lib.commands[command.lower()]
+ cmd_func = lib.get_command_by_name(command.lower())
ctx = lib.CommandContext(client=client, channel=message.to_id, args=args, message=message)
try:
cmd_func(ctx)
@@ -60,3 +66,22 @@ def handle_commands(client: pyrogram.Client, update, users, chats):
except Exception as e:
ctx.respond("unknown exception during execution. Error will be DM'd" + str(e))
print(traceback.format_exc(), file=sys.stderr)
+
+
+def handle_match_scripts(client: pyrogram.Client, update, users, chats):
+ if not is_message_update(update):
+ return
+ update: tgtypes.UpdateNewMessage
+ message: tgtypes.Message = update.message
+ author_id = message.from_id
+ if author_id != client.user_id:
+ # do not react to other people
+ return
+ text: str = message.message
+ for regex, func in lib.get_match_scripts().items():
+ match = regex.match(text)
+ if match is None:
+ continue
+ ctx = lib.MatchContext(client=client, channel=message.to_id, message=message,
+ match=match, groups=match.groups(), named_groups=match.groupdict())
+ func(ctx)