diff --git a/main.py b/main.py index bbaf429..fb42399 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +from functools import wraps import logging import os import re @@ -21,6 +22,14 @@ link_regex = re.compile( '((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)', re.DOTALL) +def private_only(f): + @wraps(f) + def cb(update: Update, *args, **kwargs): + if update.effective_chat.type != 'private': + return + return f(update, *args, **kwargs) + return cb + @dataclass class BotSettings: token: str @@ -41,7 +50,7 @@ class Bot: for f in dir(self): if f.startswith("cmd_"): self.dispatcher.add_handler( - CommandHandler(f[4:], getattr(self, f))) + CommandHandler(f[4:], private_only(getattr(self, f)))) def _register_handlers(self): self.dispatcher.add_handler(MessageHandler( @@ -90,7 +99,10 @@ class Bot: return f"{n} {singular}" return f"{n} {plural}" + @private_only def message(self, update: Update, context: CallbackContext): + if update.effective_chat.type != 'private': + return user_id = update.effective_user.id links = re.findall(link_regex, update.message.text) with self.db as db: