From d53051e6f3b713d80ce175f98861a2f9eac14312 Mon Sep 17 00:00:00 2001 From: Davte Date: Tue, 16 Jul 2019 16:38:53 +0200 Subject: [PATCH] Class method `get` defined. --- davtelepot/bot.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/davtelepot/bot.py b/davtelepot/bot.py index 2ce1394..4cb8e8b 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -218,6 +218,18 @@ class Bot(TelegramBot, ObjectWithDatabase): """Set instance path attribute.""" self._path = path + @classmethod + def get(cls, token, *args, **kwargs): + """Given a `token`, return class instance with that token. + + If no instance is found, instantiate it. + Positional and keyword arguments may be passed as well. + """ + for bot in cls.bots: + if bot.token == token: + return bot + return cls(token, *args, **kwargs) + @property def hostname(self): """Hostname for the webhook URL.