Get Started - Learn How To Make Your Bot!
List of all currently Free Items
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!
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.
First of all, letโs create a new file called follow.py inside of our functions folder:
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:
from highrise import *
and from highrise.models import *
:
async def follow(self: BaseBot, user: User, message: str) -> None:
:
follow
.self
(a reference to the instance of the class this method belongs to), user
(an object representing the user to follow), and message
(a string representing the chat message sent by the user).taskgroup = self.highrise.tg
and task_list = list(taskgroup._tasks)
:
tg
) from self.highrise
and creates a list of tasks (task_list
) in that task group.for task in task_list: if task.get_name() == "following_loop": await self.highrise.chat("Already following someone") return
:
self.highrise.chat()
to inform the user that the bot is already following someone, and then returns, ending the function.taskgroup.create_task(coro=following_loop(self, user, message), name="following_loop")
:
taskgroup
).following_loop(self, user, message)
as the coroutine to execute for the task.await self.highrise.chat(f"Following {user.username}")
:
self.highrise.chat()
to inform the user that the bot is now following the specified user
.async def stop(self: BaseBot, user: User, message: str) -> None:
:
stop
.self
(a reference to the instance of the class this method belongs to), user
(an object representing the user the bot is following), and message
(a string representing the chat message sent by the user).stop
function has similar initial code as the follow
function to get the task group and task list.for task in task_list: if task.get_name() == "following_loop": task.cancel() await self.highrise.chat(f"Stopping following {user.username}") return
:
taskgroup
).task.cancel()
.self.highrise.chat()
to inform the user that the bot is stopping following the specified user
, and then returns, ending the function.await self.highrise.chat("Not following anyone") return
:
async def following_loop(self: BaseBot, user: User, message: str) -> None:
:
following_loop
.self
(a reference to the instance of the class this method belongs to), user
(an object representing the user the bot is following), and message
(a string representing the chat message sent by the user).if message.startswith("/following_loop"): await self.highrise.chat("Invalid command, please use /follow") return
:
message
starts with "/following_loop". If it does, it means the user entered an invalid command directly for the following_loop
function.self.highrise.chat()
to inform the user that they should use "/follow" instead of "/following_loop", and then returns, ending the function.while True:
:
(await self.highrise.get_room_users()).content
:
get_room_users()
on self.highrise
to fetch a list of users in the room where this chat event occurred.await
.content
appears to be the actual list of room users.user_position
of the specified user
.if type(user_position) != AnchorPosition: await self.highrise.walk_to(Position(user_position.x + 1, user_position.y, user_position.z))
:
user_position
is not an AnchorPosition
type.self.highrise.walk_to(Position(...))
to move the bot to the right of the user by increasing the x
coordinate by 1 (it follows the user from the right side).walk_to
method seems to be used for moving the bot in the game.await asyncio.sleep(0.5)
:
asyncio.sleep(0.5)
before the next iteration of the loop.