command_driver.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if res.text is not None:
  30. print(res.text, flush=True)
  31. for att in res.attachments:
  32. print(f"Attached: {att.name}", flush=True)
  33. async def run():
  34. bot = TerminalBot()
  35. await bot.on_startup()
  36. try:
  37. while True:
  38. await bot.on_message(input("> !"))
  39. except EOFError:
  40. pass
  41. finally:
  42. await bot.on_shutdown()
  43. asyncio.run(run())