repl_driver.py 3.2 KB

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