Get Started - Learn How To Make Your Bot!
List of all currently Free Items
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)>
.
<aside> ⚠️ MAKE SURE TO READ HOW CHANGING THE OUTFIT WORKS ABOVE:
</aside>
Highrise Official Documentation for Creators.
Highrise Official Documentation for Creators.
First of all, let’s create a new file called equip.py inside of our functions folder:
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:
from highrise import *
, from highrise.webapi import *
, from highrise.models_webapi import *
, and from highrise.models import *
:
async def equip(self: BaseBot, user: User, message: str):
equip
.self
(a reference to the instance of the class this method belongs to), user
(an object representing the user who sent the chat message), and message
(a string representing the chat message sent by the user).parts = message.split(" ")
:
message
into individual words using space as the separator and stores them in the list parts
.if len(parts) < 2:
:
message
, it means the user didn't provide enough information.self.highrise.chat()
to inform the user that they need to specify the item name and then returns, ending the function.item_name = ""
and for part in parts[1:]: item_name += part + " "
:
parts[1:]
to form the item_name
.parts
starting from index 1.item_name = item_name[:-1]
:
item_name
since it was added at the end of the previous loop.index = 0
and if item_name[-1].isdigit(): item_name = item_name[:-2] index = int(parts[-1])
:
item_name
is a digit, indicating that an item index has been provided.item_name
(to remove the space and the index) and assigns the integer value of the last part of parts
to the index
variable.item = (await self.webapi.get_items(item_name = item_name)).items
:
get_items(item_name=item_name)
on self.webapi
to fetch item data based on the specified item_name
.await
.item
appears to contain the item data.item = item[index]
:
item_id
, category = ...
:
item
object, such as item_id
and category
.new_item = ...
:
Item
object representing the newly equipped item.self.highrise.set_outfit(outfit)
.