Browse Source

Add minecraft command, fix python 10 whining

Kirk Trombley 3 years ago
parent
commit
a54783d25f
2 changed files with 16 additions and 1 deletions
  1. 15 0
      commands/commands/querying.py
  2. 1 1
      rollbot/rollbot/decorators/as_command.py

+ 15 - 0
commands/commands/querying.py

@@ -235,3 +235,18 @@ async def imagine(args: Args, api_key: Config("text2img.api_key"), req: Request,
     except:
         logger.exception("Failed to call text2img service")
         RollbotFailure.SERVICE_DOWN.raise_exc(detail="Failed to call text2img service")
+
+
+@as_command
+async def minecraft(minecraft_url: Config("minecraft.url"), req: Request):
+    try:
+        async with req.get(minecraft_url) as res:
+            res.raise_forstatus()
+            js = await res.json()
+            if not js['online']:
+                return "Server is offline!"
+            data = js['data']
+            return f"I see {data['currentPlayers']} people playing {data['motd']}"
+    except:
+        logger.exception("Failed to call MC service")
+        RollbotFailure.SERVICE_DOWN.raise_exc(detail="Failed to query Minecraft server")

+ 1 - 1
rollbot/rollbot/decorators/as_command.py

@@ -111,7 +111,7 @@ def _on_command_impl(name: str, fn: Callable[..., Any]) -> Callable[..., Any]:
     return fn
 
 
-def as_command(arg: Union[str, Callable[...]]) -> Callable[...]:
+def as_command(arg: Union[str, Callable]) -> Callable:
     if isinstance(arg, str):
         return lambda fn: _on_command_impl(arg, fn)
     else: