Get Started - Learn How To Make Your Bot!
List of all currently Free Items
This is a simple code snippet to show how to make a target user perform a given emote using only on_chat commands:
Today we are gonna make a function to make a target user perform a given emote 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 /emote <username> <emote_id>
.
First of all, let’s create a new file called emote.py inside of our functions folder:
Here’s what we will need to import into our emote.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 emote(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 *
from highrise.webapi import *
from highrise.models_webapi import *
async def emote(self: BaseBot, user: User, message: str) -> None:
try:
command, target, emote_id = message.split(" ")
except:
await self.highrise.chat("Invalid command format. Please use '/emote <target> <emote_id>.")
return
user = (await self.webapi.get_users(username=target))
if user.total == 0:
await self.highrise.chat("Invalid target.")
return
user_id = user.users[0].user_id
try:
await self.highrise.send_emote(emote_id, user_id)
except:
await self.highrise.chat("Invalid emote.")
return
Now let’s understand how the code works:
from highrise import *
, from highrise.models import *
, from highrise.webapi import *
, and from highrise.models_webapi import *
:
async def emote(self: BaseBot, user: User, message: str) -> None:
:
emote
.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).try: command, target, emote_id = message.split(" ") except: await self.highrise.chat("Invalid command format. Please use '/emote <target> <emote_id>'.") return
:
message
into three parts: command
, target
, and emote_id
. It expects the message to be in the format "/emote <target> <emote_id>".self.highrise.chat()
to inform the user about the invalid command format and then returns, ending the function.user = (await self.webapi.get_users(username=target))
:
get_users(username=target)
on self.webapi
to fetch user data based on the specified target
username.await
.user
appears to contain the user data.if user.total == 0:
:
total
attribute of the user
object is 0, which would mean that no user was found with the specified target
username.self.highrise.chat()
to inform the user about the invalid target and then returns, ending the function.user_id = user.users[0].user_id
:
target
username, the code assigns the user_id
of the first user in the response (user.users[0]
) to the user_id
variable.try: await self.highrise.send_emote(emote_id, user_id) except: await self.highrise.chat("Invalid emote.") return
:
self.highrise.send_emote(emote_id, user_id)
.emote_id
or some other issue), the exception is caught.self.highrise.chat()
to inform the user about the invalid emote and then returns, ending the function.