space.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from nio.events.room_events import RoomMessageText
  2. from nio.rooms import MatrixRoom
  3. from nio.responses import RoomInviteError
  4. async def command_space(room: MatrixRoom, event: RoomMessageText, bot):
  5. if len(event.body.split()) == 3:
  6. request = event.body.split()[2]
  7. if request.lower() == "enable":
  8. bot.logger.log("Enabling space...")
  9. with bot.database.cursor() as cursor:
  10. cursor.execute(
  11. "SELECT space_id FROM user_spaces WHERE user_id = ? AND active = TRUE", (event.sender,))
  12. space = cursor.fetchone()
  13. if not space:
  14. space = await bot.create_space("GPTBot")
  15. bot.logger.log(
  16. f"Created space {space} for user {event.sender}")
  17. if bot.logo_uri:
  18. await bot.matrix_client.room_put_state(space, "m.room.avatar", {
  19. "url": bot.logo_uri
  20. }, "")
  21. with bot.database.cursor() as cursor:
  22. cursor.execute(
  23. "INSERT INTO user_spaces (space_id, user_id) VALUES (?, ?)", (space, event.sender))
  24. else:
  25. space = space[0]
  26. response = await bot.matrix_client.room_invite(space, event.sender)
  27. if isinstance(response, RoomInviteError):
  28. bot.logger.log(
  29. f"Failed to invite user {event.sender} to space {space}", "error")
  30. await bot.send_message(
  31. room, "Sorry, I couldn't invite you to the space. Please try again later.", True)
  32. return
  33. bot.database.commit()
  34. await bot.send_message(room, "Space enabled.", True)
  35. request = "update"
  36. elif request.lower() == "disable":
  37. bot.logger.log("Disabling space...")
  38. with bot.database.cursor() as cursor:
  39. cursor.execute(
  40. "SELECT space_id FROM user_spaces WHERE user_id = ? AND active = TRUE", (event.sender,))
  41. space = cursor.fetchone()[0]
  42. if not space:
  43. bot.logger.log(f"User {event.sender} does not have a space")
  44. await bot.send_message(room, "You don't have a space enabled.", True)
  45. return
  46. with bot.database.cursor() as cursor:
  47. cursor.execute(
  48. "UPDATE user_spaces SET active = FALSE WHERE user_id = ?", (event.sender,))
  49. bot.database.commit()
  50. await bot.send_message(room, "Space disabled.", True)
  51. return
  52. if request.lower() == "update":
  53. bot.logger.log("Updating space...")
  54. with bot.database.cursor() as cursor:
  55. cursor.execute(
  56. "SELECT space_id FROM user_spaces WHERE user_id = ? AND active = TRUE", (event.sender,))
  57. space = cursor.fetchone()[0]
  58. if not space:
  59. bot.logger.log(f"User {event.sender} does not have a space")
  60. await bot.send_message(
  61. room, "You don't have a space enabled. Create one first using `!gptbot space enable`.", True)
  62. return
  63. rooms = bot.matrix_client.rooms
  64. join_rooms = []
  65. for room in rooms.values():
  66. if event.sender in room.users.keys():
  67. bot.logger.log(
  68. f"Adding room {room.room_id} to space {space}")
  69. join_rooms.append(room.room_id)
  70. await bot.add_rooms_to_space(space, join_rooms)
  71. if bot.logo_uri:
  72. await bot.matrix_client.room_put_state(space, "m.room.avatar", {
  73. "url": bot.logo_uri
  74. }, "")
  75. await bot.send_message(room, "Space updated.", True)
  76. return
  77. if request.lower() == "invite":
  78. bot.logger.log("Inviting user to space...")
  79. with bot.database.cursor() as cursor:
  80. cursor.execute(
  81. "SELECT space_id FROM user_spaces WHERE user_id = ?", (event.sender,))
  82. space = cursor.fetchone()[0]
  83. if not space:
  84. bot.logger.log(f"User {event.sender} does not have a space")
  85. await bot.send_message(
  86. room, "You don't have a space enabled. Create one first using `!gptbot space enable`.", True)
  87. return
  88. response = await bot.matrix_client.room_invite(space, event.sender)
  89. if isinstance(response, RoomInviteError):
  90. bot.logger.log(
  91. f"Failed to invite user {user} to space {space}", "error")
  92. await bot.send_message(
  93. room, "Sorry, I couldn't invite you to the space. Please try again later.", True)
  94. return
  95. await bot.send_message(room, "Invited you to the space.", True)
  96. return
  97. with bot.database.cursor() as cursor:
  98. cursor.execute(
  99. "SELECT active FROM user_spaces WHERE user_id = ?", (event.sender,))
  100. status = cursor.fetchone()
  101. if not status:
  102. await bot.send_message(
  103. room, "You don't have a space enabled. Create one using `!gptbot space enable`.", True)
  104. return
  105. if not status[0]:
  106. await bot.send_message(
  107. room, "Your space is disabled. Enable it using `!gptbot space enable`.", True)
  108. return
  109. await bot.send_message(
  110. room, "Your space is enabled. Rooms will be added to it automatically.", True)
  111. return