2023-04-19 06:11:28 +00:00
|
|
|
from nio.events.room_events import RoomMessageText
|
2023-04-25 11:25:53 +00:00
|
|
|
from nio import RoomCreateError, RoomInviteError
|
2023-04-19 06:11:28 +00:00
|
|
|
from nio.rooms import MatrixRoom
|
|
|
|
|
2023-04-23 13:26:46 +00:00
|
|
|
|
2023-04-25 11:25:53 +00:00
|
|
|
async def command_newroom(room: MatrixRoom, event: RoomMessageText, bot):
|
2023-04-23 13:26:46 +00:00
|
|
|
room_name = " ".join(event.body.split()[
|
2023-05-01 19:27:45 +00:00
|
|
|
2:]) or bot.default_room_name
|
2023-04-19 06:11:28 +00:00
|
|
|
|
2023-04-25 11:25:53 +00:00
|
|
|
bot.logger.log("Creating new room...")
|
|
|
|
new_room = await bot.matrix_client.room_create(name=room_name)
|
|
|
|
|
|
|
|
if isinstance(new_room, RoomCreateError):
|
|
|
|
bot.logger.log(f"Failed to create room: {new_room.message}")
|
|
|
|
await bot.send_message(room, f"Sorry, I was unable to create a new room. Please try again later, or create a room manually.", True)
|
|
|
|
return
|
|
|
|
|
|
|
|
bot.logger.log(f"Inviting {event.sender} to new room...")
|
|
|
|
invite = await bot.matrix_client.room_invite(new_room.room_id, event.sender)
|
|
|
|
|
|
|
|
if isinstance(invite, RoomInviteError):
|
|
|
|
bot.logger.log(f"Failed to invite user: {invite.message}")
|
|
|
|
await bot.send_message(room, f"Sorry, I was unable to invite you to the new room. Please try again later, or create a room manually.", True)
|
|
|
|
return
|
2023-04-19 06:11:28 +00:00
|
|
|
|
2023-05-09 10:27:03 +00:00
|
|
|
with bot.database.cursor() as cursor:
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT space_id FROM user_spaces WHERE user_id = ? AND active = TRUE", (event.sender,))
|
|
|
|
space = cursor.fetchone()
|
|
|
|
|
|
|
|
if space:
|
|
|
|
bot.logger.log(f"Adding new room to space {space[0]}...")
|
|
|
|
await bot.add_rooms_to_space(space[0], [new_room.room_id])
|
|
|
|
|
2023-05-09 11:30:51 +00:00
|
|
|
if bot.logo_uri:
|
|
|
|
await bot.matrix_client.room_put_state(room, "m.room.avatar", {
|
|
|
|
"url": bot.logo_uri
|
|
|
|
}, "")
|
|
|
|
|
2023-04-28 10:01:27 +00:00
|
|
|
await bot.matrix_client.room_put_state(
|
2023-05-09 11:30:51 +00:00
|
|
|
new_room.room_id, "m.room.power_levels", {"users": {event.sender: 100, bot.matrix_client.user_id: 100}})
|
2023-04-19 06:11:28 +00:00
|
|
|
|
2023-04-25 11:25:53 +00:00
|
|
|
await bot.matrix_client.joined_rooms()
|
|
|
|
await bot.send_message(room, f"Alright, I've created a new room called '{room_name}' and invited you to it. You can find it at {new_room.room_id}", True)
|
2023-05-09 11:30:51 +00:00
|
|
|
await bot.send_message(bot.rooms[new_room.room_id], f"Welcome to the new room! What can I do for you?")
|