The Rust+ library uses a decorator-based event system to listen for specific socket events. When an event occurs on the server, the corresponding decorated asynchronous function is called by the socket.
There are four primary event types you can listen for:
- Chat Event: Triggered when a message is sent to team chat.
- Entity Event: Triggered when an entity (like an alarm or door) changes state.
- Team Event: Triggered when team information changes (e.g., players joining or leaving).
- Protobuf Event: Triggered when raw protobuf data is received over the websocket (intended for monitoring only).
Important Requirement for Entity Events:
You must call get_entity_info(eid) for a specific entity ID before an EntityEvent listener will receive any data. This action effectively "subscribes" your client to that entity.
from rustplus import EntityEventPayload, TeamEventPayload, ChatEventPayload, ProtobufEvent, ChatEvent, EntityEvent, TeamEvent
# Example of subscribing to an Entity Event
@EntityEvent(server_details, 25743493)
async def alarm(event: EntityEventPayload):
value = "On" if event.value else "Off"
print(f"Entity has been turned {value}")
# Example of subscribing to a Team Event
@TeamEvent(server_details)
async def team(event: TeamEventPayload):
print(f"The team leader's steamId is: {event.team_info.leader_steam_id}")
# Example of subscribing to a Chat Event
@ChatEvent(server_details)
async def chat(event: ChatEventPayload):
print(f"{event.message.name}: {event.message.message}")
# Example of subscribing to a Protobuf Event
@ProtobufEvent(server_details)
async def proto(data: bytes):
print(data)