浏览代码

Implementing the cat command

Kirk Trombley 5 年之前
父节点
当前提交
22e0c6a430
共有 2 个文件被更改,包括 31 次插入19 次删除
  1. 11 2
      lib/rollbot/messaging.py
  2. 20 17
      src/plugins/querying.py

+ 11 - 2
lib/rollbot/messaging.py

@@ -163,14 +163,23 @@ class RollbotFailure(Enum):
     INTERNAL_ERROR = auto()
 
     def get_debugging(self):
+        debugging = {}
         reason = getattr(self, "reason", None)
-        return {} if reason is None else { "explain": reason }
-        
+        if reason is not None:
+            debugging["explain"] = reason
+        exception = getattr(self, "exception", None)
+        if exception is not None:
+            debugging["exception"] = exception
+        return debugging
 
     def with_reason(self, reason):
         self.reason = reason
         return self
 
+    def with_exception(self, exception):
+        self.exception = exception
+        return self
+
 
 _RESPONSE_TEMPLATE = """Response{
     Original Message: %s,

+ 20 - 17
src/plugins/querying.py

@@ -202,26 +202,29 @@ def fortune():
     return f"'{quote}'\nLucky Numbers: {lotto}\nLearn Chinese: {learn}"
 
 
+def run_upload(message, img_data):
+    success, result = upload_image(get_secret("imgur_client_id"), img_data)
+    if success:
+        return RollbotResponse(message, img=result)
+    else:
+        return RollbotFailure.SERVICE_DOWN.with_reason(result["explain"]).with_exception(result["exception"])
+
+
 @as_plugin
 def selfie(message):
     try:
         r = requests.get("https://cdn.star.nesdis.noaa.gov/GOES16/ABI/SECTOR/ne/GEOCOLOR/latest.jpg")
     except ConnectionError as e:
-        return RollbotResponse(
-            message,
-            failure=RollbotFailure.SERVICE_DOWN,
-            debugging={
-                "explain": "Could not reach GOES16.",
-                "exception": e
-            }
-        )
+        return RollbotFailure.SERVICE_DOWN.with_reason("Could not reach GOES16.").with_exception(e)
 
-    success, result = upload_image(get_secret("imgur_client_id"), r.content)
-    if success:
-        return RollbotResponse(message, img=result)
-    else:
-        return RollbotResponse(
-            message,
-            failure=RollbotFailure.SERVICE_DOWN,
-            debugging=result
-        )
+    return run_upload(message, r.content)
+
+@as_plugin
+def cat(message):
+    try:
+        r = requests.get("https://thiscatdoesnotexist.com/", headers={ "User-Agent": "Rollbot" })
+    except ConnectionError as e:
+        return RollbotFailure.SERVICE_DOWN.with_reason("Could not reach cat generator.").with_exception(e)
+
+    return run_upload(message, r.content)
+