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


Concept

Want a easier way to change your bot’s outfit? You found it!

https://youtu.be/kN-JjtszxIs

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)>.

<aside> ⚠️ MAKE SURE TO READ HOW CHANGING THE OUTFIT WORKS ABOVE:

</aside>

Highrise Official Documentation for Creators.

Highrise Official Documentation for Creators.

Methods/Functions used:

Required Libraries

Code

First of all, let’s create a new file called equip.py inside of our functions folder:

File structure in the workspace.

File structure in the workspace.

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:

  1. from highrise import *, from highrise.webapi import *, from highrise.models_webapi import *, and from highrise.models import *:
  2. async def equip(self: BaseBot, user: User, message: str):
  3. parts = message.split(" "):
  4. if len(parts) < 2::
  5. item_name = "" and for part in parts[1:]: item_name += part + " ":
  6. item_name = item_name[:-1]:
  7. index = 0 and if item_name[-1].isdigit(): item_name = item_name[:-2] index = int(parts[-1]):
  8. item = (await self.webapi.get_items(item_name = item_name)).items:
  9. 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.
  10. item = item[index]:
  11. item_id, category = ...:
  12. 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.
  13. new_item = ...:
  14. The code then fetches the bot's outfit, identifies items of the same category, and removes them from the outfit.
  15. If the category is "hair_front," the code also equips the corresponding "hair_back" item.
  16. 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).