Queer European MD passionate about IT
瀏覽代碼

Class method `get` defined.

Davte 5 年之前
父節點
當前提交
d53051e6f3
共有 1 個文件被更改,包括 12 次插入0 次删除
  1. 12 0
      davtelepot/bot.py

+ 12 - 0
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.