Managing Player Cache:
The
Get Players In Roombot.room.cache provides methods for managing the player cache in the room. This cache utilizes the bot's memory and enhances performance by reducing API requests. Alternatively, you can use:Enabling Cache:
To enable the player cache, update the bot instance to include the events "Joins", "Leaves", and "Movements", and set
Cache to true.const bot = new Highrise({ Events: [ Events.Joins, // Listen for player joins. Events.Leaves, // Listen for player leaves. Events.Movements // Listen for player movements. ], Cache: true // Enable player cache. });
Methods:
Retrieving Players:
Retrieves the list of players currently in the room.
// Assuming you have defined the Highrise instance as "bot". bot.on("chatCreate", async (user, message) => { if (message === "players") { const players = await bot.room.cache.get(); const playerList = players.map(player => player[0].username).join(", "); console.log("Players in the room: " + playerList); } });
Retrieving User ID:
Retrieves the username of a player using their user ID.
// Assuming you have defined the Highrise instance as "bot". bot.on("chatCreate", async (user, message) => { if (message === "id") { const playerId = await bot.room.cache.username(user.username); console.log(`The user's ID is: ${playerId}`); } });
Retrieving Username:
Retrieves the user ID of a player using their username.
// Assuming you have defined the Highrise instance as "bot". bot.on("chatCreate", async (user, message) => { if (message === "username") { const username = await bot.room.cache.id(user.id); console.log(`The user's username is: ${username}`); } });
Clearing Cache:
Clears the player cache.
// Assuming you have defined the Highrise instance as "bot". bot.on("chatCreate", async (user, message) => { if (message === "clear") { bot.room.cache.clear(); } });
Filtering Players:
Filters players based on custom criteria specified in the callback function.
// Assuming you have defined the Highrise instance as "bot". bot.on("chatCreate", async (user, message) => { if (message === "filter") { const players = bot.room.cache.filter(player => player.position.y === 0.25); console.log(`Players at position 0:`, players); } });
Getting Size:
Returns the number of players in the room.
// Assuming you have defined the Highrise instance as "bot". bot.on("chatCreate", async (user, message) => { if (message === "size") { const players = bot.room.cache.size(); console.log(`There are ${players} players in the room.`); } });
These methods provide efficient ways to manage and retrieve player data in the room cache, enhancing performance and responsiveness of the bot.
Go Back: Fetch Room Information