command_driver.py 1.5 KB

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