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

Do you ever wondered how to make your bot follow you? Well, now you can do that with simple commands and this way you can get your own private Butler Bot!

https://youtu.be/uhQ8NaHCVZo

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.

Methods/Functions used:

Required Libraries

Code

First of all, letโ€™s create a new file called follow.py inside of our functions folder:

File structure example in the workspace.

File structure example in the workspace.

Hereโ€™s what we will need to import into our follow.py file:

from highrise import *
from highrise.models import *
import asyncio

Letโ€™s proceed to our functions definitions, 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 mines as follow and stop.

Since we want that the bot continuously follows us, this function has to be an loop and be asynchronously executed. For that, we will be using the buit-in Highrise taskgroup, and we will make a function called following_loop to be inserted in this taskgroup

from highrise import *
from highrise.models import *
import asyncio

async def follow(self: BaseBot, user: User, message: str) -> None:
		async def following_loop(self: BaseBot, user: User, message: str) -> None:
				pass
		pass

async def stop(self: BaseBot, user: User, message: str) -> None:
		pass

Finally, letโ€™s implement the functions and after it you will see a detailed step by step guide on how the code works:

from highrise import *
from highrise.models import *
import asyncio
from asyncio import Task

async def follow(self: BaseBot, user: User, message: str) -> None:
    async def following_loop(self: BaseBot, user: User, message: str) -> None:
        if message.startswith("/following_loop"):
            await self.highrise.chat("Invalid command, please use /follow")
            return
        while True:
            #gets the user position
            room_users = (await self.highrise.get_room_users()).content
            for room_user, position in room_users:
                if room_user.id == user.id:
                    user_position = position
                    break
            print(user_position)
            if type(user_position) != AnchorPosition:
                await self.highrise.walk_to(Position(user_position.x + 1, user_position.y, user_position.z))
            await asyncio.sleep(0.5)
    taskgroup = self.highrise.tg
    task_list = list(taskgroup._tasks)
    for task in task_list:
        if task.get_name() == "following_loop":
            await self.highrise.chat("Already following someone")
            return
    #checks if this function is already in the Highrise class tg (task group).
    taskgroup.create_task(coro=following_loop(self, user, message))
    task_list : list[Task] = list(taskgroup._tasks)
    # Sets the name of the task who has the following_loop function to "following_loop"
    for task in task_list:
        if task.get_coro().__name__ == "following_loop":
            task.set_name("following_loop")
    await self.highrise.chat(f"Following {user.username}")
    
async def stop(self: BaseBot, user: User, message: str) -> None:
    taskgroup = self.highrise.tg
    task_list = list(taskgroup._tasks)
    for task in task_list:
        if task.get_name() == "following_loop":
            task.cancel()
            await self.highrise.chat(f"Stopping following {user.username}")
            return
    await self.highrise.chat("Not following anyone")
    return

Now letโ€™s understand how the code works:

  1. from highrise import * and from highrise.models import *:
  2. async def follow(self: BaseBot, user: User, message: str) -> None::
  3. taskgroup = self.highrise.tg and task_list = list(taskgroup._tasks):
  4. for task in task_list: if task.get_name() == "following_loop": await self.highrise.chat("Already following someone") return:
  5. taskgroup.create_task(coro=following_loop(self, user, message), name="following_loop"):
  6. await self.highrise.chat(f"Following {user.username}"):
  7. async def stop(self: BaseBot, user: User, message: str) -> None::
  8. The stop function has similar initial code as the follow function to get the task group and task list.
  9. for task in task_list: if task.get_name() == "following_loop": task.cancel() await self.highrise.chat(f"Stopping following {user.username}") return:
  10. await self.highrise.chat("Not following anyone") return:
  11. async def following_loop(self: BaseBot, user: User, message: str) -> None::
  12. if message.startswith("/following_loop"): await self.highrise.chat("Invalid command, please use /follow") return:
  13. while True::
  14. (await self.highrise.get_room_users()).content:
  15. The code then iterates through the room users and their corresponding positions to find the user_position of the specified user.
  16. if type(user_position) != AnchorPosition: await self.highrise.walk_to(Position(user_position.x + 1, user_position.y, user_position.z)):
  17. await asyncio.sleep(0.5):