repl_driver.py 3.2 KB

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