Get Started - Learn How To Make Your Bot!

Guides

Code Snippets

Get methods

Post methods

Web API

Useful links

List of all currently Free Items

List of Emotes

Highrise 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:

  1. async def on_chat(self, user: User, message: str) -> None: This is the definition of an asynchronous function named on_chat. It takes three arguments:
  2. The function starts with an initial check to see if the message starts with the word "kick".
  3. 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.
  4. 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.
  5. The code then proceeds to split the message into parts using whitespace as the delimiter.
  6. 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.
  7. The code then checks if the second part of the message (presumably the username of the user to be kicked) contains an "@" symbol.
  8. 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 the username.
  9. The code then fetches the list of users present in the chat room.
  10. 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 given username to lowercase.
  11. If it finds a matching user, it stores their user_id, indicating that the user has been found.
  12. 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.
  13. 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.
  14. If the kick action encounters an exception (error), the code catches it, sends an error message to the chat, and the function returns.
  15. 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.