repl_driver.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from datetime import datetime
  2. import asyncio
  3. import rollbot
  4. class MyBot(rollbot.Rollbot[str]):
  5. def read_config(self, key):
  6. return key
  7. def parse(self, raw):
  8. return rollbot.Message(
  9. origin_id="REPL",
  10. channel_id=".",
  11. sender_id=".",
  12. timestamp=datetime.now(),
  13. origin_admin=True,
  14. channel_admin=True,
  15. text=raw,
  16. attachments=[],
  17. )
  18. async def respond(self, res):
  19. print(res, flush=True)
  20. async def goodbye_command(message, context):
  21. await context.respond(rollbot.Response.from_message(message, "Goodbye!"))
  22. async def count_command(message, context):
  23. args = message.text.split("count", maxsplit=1)[1].strip().split()
  24. name = args[0] if len(args) > 0 else "main"
  25. async with context.database() as db:
  26. await db.execute(
  27. "INSERT INTO counter VALUES (?, 1) \
  28. ON CONFLICT (name) DO \
  29. UPDATE SET count=count + 1",
  30. (name,),
  31. )
  32. await db.commit()
  33. async with db.execute(
  34. "SELECT count FROM counter WHERE name = ?", (name,)
  35. ) as cursor:
  36. res = (await cursor.fetchone())[0]
  37. await context.respond(rollbot.Response.from_message(message, f"{name} = {res}"))
  38. @rollbot.on_startup
  39. async def make_table(context):
  40. async with context.database() as db:
  41. await db.execute(
  42. "CREATE TABLE IF NOT EXISTS counter ( \
  43. name TEXT NOT NULL PRIMARY KEY, \
  44. count INTEGER NOT NULL DEFAULT 0 \
  45. );"
  46. )
  47. await db.commit()
  48. @rollbot.on_shutdown
  49. async def shutdown(context):
  50. await context.respond(
  51. rollbot.Response(origin_id="REPL", channel_id=".", text="Shutting down!")
  52. )
  53. @rollbot.as_command
  54. def simple():
  55. return "Simple!"
  56. @rollbot.as_command
  57. def generator():
  58. yield "This is"
  59. yield "a generator!"
  60. @rollbot.as_command
  61. async def coroutine():
  62. await asyncio.sleep(1.0)
  63. return "Here's a coroutine!"
  64. @rollbot.as_command
  65. async def asyncgen():
  66. yield "This is"
  67. await asyncio.sleep(0.5)
  68. yield "an async"
  69. await asyncio.sleep(0.5)
  70. yield "generator!"
  71. config = rollbot.get_command_config().extend(
  72. rollbot.CommandConfiguration(
  73. commands={
  74. "goodbye": goodbye_command,
  75. "count": count_command,
  76. },
  77. call_and_response={
  78. "hello": "Hello!",
  79. },
  80. aliases={
  81. "hi": "hello",
  82. "bye": "goodbye",
  83. },
  84. bangs=("/",),
  85. )
  86. )
  87. bot = MyBot(config, "/tmp/my.db")
  88. async def run():
  89. await bot.on_startup()
  90. try:
  91. while True:
  92. await bot.on_message(input("> "))
  93. except EOFError:
  94. pass
  95. await bot.on_shutdown()
  96. asyncio.run(run())