Get Started - Learn How To Make Your Bot!
List of all currently Free Items
Added on version 23.1.0b5
change_room_privilege(self, user_id: str, permissions: RoomPermissions ) -> None:
Change the room privilege for given user_id.
Here’s an example on how to promote an user using a “promote <@user> <role>” command on chat:
async def on_chat(self, user: User, message: str) -> None:
if message.startswith("promote"):
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 promote command format.")
return
command, username, role = parts
if "@" not in username:
username = username
else:
username = username[1:]
if role.lower() not in ["moderator", "designer"]:
await self.highrise.chat("Invalid role, please specify a valid role.")
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
#promote user
permissions = (await self.highrise.get_room_privilege(user_id))
setattr(permissions, role.lower(), True)
try:
await self.highrise.change_room_privilege(user_id, permissions)
await self.highrise.chat(f"{username} has been promoted to {role}.")
except Exception as e:
await self.highrise.chat(f"Error: {e}")
return
if message.startswith("demote"):
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 demote command format.")
return
command, username, role = parts
if "@" not in username:
username = username
else:
username = username[1:]
if role.lower() not in ["moderator", "designer"]:
await self.highrise.chat("Invalid role, please specify a valid role.")
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
#promote user
permissions = (await self.highrise.get_room_privilege(user_id))
setattr(permissions, role.lower(), False)
try:
await self.highrise.change_room_privilege(user_id, permissions)
await self.highrise.chat(f"{username} has been demoted from {role}.")
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("promote"):
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, role = parts
:
parts
list and assigns them to three variables: command
, username
, and role
.if "@" not in username:
:
username
provided is 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 role.lower() not in ["moderator", "designer"]:
role
provided is not one of the allowed roles: "moderator" or "designer".(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.get_room_privilege(user_id))
:
get_room_privilege(user_id)
on self.highrise
to fetch the privileges of the user with the specified user_id
.await
.setattr(permissions, role.lower(), True)
:
permissions
object based on the role.lower()
value.role
is "moderator", it sets permissions.moderator
to True
, or if role
is "designer", it sets permissions.designer
to True
.await self.highrise.change_room_privilege(user_id, permissions)
:
change_room_privilege(user_id, permissions)
on self.highrise
.user_id
based on the permissions
object.except Exception as e:
: