roomsettings.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. async def command_roomsettings(room: MatrixRoom, event: RoomMessageText, bot):
  4. setting = event.body.split()[2] if len(event.body.split()) > 2 else None
  5. value = " ".join(event.body.split()[3:]) if len(
  6. event.body.split()) > 3 else None
  7. if setting == "classification":
  8. setting = "use_classification"
  9. if setting == "systemmessage":
  10. setting = "system_message"
  11. if setting == "system_message":
  12. if value:
  13. bot.logger.log("Adding system message...")
  14. with bot.database.cursor() as cur:
  15. cur.execute(
  16. """INSERT INTO room_settings (room_id, setting, value) VALUES (?, ?, ?)
  17. ON CONFLICT (room_id, setting) DO UPDATE SET value = ?;""",
  18. (room.room_id, "system_message", value, value)
  19. )
  20. await bot.send_message(room, f"Alright, I've stored the system message: '{value}'.", True)
  21. return
  22. bot.logger.log("Retrieving system message...")
  23. system_message = bot.get_system_message(room)
  24. await bot.send_message(room, f"The current system message is: '{system_message}'.", True)
  25. return
  26. if setting in ("use_classification", "always_reply"):
  27. if value:
  28. if value.lower() in ["true", "false"]:
  29. value = value.lower() == "true"
  30. bot.logger.log(f"Setting {setting} status for {room.room_id} to {value}...")
  31. with bot.database.cursor() as cur:
  32. cur.execute(
  33. """INSERT INTO room_settings (room_id, setting, value) VALUES (?, ?, ?)
  34. ON CONFLICT (room_id, setting) DO UPDATE SET value = ?;""",
  35. (room.room_id, setting, "1" if value else "0", "1" if value else "0")
  36. )
  37. await bot.send_message(room, f"Alright, I've set {setting} to: '{value}'.", True)
  38. return
  39. await bot.send_message(room, "You need to provide a boolean value (true/false).", True)
  40. return
  41. bot.logger.log(f"Retrieving {setting} status for {room.room_id}...")
  42. with bot.database.cursor() as cur:
  43. cur.execute(
  44. """SELECT value FROM room_settings WHERE room_id = ? AND setting = ?;""",
  45. (room.room_id, setting)
  46. )
  47. value = cur.fetchone()[0]
  48. if not value:
  49. if setting == "use_classification":
  50. value = False
  51. elif setting == "always_reply":
  52. value = True
  53. else:
  54. value = bool(int(value))
  55. await bot.send_message(room, f"The current {setting} status is: '{value}'.", True)
  56. return
  57. message = f"""The following settings are available:
  58. - system_message [message]: Get or set the system message to be sent to the chat model
  59. - classification [true/false]: Get or set whether the room uses classification
  60. - always_reply [true/false]: Get or set whether the bot should reply to all messages (if false, only reply to mentions and commands)
  61. """
  62. await bot.send_message(room, message, True)