Get Started - Learn How To Make Your Bot!
List of all currently Free Items
Want a easier way to change your bot’s outfit color? You found it!
The code that we’re going to use today is very similar to the code at ‣.
Today we are gonna make a function to change thebot’s outfit color 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 works by saying the command (color), the item category, and the color number in the Highrise palette:
<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 color.py inside of our functions folder:
File structure in the workspace.
Here’s what we will need to import into our color.py file:
from highrise import *
from highrise.models 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 color:
from highrise import *
from highrise.models import *
async def color(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.models import *
async def color(self: BaseBot, user: User, message: str):
parts = message.split(" ")
print (parts)
if len(parts) != 3:
await self.highrise.chat("Invalid command format. You need to specify the category and color palette number.")
return
category = parts[1]
try:
color_palette = int(parts[2])
except:
await self.highrise.chat("Invalid command format. You need to specify the category and color palette number.")
return
outfit = (await self.highrise.get_my_outfit()).outfit
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]
if item_category == category:
try:
outfit_item.active_palette = color_palette
except:
await self.highrise.chat(f"The bot isn't using any item from the category '{category}'.")
return
await self.highrise.set_outfit(outfit)
Now let’s understand how the code works:
from highrise import *
and from highrise.models import *
:
async def color(self: BaseBot, user: User, message: str):
color
.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
string into individual words using space as the separator and stores them in the list parts
.print(parts)
:
message
to the console. It can be useful for debugging purposes to check what the user's input looks like.if len(parts) != 3:
:
message
, it means the user has not specified the category and color palette correctly.self.highrise.chat()
to inform the user about the invalid command format and then returns, ending the function.category = parts[1]
and color_palette = int(parts[2])
:
parts
list and store them in category
and color_palette
variables, respectively.(await self.highrise.get_my_outfit()).outfit
:
get_my_outfit()
on self.highrise
to fetch the bot's outfit items.await
.outfit
appears to be the actual list of items in the bot's outfit.category
.item_category = outfit_item.id.split("-")[0]
:
outfit_item
by splitting the item's ID using the "-" character and taking the first part (before the "-").if item_category == category:
:
outfit_item
matches the specified category
, the code tries to set the active_palette
property of that item to the provided color_palette
.try: outfit_item.active_palette = color_palette
:
active_palette
property of the outfit_item
to the specified color_palette
.except: await self.highrise.chat(f"The bot isn't using any item from the category '{category}'.")
:
outfit_item
is not found for the specified category
, an exception is raised (likely because outfit_item
is None
or it doesn't have an active_palette
property).self.highrise.chat()
to inform the user that the bot isn't using any item from the specified category.await self.highrise.set_outfit(outfit)
:
active_palette
property for the outfit item(s) of the specified category
, the code calls an asynchronous method set_outfit(outfit)
on self.highrise
.outfit
list.