Эх сурвалжийг харах

Adding mechanisms for including help messages

Kirk Trombley 5 жил өмнө
parent
commit
b38ba221b0

+ 1 - 1
lib/rollbot/__init__.py

@@ -1,4 +1,4 @@
 from .bot import Rollbot
 from .messaging import pop_arg, RollbotMessage, RollbotFailure, RollbotResponse
 from .database import as_group_singleton, ModelBase
-from .plugins import as_plugin, RollbotPlugin
+from .plugins import as_plugin, RollbotPlugin, with_help

+ 7 - 0
lib/rollbot/bot.py

@@ -97,6 +97,13 @@ class Rollbot:
             self.logger.warn(f"Tried to run non-command message {message.message_id}")
             return RollbotResponse(message, failure=RollbotFailure.INTERNAL_ERROR)
 
+        if message.command == "help":
+            topic = next(message.args())
+            targeted = self.commands.get(topic, None)
+            if targeted is None:
+                return RollbotResponse(message, failure=RollbotFailure.INVALID_ARGUMENTS, debugging={"explain": f"Could not find command {topic}"})
+            return RollbotResponse(message, txt=targeted.help_msg())
+
         plugin = self.commands.get(message.command, None)
 
         if plugin is None:

+ 13 - 0
lib/rollbot/plugins.py

@@ -21,6 +21,9 @@ class RollbotPlugin:
     def on_command(self, db, message):
         raise NotImplementedError
 
+    def help_msg(self):
+        return f"No help message provided for {self.command} command!"
+
     @staticmethod
     def find_all_plugins():
         def get_subclasses(clazz):
@@ -90,3 +93,13 @@ def as_plugin(command):
         return decorator
     else:
         return decorator(command)
+
+def with_help(help_msg):
+    def getter(self):
+        return help_msg
+    
+    def decorator(cls):
+        setattr(cls, "help_msg", getter)
+        return cls
+
+    return decorator

+ 9 - 24
src/plugins/querying.py

@@ -5,31 +5,20 @@ import time
 import requests
 from requests_html import HTMLSession
 
-from rollbot import RollbotResponse, RollbotFailure, as_plugin, pop_arg
+from rollbot import RollbotResponse, RollbotFailure, as_plugin, pop_arg, with_help
 
 from config import get_config, get_secret
 
 from .util import upload_image
 
+@with_help("Using the command !inspire will request an inspiration image from http://inspirobot.me/")
 @as_plugin
 def inspire(message):
-    argstr = message.raw_args
-
-    if argstr is not None and "!explain" in argstr:
-        return "Using the command !inspire will request an inspiration image from http://inspirobot.me/"
-
     try:
         url = requests.get("http://inspirobot.me/api?generate=true").text
         return RollbotResponse(message, img=url)
     except ConnectionError as e:
-        return RollbotResponse(
-            message,
-            failure=RollbotFailure.SERVICE_DOWN,
-            debugging={
-                "explain": "Could not reach inspirobot.",
-                "exception": e
-            }
-        )
+        return RollbotFailure.SERVICE_DOWN.with_reason("Could not reach inspirobot.").with_exception(e)
 
 
 @as_plugin
@@ -220,10 +209,9 @@ def selfie(message):
     return run_upload(message, r.content)
 
 
+@with_help("The !cat command grabs a cat from https://thiscatdoesnotexist.com/")
 @as_plugin
 def cat(message):
-    if message.raw_args is not None and "!explain" in message.raw_args:
-        return "The !cat command grabs a cat from https://thiscatdoesnotexist.com/"
     try:
         r = requests.get("https://thiscatdoesnotexist.com/", headers={ "User-Agent": "Rollbot" })
     except ConnectionError as e:
@@ -232,10 +220,9 @@ def cat(message):
     return run_upload(message, r.content)
 
 
+@with_help("The !npc command grabs a person from https://thispersondoesnotexist.com/")
 @as_plugin
 def npc(message):
-    if message.raw_args is not None and "!explain" in message.raw_args:
-        return "The !npc command grabs a person from https://thispersondoesnotexist.com/"
     try:
         r = requests.get("https://thispersondoesnotexist.com/image", headers={ "User-Agent": "Rollbot" })
     except ConnectionError as e:
@@ -244,14 +231,12 @@ def npc(message):
     return run_upload(message, r.content)
 
 
+@with_help("The !imagine command uses the text2img API at https://deepai.org/machine-learning-model/text2img")
 @as_plugin
 def imagine(message):
     if message.raw_args is None:
         return RollbotFailure.INVALID_ARGUMENTS.with_reason("The !imagine command needs text to imagine!")
 
-    if "!explain" in message.raw_args:
-        return "The !imagine command uses the text2img API at https://deepai.org/machine-learning-model/text2img"
-
     try:
         r = requests.post(
             "https://api.deepai.org/api/text2img",
@@ -270,11 +255,11 @@ def imagine(message):
 
 art_number = 0
 
+@with_help("The !art command uses the 9gans gallery at https://9gans.com/ which generates 9 images every hour. \
+This command will cycle through those 9 images, so if you fire it 10 times in quick succession, the tenth \
+piece of art might be the same as the first.")
 @as_plugin
 def art(message):
-    if message.raw_args is not None and "!explain" in message.raw_args:
-        return "The !art command uses the 9gans gallery at https://9gans.com/ which generates 9 images every hour."
-
     global art_number
     art_number += 1
     art_number %= 9

+ 2 - 1
src/plugins/roll.py

@@ -1,6 +1,6 @@
 import dice
 
-from rollbot import as_plugin
+from rollbot import as_plugin, with_help
 
 
 @as_plugin
@@ -28,6 +28,7 @@ def rollstatsdumb():
     return f"Rolled dumb d20 stats, got: {', '.join(str(int(dice.roll('d20'))) for _ in range(6))}"
 
 
+@with_help("The !character command generates a DnD character by firing 3 commands into the chat.")
 @as_plugin
 def character(message, bot):
     if message.raw_args is not None and "d20" in message.raw_args: