Ver Fonte

Converting teamspeak plugin to use actual class

Kirk Trombley há 6 anos atrás
pai
commit
428925e5cb
1 ficheiros alterados com 40 adições e 34 exclusões
  1. 40 34
      src/plugins/teamspeak.py

+ 40 - 34
src/plugins/teamspeak.py

@@ -1,6 +1,7 @@
+import logging
 from telnetlib import Telnet
 
-from command_system import RollbotResponse, RollbotFailure, as_plugin
+from command_system import RollbotResponse, RollbotFailure, RollbotPlugin
 from config import get_config, get_secret
 
 TS3_HOST = get_config("teamspeak.host")
@@ -9,38 +10,43 @@ _TS3_PASS = get_secret("teamspeak.pass")
 TS3_LOGIN = ("login %s %s\n" % (_TS3_USER, _TS3_PASS)).encode("utf-8")
 
 
-@as_plugin
-def teamspeak(db, message):
-    try:
-        with Telnet(TS3_HOST, 10011, 5) as tn:
-            tn.write(TS3_LOGIN)
-            tn.write(b"use 1 -virtual\n")
-            tn.write(b"clientlist\n")
-            tn.write(b"whoami\n")
-            tn.write(b"quit\n")
-            response = tn.read_until(b"virtualserver_status").decode("ascii")
-    except Exception as e:
-        return RollbotResponse(
-            message,
-            failure=RollbotFailure.SERVICE_DOWN,
-            debugging={
-                "explain": "Encountered failure during Telnet calls.",
-                "exception": e
-            }
-        )
+class teamspeak(RollbotPlugin):
+    def __init__(self, command, logger=logging.getLogger(__name__)):
+        RollbotPlugin.__init__(self, "teamspeak", logger)
+        self.logger.info(f"Intializing Teamspeak command")
 
-    if "banned" in response:
-        return RollbotResponse(
-            message,
-            failure=RollbotFailure.SERVICE_DOWN,
-            debugging={"explain": "Rate limiting issue."}
-        )
+    def on_command(self, db, message):
+        try:
+            with Telnet(TS3_HOST, 10011, 5) as tn:
+                tn.write(TS3_LOGIN)
+                tn.write(b"use 1 -virtual\n")
+                tn.write(b"clientlist\n")
+                tn.write(b"whoami\n")
+                tn.write(b"quit\n")
+                response = tn.read_until(b"virtualserver_status").decode("ascii")
+        except Exception as e:
+            self.logger.error("Teamspeak failure.", e)
+            return RollbotResponse(
+                message,
+                failure=RollbotFailure.SERVICE_DOWN,
+                debugging={
+                    "explain": "Encountered failure during Telnet calls.",
+                    "exception": e
+                }
+            )
 
-    nicks = response.split()
-    nicks = [x.split("=")[1] for x in nicks if x.startswith("client_nickname")]
-    nicks = [y for y in nicks if "serveradmin" not in y]
-    if len(nicks) == 0:
-        return RollbotResponse(message, txt="No one in teamspeak!")
-    if len(nicks) == 1:
-        return RollbotResponse(message, txt="Only "+nicks[0])
-    return RollbotResponse(message, txt="I see the following people: "+", ".join(nicks))
+        if "banned" in response:
+            return RollbotResponse(
+                message,
+                failure=RollbotFailure.SERVICE_DOWN,
+                debugging={"explain": "Rate limiting issue."}
+            )
+
+        nicks = response.split()
+        nicks = [x.split("=")[1] for x in nicks if x.startswith("client_nickname")]
+        nicks = [y for y in nicks if "serveradmin" not in y]
+        if len(nicks) == 0:
+            return RollbotResponse(message, txt="No one in teamspeak!")
+        if len(nicks) == 1:
+            return RollbotResponse(message, txt="Only "+nicks[0])
+        return RollbotResponse(message, txt="I see the following people: "+", ".join(nicks))