Bläddra i källkod

Added a way to do simple commands as pure configuration

Kirk Trombley 6 år sedan
förälder
incheckning
0961609d0f
4 ändrade filer med 31 tillägg och 44 borttagningar
  1. 17 2
      config/config.toml
  2. 1 0
      src/config.py
  3. 0 33
      src/plugins/simple.py
  4. 13 9
      src/rollbot.py

+ 17 - 2
config/config.toml

@@ -1,8 +1,8 @@
 database = "/tmp/rollbot.sqlite"
-sleep_time = 2.0
+sleep_time = 1.5
 
 [plugins]
-simple = [ "info", "isadmin", "debug", "echo", "thanks", "guess", "meme", "unmeme", "greet", "console" ]
+simple = [ "isadmin", "debug", "echo", "greet", "console" ]
 teamspeak = [ "teamspeak", "speamteek", "teamscream", ]
 session = [ "session" ]
 roll = [ "roll" ]
@@ -16,6 +16,21 @@ speemteek = "speamteek"
 speemteak = "speamteek"
 interpret = "inspire"
 horoscope = "inspire"
+lomg = "bump"
+mute = "bump"
+
+[responses]
+info = """
+Hello!
+I am Rollbot 3.0 - a simple chatbot built in Python.
+I am licensed as open source under the MIT license.
+"""
+thanks = "You're welcome!"
+guess = "Rollbot's back - tell a friend!"
+meme = "fuck off"
+unmeme = "get a job"
+issues = "https://kirkleon.ddns.net/gogs/kirkleon/rollbot3/issues"
+bump = "Bumping the chat!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nAll done!"
 
 [teamspeak]
 host = "kirkleon.ddns.net"

+ 1 - 0
src/config.py

@@ -31,6 +31,7 @@ GLOBAL_ADMINS = get_secret("auths.global")
 GROUP_ADMINS = get_secret("auths.group")
 PLUGINS = get_config("plugins")
 ALIASES = get_config("aliases")
+RESPONSES = get_config("responses")
 API_KEY = get_secret("api_key")
 DB_FILE = os.path.abspath(get_config("database"))
 SLEEP_TIME = float(get_config("sleep_time"))

+ 0 - 33
src/plugins/simple.py

@@ -3,19 +3,6 @@ import random
 from command_system import as_plugin, RollbotResponse
 
 
-HELP_MSG = """
-Hello!
-I am Rollbot 3.0 - a reimplementation of the previous Rollbot.
-I am a simple chatbot built in Python.
-I am licensed as open source under the MIT license.
-"""
-
-
-@as_plugin
-def info(db, message):
-    return RollbotResponse(message, txt=HELP_MSG)
-
-
 @as_plugin
 def isadmin(db, message):
     return RollbotResponse(message, txt=str(message.from_admin))
@@ -31,26 +18,6 @@ def echo(db, message):
     return RollbotResponse(message, txt="'%s' - %s" % (message.raw_args, message.name))
 
 
-@as_plugin
-def thanks(db, message):
-    return RollbotResponse(message, txt="You're welcome!")
-
-
-@as_plugin
-def guess(db, message):
-    return RollbotResponse(message, txt="Rollbot's back - tell a friend!")
-
-
-@as_plugin
-def meme(db, message):
-    return RollbotResponse(message, txt="fuck off")
-
-
-@as_plugin
-def unmeme(db, message):
-    return RollbotResponse(message, txt="get a job")
-
-
 @as_plugin
 def greet(db, message):
     return RollbotResponse(message, txt=random.choice(("Hi!", "Hello!", "안녕하세요", "こんにちは", "你好", "👋")))

+ 13 - 9
src/rollbot.py

@@ -1,7 +1,7 @@
 import importlib
 
 import db
-from config import PLUGINS, ALIASES, BOTS_LOOKUP
+from config import PLUGINS, ALIASES, RESPONSES, BOTS_LOOKUP
 from command_system import RollbotResponse, RollbotFailure
 
 
@@ -40,16 +40,20 @@ class Rollbot:
             self.logger.warn(f"Tried to run non-command message {message.message_id}")
             return
 
-        if message.command in ALIASES:
-            plugin = self.commands.get(ALIASES[message.command], None)
-        else:
-            plugin = self.commands.get(message.command, None)
+        # if this command is aliased, resolve that first, otherwise use the literal command
+        cmd = ALIASES.get(message.command, message.command)
+
+        if cmd in RESPONSES:
+            return RollbotResponse(message, txt=RESPONSES[cmd])
+
+        plugin = self.commands.get(cmd, None)
 
         if plugin is None:
-            response = RollbotResponse(message, failure=RollbotFailure.INVALID_COMMAND)
-        else:
-            with db.session_scope() as session:
-                response = plugin.on_command(session, message)
+            self.logger.warn(f"Message {message.message_id} had a command {message.command} (resolved to {cmd}) that could not be run.")
+            return RollbotResponse(message, failure=RollbotFailure.INVALID_COMMAND)
+
+        with db.session_scope() as session:
+            response = plugin.on_command(session, message)
 
         if not response.is_success:
             self.logger.warn(f"Message {message.message_id} caused failure")