🚨 Creating a command whitelist

🚨 Creating a command whitelist

Concept

In this guide we will abroad how to create a whitelist or to allow only a few users to be able to use a function from your bot.
Β 
This is extra helpful when you want to add some functions to your bot that might be dangerous to have it public for any user to reach it, in those case we first check if the user using the function has enough privileges or meet a certain criteria to use a function.

Required Libraries

For this guide we will only be using the standard Highrise Bot SDK.

Code

Let’s see some examples on how to filter which players can use certain commands, the basic idea will be to always use an if statement to check if the user has meet the criteria.
Regular username check
A regular username name check would be a simple code to check if the username is equal to the username previously specified:
if user.username != "ItsVini": return
When you have this block at the start of a function, it will check if the User’s username is different than ItsVini, if this happen, the function will simply return nothing and the function will not continue, you can also make the bot say something before returning:
if user.username != "ItsVini": await self.highrise.chat("Sorry, only ItsVini can use this command.") return
Username list check
This method can be used declaring an username list that can be created inside of the function or outside of it, depends on which way you want to do it. For example you can do:
async def any_function(self, user: User, message: str) -> None: allowed_users = ["ItsVini", "ClaraMatos", "iHsein"] if user.username not in allowed_users: await self.highrise.chat("You're not alowed to use this command!") return
Or you can declare the list as a class attribute:
class Bot(BaseBot): whitelist= ["ItsVini", "ClaraMatos", "iHsein"] async def any_function(self, user: User, message: str) -> None: if user.username not in self.whitelist: await self.highrise.chat("You're not alowed to use this command!") return
The difference is that the when declared as a class attribute, it can be called anywhere, in the other hand, when created locally inside of an function, it can only be called from that function.
User privileges check
Let’s say you don’t wanna to go everytime in your code to add one of your moderators to the allowed users to use a command. In this case you can make a request to the API to check if that user has moderator privileges:
user_privileges = await self.highrise.get_room_privilege(user.id) if (not user_privileges.moderator) and (user.username != "ItsVini"): await self.highrise.chat("You are not a moderator, you can't use this command.") return
In this code we are checking if the user is not a moderator and if the user is not the room owner (me), in this case, they wont be able to use the command.
Position check
Another cool way to allow someone to use a command if they are in a certain position, let’s say that we want to allow someone to use a command only if they are standing at the position x = 3, y = 0, z = 5, facing the front right. In this case we can check it in this way:
user_positions = (await self.highrise.get_room_users()).content user_allowed = False for room_user, position in user_positions: if room_user.username == user.username and position == Position(x=3.0, y=0.0, z=5.0, facing="FrontRight"): user_allowed = True break if not user_allowed: await self.highrise.chat("You are not allowed to use this command!") return
Β 
Β 
Β 
Built with Potion.so