Browse Source

Add commands library and a simple driver for testing

Kirk Trombley 4 năm trước cách đây
mục cha
commit
ecf96b8bc5
4 tập tin đã thay đổi với 87 bổ sung0 xóa
  1. 45 0
      drivers/command_driver.py
  2. 9 0
      src/commands/__init__.py
  3. 17 0
      src/commands/roll.py
  4. 16 0
      src/setup.py

+ 45 - 0
drivers/command_driver.py

@@ -0,0 +1,45 @@
+import asyncio
+from datetime import datetime
+
+import rollbot
+from commands import config
+
+
+class TerminalBot(rollbot.Rollbot[str]):
+    def __init__(self):
+        super().__init__(config.extend(rollbot.CommandConfiguration(bangs=("!",))), "/tmp/terminalbot.db")
+
+    def read_config(self, key):
+        return key
+
+    def parse(self, raw):
+        return rollbot.Message(
+            origin_id="REPL",
+            channel_id=".",
+            sender_id=".",
+            timestamp=datetime.now(),
+            origin_admin=True,
+            channel_admin=True,
+            text="!" + raw,
+            attachments=[],
+        )
+
+    async def respond(self, res):
+        print(res.text, flush=True)
+        for att in res.attachments:
+            print(f"Attached: {att.name}", flush=True)
+
+
+async def run():
+    bot = TerminalBot()
+    await bot.on_startup()
+    try:
+        while True:
+            await bot.on_message(input("> !"))
+    except EOFError:
+        pass
+    finally:
+        await bot.on_shutdown()
+
+
+asyncio.run(run())

+ 9 - 0
src/commands/__init__.py

@@ -0,0 +1,9 @@
+import commands.roll
+
+from rollbot import get_command_config
+
+__all__ = [
+    "config"
+]
+
+config = get_command_config()

+ 17 - 0
src/commands/roll.py

@@ -0,0 +1,17 @@
+import dice
+
+from rollbot import as_command
+from rollbot.injection import Args, ArgListSplitOn
+
+@as_command
+def roll(argstr: Args, args: ArgListSplitOn(",")):
+    try:
+        rolls = []
+        for roll_exp in args:
+            roll = dice.roll(roll_exp)
+            if not isinstance(roll, int):
+                roll = int(roll)
+            rolls.append(str(roll))
+        return f"Rolled {argstr}, got: {', '.join(rolls)}"
+    except Exception as _:
+        return "Sorry - dice expression appears malformed. Add more ()'s"

+ 16 - 0
src/setup.py

@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+
+from distutils.core import setup
+
+setup(
+    name="commands",
+    version="1.0.0",
+    description="Rollbot commands library",
+    author="Kirk Trombley",
+    author_email="ktrom3894@gmail.com",
+    packages=["commands"],
+    install_requires=[
+        "rollbot",
+        "dice",
+    ],
+)