Browse Source

Big refactor to make it easier to make simple plugins entirely in configuration

Kirk Trombley 6 years ago
parent
commit
aff7f50237
9 changed files with 100 additions and 108 deletions
  1. 63 4
      config/config.toml
  2. 0 1
      src/app.py
  3. 1 3
      src/plugins/__init__.py
  4. 35 0
      src/plugins/dynamic.py
  5. 0 16
      src/plugins/meme.py
  6. 0 10
      src/plugins/simple.py
  7. 0 19
      src/plugins/the_guy.py
  8. 0 39
      src/plugins/the_house.py
  9. 1 16
      src/rollbot.py

+ 63 - 4
config/config.toml

@@ -1,5 +1,5 @@
 database = "/tmp/rollbot.sqlite"
-sleep_time = 1.5
+sleep_time = 0.25
 
 [aliases]
 speemteek = "speamteek"
@@ -9,12 +9,11 @@ horoscope = "inspire"
 lomg = "bump"
 mute = "bump"
 
-[responses]
+[dynamic.simple_responses]
 info = """
 Hello!
 I am Rollbot 3.0 - a simple chatbot built in Python.
-I am licensed as open source under the MIT license.
-"""
+I am licensed as open source under the MIT license."""
 thanks = "You're welcome!"
 guess = "Rollbot's back - tell a friend!"
 unmeme = "get a job"
@@ -24,6 +23,66 @@ vore = "What? No"
 break = "I'm too robust for that!"
 yiff = "Sorry - I don't think I understand the command '!yiff'... I'll talk to Ivan and get back to you!"
 f = "FFFFFFFFFFFFFFFFFFFFF\nFFFFFFFFFFFFFFFFFFFFF\nFFFFFFFFFFFFFFFFFFFFF\nFFFFFFF\nFFFFFFF\nFFFFFFF\nFFFFFFFFFFFFFF\nFFFFFFFFFFFFFF\nFFFFFFFFFFFFFF\nFFFFFFF\nFFFFFFF\nFFFFFFF\nFFFFFFF\nFFFFFFF"
+hey = """
+🖕     😎
+🐛💤👔🐛
+        ⛽  👢
+       ⚡8=👊=D💦
+   🎸     🌽
+👢           👢
+"""
+
+[dynamic.echo_responses]
+echo = "'{}'"
+appreciate = """
+┏┓
+┃┃╱╲ In this
+┃╱╱╲╲ house
+╱╱╭╮╲╲ we love
+▔▏┗┛▕▔ & appreciate
+╱▔▔▔▔▔▔▔▔▔▔╲
+{}
+╱╱┏┳┓╭╮┏┳┓ ╲╲
+▔▏┗┻┛┃┃┗┻┛▕▔"""
+stfu = """
+┏┓
+┃┃╱╲ Shut
+┃╱╱╲╲ The
+╱╱╭╮╲╲ Fuck
+▔▏┗┛▕▔ Up
+╱▔▔▔▔▔▔▔▔▔▔╲
+{}
+╱╱┏┳┓╭╮┏┳┓ ╲╲
+▔▏┗┻┛┃┃┗┻┛▕▔"""
+
+[dynamic.choice_responses]
+
+    [[dynamic.choice_responses.greet]]
+    value = "Hi!"
+
+    [[dynamic.choice_responses.greet]]
+    value = "Hello!"
+
+    [[dynamic.choice_responses.greet]]
+    value = "안녕하세요"
+
+    [[dynamic.choice_responses.greet]]
+    value = "こんにちは"
+
+    [[dynamic.choice_responses.greet]]
+    value = "你好"
+
+    [[dynamic.choice_responses.greet]]
+    value = "👋"
+
+    [[dynamic.choice_responses.meme]]
+    value = "fuck off"
+    weight = 19
+
+    [[dynamic.choice_responses.meme]]
+    value = "I love you"
+
+
 
 [teamspeak]
 host = "kirkleon.ddns.net"

+ 0 - 1
src/app.py

@@ -36,7 +36,6 @@ rollbot = Rollbot(
     logger=app.logger,
     plugin_classes=find_all_subclasses(RollbotPlugin),
     aliases=get_config("aliases"),
-    responses=get_config("responses"),
     lookup=BOTS_LOOKUP,
     sleep_time=float(get_config("sleep_time")),
     callback=post_message,

+ 1 - 3
src/plugins/__init__.py

@@ -1,10 +1,8 @@
 import plugins.roll
 import plugins.querying
 import plugins.seychelles
-import plugins.the_guy
 import plugins.session
 import plugins.simple
 import plugins.curse
 import plugins.teamspeak
-import plugins.the_house
-import plugins.meme
+import plugins.dynamic

+ 35 - 0
src/plugins/dynamic.py

@@ -0,0 +1,35 @@
+import random
+
+from config import get_config
+from command_system import as_plugin, RollbotResponse
+
+
+def lift_simple_response(call, response):
+    @as_plugin(call)
+    def response_func(db, msg):
+        return RollbotResponse(msg, txt=response)
+
+
+def lift_echo_response(call, response):
+    @as_plugin(call)
+    def response_func(db, msg):
+        return RollbotResponse(msg, txt=response.format(msg.raw_args or ""))
+
+
+def lift_choice_response(call, options):
+    @as_plugin(call)
+    def response_func(db, msg):
+        return RollbotResponse(msg, txt=random.choice(options))
+
+
+dynamic = get_config("dynamic")
+
+for cmd, response in dynamic["simple_responses"].items():
+    lift_simple_response(cmd, response)
+
+for cmd, response in dynamic["echo_responses"].items():
+    lift_echo_response(cmd, response)
+
+for cmd, options in dynamic["choice_responses"].items():
+    opts = [y for x in ([o["value"]] * o.get("weight", 1) for o in options) for y in x]
+    lift_choice_response(cmd, opts)

+ 0 - 16
src/plugins/meme.py

@@ -1,16 +0,0 @@
-import random
-
-from command_system import as_plugin, RollbotResponse
-
-@as_plugin
-def meme(db, message):
-
-        diceroll = random.randint(1,20)
-        good = "I love you."
-        bad = "fuck off"
-
-        if diceroll == 20:
-            return RollbotResponse(message, txt=good)
-
-        else:
-            return RollbotResponse(message, txt=bad)

+ 0 - 10
src/plugins/simple.py

@@ -13,16 +13,6 @@ def debug(db, message):
     return RollbotResponse(message, txt=str(message))
 
 
-@as_plugin
-def echo(db, message):
-    return RollbotResponse(message, txt="'%s' - %s" % (message.raw_args, message.name))
-
-
-@as_plugin
-def greet(db, message):
-    return RollbotResponse(message, txt=random.choice(("Hi!", "Hello!", "안녕하세요", "こんにちは", "你好", "👋")))
-
-
 @as_plugin
 def console(db, message):
     argstr = message.raw_args

+ 0 - 19
src/plugins/the_guy.py

@@ -1,19 +0,0 @@
-from command_system import RollbotResponse, RollbotFailure, as_plugin
-
-
-THE_GUY = """\
-🖕     😎
-🐛💤👔🐛
-        ⛽  👢
-       ⚡8=👊=D💦
-   🎸     🌽
-👢           👢
-"""
-
-
-@as_plugin("hey")
-def the_guy(db, msg):
-    for x, y in zip(msg.args(), ("can", "i", "have", "the", "guy")):
-        if x.lower() != y:
-            return RollbotResponse(msg, failure=RollbotFailure.INVALID_ARGUMENTS)
-    return RollbotResponse(msg, txt=THE_GUY)

+ 0 - 39
src/plugins/the_house.py

@@ -1,39 +0,0 @@
-from command_system import RollbotResponse, RollbotFailure, as_plugin
-
-housetop = """\
-┏┓
-┃┃╱╲ In this
-┃╱╱╲╲ house
-╱╱╭╮╲╲ we love
-▔▏┗┛▕▔ & appreciate
-╱▔▔▔▔▔▔▔▔▔▔╲
-"""
-
-stfutop = """\
-┏┓
-┃┃╱╲ Shut
-┃╱╱╲╲ The
-╱╱╭╮╲╲ Fuck
-▔▏┗┛▕▔  Up
-╱▔▔▔▔▔▔▔▔▔▔╲
-"""
-
-housebot = """
-╱╱┏┳┓╭╮┏┳┓ ╲╲
-▔▏┗┻┛┃┃┗┻┛▕▔
-"""
-
-@as_plugin("appreciate")
-def the_house(db, msg):
-    
-    thehouse = housetop + ' '.join(list(msg.args(normalize=False))) + housebot
-
-    return RollbotResponse(msg, txt=thehouse)
-
-@as_plugin("stfu")
-def shut_up(db, msg):
-
-    shutup = stfutop + ' '.join(list(msg.args(normalize=False))) + housebot
-
-    return RollbotResponse(msg, txt=shutup)
-

+ 1 - 16
src/rollbot.py

@@ -4,15 +4,8 @@ import time
 from command_system import RollbotResponse, RollbotFailure, as_plugin
 
 
-def lift_response(call, response):
-    @as_plugin(call)
-    def response_func(db, msg):
-        return RollbotResponse(msg, txt=response)
-    return response_func
-
-
 class Rollbot:
-    def __init__(self, logger=logging.getLogger(__name__), plugin_classes={}, aliases={}, responses={}, lookup={}, sleep_time=0.0, session_factory=None, callback=None):
+    def __init__(self, logger=logging.getLogger(__name__), plugin_classes={}, aliases={}, lookup={}, sleep_time=0.0, session_factory=None, callback=None):
         self.logger = logger
         self.session_factory = session_factory or (lambda: None)
         self.callback = callback or (lambda txt, gid: self.logger.info(f"Responding to {gid} with {txt}"))
@@ -33,14 +26,6 @@ class Rollbot:
                 self.to_stop.add(plugin_instance)
         self.logger.info(f"Finished loading plugins, {len(self.commands)} commands found")
 
-        self.logger.info("Loading simple responses")
-        for cmd, response in responses.items():
-            if cmd in self.commands:
-                self.logger.error(f"Duplicate command word '{cmd}'")
-                raise ValueError(f"Duplicate command word '{cmd}'")
-            self.commands[cmd] = lift_response(cmd, response)(logger=logger)
-        self.logger.info(f"Finished loading simple responses, {len(self.commands)} total commands available")
-
         self.logger.info("Loading aliases")
         for alias, cmd in aliases.items():
             if cmd not in self.commands: