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
Concept
Want a easier way to change your botβs outfit? You found it!
Today we are gonna make a function to change the botβs outfit using on_chat commands. To do that we will use the π¬ Command Handler, note that this use is optional, you can implement this function directly on your main code or directly on your on_chat method.
The command format used is
/equip <Item Name> <index (optional)>
.MAKE SURE TO READ HOW CHANGING THE OUTFIT WORKS ABOVE:
Methods/Functions used:
Required Libraries
- highrise-bot-sdk
Code
First of all, letβs create a new file called equip.py inside of our functions folder:
Hereβs what we will need to import into our equip.py file:
from highrise import * from highrise.models import * from highrise.webapi import * from highrise.models_webapi import *
Letβs proceed to our function definition, since weβre using the command handler for this example, your function name will be the command that you will use in the chat to call it, I will name mine as equip:
from highrise import * from highrise.webapi import * from highrise.models_webapi import * from highrise.models import * async def equip(self: BaseBot, user: User, message: str):
Finally, letβs implement the function and after it you will see a detailed step by step guide on how the code works:
from highrise import * from highrise.webapi import * from highrise.models_webapi import * from highrise.models import * async def equip(self: BaseBot, user: User, message: str): #gets the item id and category from the message parts = message.split(" ") if len(parts) < 2: await self.highrise.chat("You need to specify the item name.") return item_name = "" for part in parts[1:]: item_name += part + " " item_name = item_name[:-1] #check if the last part of the message is a number index = 0 if item_name[-1].isdigit(): item_name = item_name[:-2] index = int(parts[-1]) item = (await self.webapi.get_items(item_name = item_name)).items #checks if the response is valid if item == []: await self.highrise.chat(f"Item '{item_name}' not found.") return elif len(item) > 1: await self.highrise.chat(f"Multiple items found for '{item_name}', using the item number {index} in the list {item[index].item_name}.") item = item[index] item_id = item.item_id category = item.category #--------------------------------------------------------# verification = False #checks if the bot has the item inventory = (await self.highrise.get_inventory()).items for inventory_item in inventory: if inventory_item.id == item_id: verification = True break if verification == False: #checks if the item is free if item.rarity == Rarity.NONE: pass #checks if the item is purchasable elif item.is_purchasable == False: await self.highrise.chat(f"Item '{item_name}' can't be purchased.") return else: try: response = await self.highrise.buy_item(item_id) if response != "success": await self.highrise.chat(f"Item '{item_name}' can't be purchased.") return else: await self.highrise.chat(f"Item '{item_name}' purchased.") except Exception as e: print(e) await self.highrise.chat(f"Exception: {e}'.") return #--------------------------------------------------------# new_item = Item(type = "clothing", amount = 1, id = item_id, account_bound=False, active_palette=0,) #--------------------------------------------------------# #checks if the item category is already in use outfit = (await self.highrise.get_my_outfit()).outfit items_to_remove = [] for outfit_item in outfit: #the category of the item in an outfit can be found by the first string in the id before the "-" character item_category = outfit_item.id.split("-")[0][0:4] print(f"{item_category}") if item_category == category[0:4]: items_to_remove.append(outfit_item) for item_to_remove in items_to_remove: outfit.remove(item_to_remove) #if the item is a hair, also equips the hair_back if category == "hair_front": hair_back_id = item.link_ids[0] hair_back = Item(type = "clothing", amount = 1, id = hair_back_id, account_bound=False, active_palette=0,) outfit.append(hair_back) outfit.append(new_item) await self.highrise.set_outfit(outfit)
Now letβs understand how the code works:
from highrise import *
,from highrise.webapi import *
,from highrise.models_webapi import *
, andfrom highrise.models import *
:- These lines import the necessary modules and classes required for interacting with the game's API, both for the main bot logic, web API calls, and various models.
async def equip(self: BaseBot, user: User, message: str):
- This is an asynchronous function named
equip
. - It takes four parameters:
self
(a reference to the instance of the class this method belongs to),user
(an object representing the user who sent the chat message), andmessage
(a string representing the chat message sent by the user).
parts = message.split(" ")
:- The function splits the
message
into individual words using space as the separator and stores them in the listparts
.
if len(parts) < 2:
:- If there are fewer than two words in the
message
, it means the user didn't provide enough information. - The function sends a message to the chat using
self.highrise.chat()
to inform the user that they need to specify the item name and then returns, ending the function.
item_name = ""
andfor part in parts[1:]: item_name += part + " "
:- These lines concatenate the words from
parts[1:]
to form theitem_name
. - The loop runs through each word in
parts
starting from index 1.
item_name = item_name[:-1]
:- This removes the trailing space from the
item_name
since it was added at the end of the previous loop.
index = 0
andif item_name[-1].isdigit(): item_name = item_name[:-2] index = int(parts[-1])
:- The code checks if the last character of the
item_name
is a digit, indicating that an item index has been provided. - If an index is present, it removes the last two characters from
item_name
(to remove the space and the index) and assigns the integer value of the last part ofparts
to theindex
variable.
item = (await self.webapi.get_items(item_name = item_name)).items
:- The code calls an asynchronous method
get_items(item_name=item_name)
onself.webapi
to fetch item data based on the specifieditem_name
. - The result is awaited using
await
. - The method seems to return some kind of response object, and
item
appears to contain the item data.
- The code checks if the response is valid and whether an item was found. If no item is found, or if multiple items are found, it sends appropriate messages to the chat and returns, ending the function.
item = item[index]
:- If an index is provided, the code retrieves the item with the specified index from the list of items.
item_id
,category = ...
:- The code extracts the necessary information from the
item
object, such asitem_id
andcategory
.
- The code then checks if the bot already has the item. If not, it checks if the item is purchasable or free, and if it's purchasable, it attempts to buy it. If the purchase is unsuccessful, it sends an appropriate message to the chat and returns.
new_item = ...
:- The code creates a new
Item
object representing the newly equipped item.
- The code then fetches the bot's outfit, identifies items of the same category, and removes them from the outfit.
- If the category is "hair_front," the code also equips the corresponding "hair_back" item.
- Finally, the new item and, potentially, the hair_back item are appended to the outfit, and the updated outfit is set using
self.highrise.set_outfit(outfit)
.
Β
Β