command_driver.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import asyncio
  2. from datetime import datetime
  3. import rollbot
  4. from commands import config
  5. class TerminalBot(rollbot.Rollbot[str]):
  6. def __init__(self):
  7. super().__init__(
  8. config.extend(rollbot.CommandConfiguration(bangs=("!",))), "/tmp/terminalbot.db"
  9. )
  10. def read_config(self, key):
  11. if key == "democracy.candidates":
  12. return ["cand1", "cand2"]
  13. return key
  14. def parse(self, raw):
  15. return rollbot.Message(
  16. origin_id="REPL",
  17. channel_id=".",
  18. sender_id=".",
  19. timestamp=datetime.now(),
  20. origin_admin=True,
  21. channel_admin=True,
  22. sender_name="User",
  23. text="!" + raw,
  24. attachments=[],
  25. )
  26. async def respond(self, res):
  27. print(res.text, flush=True)
  28. for att in res.attachments:
  29. print(f"Attached: {att.name}", flush=True)
  30. async def run():
  31. bot = TerminalBot()
  32. await bot.on_startup()
  33. try:
  34. while True:
  35. await bot.on_message(input("> !"))
  36. except EOFError:
  37. pass
  38. finally:
  39. await bot.on_shutdown()
  40. asyncio.run(run())