command_driver.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. if key == "riddle.sleep":
  14. return 10
  15. return key
  16. def parse(self, raw):
  17. return rollbot.Message(
  18. origin_id="REPL",
  19. channel_id=".",
  20. sender_id=".",
  21. timestamp=datetime.now(),
  22. origin_admin=True,
  23. channel_admin=True,
  24. sender_name="User",
  25. text="!" + raw,
  26. attachments=[],
  27. )
  28. async def respond(self, res):
  29. print(res.text, flush=True)
  30. for att in res.attachments:
  31. print(f"Attached: {att.name}", flush=True)
  32. async def run():
  33. bot = TerminalBot()
  34. await bot.on_startup()
  35. try:
  36. while True:
  37. await bot.on_message(input("> !"))
  38. except EOFError:
  39. pass
  40. finally:
  41. await bot.on_shutdown()
  42. asyncio.run(run())