repl_driver.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 UPDATE SET count=count + 1",
  29. (name,),
  30. )
  31. await db.commit()
  32. async with db.execute(
  33. "SELECT count FROM counter WHERE name = ?", (name,)
  34. ) as cursor:
  35. res = (await cursor.fetchone())[0]
  36. await context.respond(rollbot.Response.from_message(message, f"{name} = {res}"))
  37. @rollbot.as_command
  38. async def count2(args: rollbot.ArgList, connect: rollbot.Lazy(rollbot.Database)):
  39. name = args[0] if len(args) > 0 else "main"
  40. async with await connect() as db:
  41. await db.execute(
  42. "INSERT INTO counter VALUES (?, 1) \
  43. ON CONFLICT (name) DO UPDATE SET count=count + 2",
  44. (name,),
  45. )
  46. await db.commit()
  47. async with db.execute(
  48. "SELECT count FROM counter WHERE name = ?", (name,)
  49. ) as cursor:
  50. res = (await cursor.fetchone())[0]
  51. return f"{name} = {res}"
  52. @rollbot.on_startup
  53. async def make_table(context):
  54. async with context.database() as db:
  55. await db.execute(
  56. "CREATE TABLE IF NOT EXISTS counter ( \
  57. name TEXT NOT NULL PRIMARY KEY, \
  58. count INTEGER NOT NULL DEFAULT 0 \
  59. );"
  60. )
  61. await db.commit()
  62. @rollbot.on_shutdown
  63. async def shutdown(context):
  64. await context.respond(
  65. rollbot.Response(origin_id="REPL", channel_id=".", text="Shutting down!")
  66. )
  67. @rollbot.as_command
  68. def simple():
  69. return "Simple!"
  70. @rollbot.as_command
  71. def generator():
  72. yield "This is"
  73. yield "a generator!"
  74. @rollbot.as_command
  75. async def coroutine():
  76. await asyncio.sleep(1.0)
  77. return "Here's a coroutine!"
  78. @rollbot.as_command
  79. async def asyncgen():
  80. yield "This is"
  81. await asyncio.sleep(0.5)
  82. yield "an async"
  83. await asyncio.sleep(0.5)
  84. yield "generator!"
  85. config = rollbot.get_command_config().extend(
  86. rollbot.CommandConfiguration(
  87. commands={
  88. "goodbye": goodbye_command,
  89. "count": count_command,
  90. },
  91. call_and_response={
  92. "hello": "Hello!",
  93. },
  94. aliases={
  95. "hi": "hello",
  96. "bye": "goodbye",
  97. },
  98. bangs=("/",),
  99. )
  100. )
  101. bot = MyBot(config, "/tmp/my.db")
  102. async def run():
  103. await bot.on_startup()
  104. try:
  105. while True:
  106. await bot.on_message(input("> "))
  107. except EOFError:
  108. pass
  109. await bot.on_shutdown()
  110. asyncio.run(run())