Get Started - Learn How To Make Your Bot!
List of all currently Free Items
Added on version 23.1.0b8
move_user_to_room(self, user_id: str, room_id: str) -> None:
This enables your bot to move user to another room, bots operator has to be owner of the second room.
Here’s an example on how to move an user using a “move <@user> <room>” command on chat:
async def on_chat(self, user: User, message: str) -> None:
if message.startswith("move"):
room_dictionary = {"room_1": "<insert here your room id>",
"room_2": "<insert here your room id>",}
if user.username != "ItsVini":
await self.highrise.chat("You do not have permission to use this command.")
return
parts = message.split()
if len(parts) != 3:
await self.highrise.chat("Invalid move command format.")
return
command, username, room = parts
if "@" not in username:
username = username
else:
username = username[1:]
if room not in room_dictionary:
await self.highrise.chat("Invalid room, please specify a valid room.")
return
#check if user is in room
room_users = (await self.highrise.get_room_users()).content
for room_user, pos in room_users:
if room_user.username.lower() == username.lower():
user_id = room_user.id
break
if "user_id" not in locals():
await self.highrise.chat("User not found, please specify a valid user and coordinate")
return
#move user
try:
await self.highrise.move_user_to_room(user_id, room_dictionary[room])
except Exception as e:
await self.highrise.chat(f"Error: {e}")
return
Here’s an step by step explanation on how this code works:
async def on_chat(self, user: User, message: str) -> None:
:
on_chat
.self
, user
, and message
.self
is a reference to the instance of the class this method belongs to.user
is an object representing the user who sent the chat message.message
is a string representing the chat message sent by the user.if message.startswith("move"):
room_dictionary = {"room_1": "<insert here your room id>", "room_2": "<insert here your room id>",}
if user.username != "ItsVini":
self.highrise.chat()
informing the user that they don't have permission and then returns, ending the function.parts = message.split()
parts
.if len(parts) != 3:
command, username, room = parts
parts
list and assigns them to three variables: command
, username
, and room
.if "@" not in username:
username
provided is not prefixed with "@"; if it is not, it means the username doesn't have the "@" symbol and is stored as it is.else:
username = username[1:]
.if room not in room_dictionary:
room
provided is not a valid room name present in the room_dictionary
.(await self.highrise.get_room_users()).content
get_room_users()
on self.highrise
to fetch a list of users in the room where this chat event occurred.await
.content
appears to be the actual list of room users.for room_user, pos in room_users:
if room_user.username.lower() == username.lower():
username
(also converted to lowercase).user_id
is stored in a variable.if "user_id" not in locals():
user_id
variable defined, it means the user with the specified username
was not found in the room.await self.highrise.move_user_to_room(user_id, room_dictionary[room])
:
move_user_to_room(user_id, room_id)
on self.highrise
.user_id
to the room associated with the provided room
name in the room_dictionary
.except Exception as e:
: