πŸ•ΊπŸΌ Emote Sender

πŸ•ΊπŸΌ Emote Sender

Concept

This is a simple code snippet to show how to make a target user perform a given emote using only on_chat commands:
Video preview
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>.

Methods/Functions used:

Required Libraries

  • highrise-bot-sdk

Code

First of all, let’s create a new file called emote.py inside of our functions folder:
notion image
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 *
Modules to import in the emote.py file.
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):
Example of how the function will be defined in the emote.py file.
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
Function implementation in the emote.py file.
Now let’s understand how the code works:
  1. from highrise import *, from highrise.models import *, from highrise.webapi import *, and from highrise.models_webapi import *:
      • These lines import the necessary modules and classes required for interacting with the game's API, both for the main bot logic and for making API requests.
  1. async def emote(self: BaseBot, user: User, message: str) -> None::
      • This is an asynchronous function called emote.
      • 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), and message (a string representing the chat message sent by the user).
  1. try: command, target, emote_id = message.split(" ") except: await self.highrise.chat("Invalid command format. Please use '/emote <target> <emote_id>'.") return:
      • The code uses a try-except block to split the message into three parts: command, target, and emote_id. It expects the message to be in the format "/emote <target> <emote_id>".
      • If the split fails (likely due to an incorrect format), it means the user didn't provide the required three arguments.
      • In that case, the function sends a message to the chat using self.highrise.chat() to inform the user about the invalid command format and then returns, ending the function.
  1. user = (await self.webapi.get_users(username=target)):
      • The code calls an asynchronous method get_users(username=target) on self.webapi to fetch user data based on the specified target username.
      • The result is awaited using await.
      • The method seems to return some kind of response object, and user appears to contain the user data.
  1. if user.total == 0::
      • The code checks if the total attribute of the user object is 0, which would mean that no user was found with the specified target username.
      • If no user is found, the function sends a message to the chat using self.highrise.chat() to inform the user about the invalid target and then returns, ending the function.
  1. user_id = user.users[0].user_id:
      • If a user is found with the specified target username, the code assigns the user_id of the first user in the response (user.users[0]) to the user_id variable.
  1. try: await self.highrise.send_emote(emote_id, user_id) except: await self.highrise.chat("Invalid emote.") return:
      • The code uses another try-except block to attempt to send an emote to the specified target user using self.highrise.send_emote(emote_id, user_id).
      • If the emote sending fails (likely due to an invalid emote_id or some other issue), the exception is caught.
      • In that case, the function sends a message to the chat using self.highrise.chat() to inform the user about the invalid emote and then returns, ending the function.
Β 
Β 
Built with Potion.so