Get Started - Learn How To Make Your Bot!Code SnippetsGet methodsPost methodsWeb APIUseful linksList of all currently Free ItemsList of all currently Free EmotesHighrise Bot SDK Changelog
Added on version 23.1.0b5
moderate_room(self, user_id: str, action: Literal["kick", "ban", "unban", "mute"], action_length: int | None = None,) -> None:
Takes action against a user in the room.
βοΈ Use cases
Hereβs an example on how to kick an user using a βkick <@user>β command on chat:
async def on_chat(self, user: User, message: str) -> None: if message.startswith("kick"): if user.username == "ItsVini": pass else: await self.highrise.chat("You do not have permission to use this command.") return #separete message into parts parts = message.split() #check if message is valid "kick @username" if len(parts) != 2: await self.highrise.chat("Invalid kick command format.") return #checks if there's a @ in the message if "@" not in parts[1]: username = parts[1] else: username = parts[1][1:] #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 #kick user try: await self.highrise.moderate_room(user_id, "kick") except Exception as e: await self.highrise.chat(f"{e}") return #send message to chat await self.highrise.chat(f"{username} has been kicked from the room.")
Let's break down the code step-by-step to understand its functionality:
async def on_chat(self, user: User, message: str) -> None:
This is the definition of an asynchronous function namedon_chat
. It takes three arguments:self
: The reference to the instance of the class (assuming this function is inside a class).user
: An object representing the user who sent the message, probably containing user information like their username.message
: A string representing the content of the user's message in the chat.
- The function starts with an initial check to see if the message starts with the word "kick".
if user.username == "ItsVini":
This checks if the user who sent the message has the username "ItsVini." If so, the code proceeds without performing the kick action. This probably means that the user "ItsVini" is exempt from being kicked.
- If the user is not "ItsVini," the function sends a message to the chat saying that they don't have permission to use the "kick" command, and the function returns, stopping further execution.
- The code then proceeds to split the message into parts using whitespace as the delimiter.
if len(parts) != 2:
This checks if the split message has exactly two parts. If not, the code sends a message to the chat indicating an "Invalid kick command format," and the function returns.
- The code then checks if the second part of the message (presumably the username of the user to be kicked) contains an "@" symbol.
- If the "@" symbol is not present, the code assigns the second part as the
username
. Otherwise, it removes the "@" symbol and assigns the modified second part as theusername
.
- The code then fetches the list of users present in the chat room.
- It iterates through each user in the list to find the user with the specified
username
. It does a case-insensitive comparison by converting both the room_user's username and the givenusername
to lowercase.
- If it finds a matching user, it stores their
user_id
, indicating that the user has been found.
- If the
user_id
variable is not present, it means the specified user was not found in the chat room. The code sends a message to the chat saying "User not found, please specify a valid user and coordinate," and the function returns.
- If the specified user is found, the code attempts to kick the user from the room using the
await self.highrise.moderate_room(user_id, "kick")
statement. This function likely sends a moderation command to remove the specified user from the chat room.
- If the kick action encounters an exception (error), the code catches it, sends an error message to the chat, and the function returns.
- If the kick action is successful, the code sends a message to the chat indicating that the specified
username
has been kicked from the room.
Overall, this code is designed to allow users with the username "ItsVini" to use the "kick" command to remove other users from the chat room. Other users attempting to use the "kick" command will receive a message indicating they don't have permission. The function requires the "kick" command to be in the format "kick @username," where
username
is the username of the user to be kicked. If the specified user is found in the chat room, they will be kicked, and a confirmation message will be sent to the chat.Β