From 6a201ac4fa8fe6fb0a2ac60f5c174dd9fed74bb1 Mon Sep 17 00:00:00 2001 From: olijeffers0n <69084614+olijeffers0n@users.noreply.github.com> Date: Fri, 31 Dec 2021 23:08:39 +0000 Subject: [PATCH] Adding ChatEvent --- rustplus/__init__.py | 4 ++-- rustplus/api/base_rust_api.py | 7 +++++++ rustplus/api/remote/event_handler.py | 8 +++++++- rustplus/api/remote/rustws.py | 2 +- rustplus/api/structures/__init__.py | 2 +- rustplus/api/structures/event.py | 7 +++++++ 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/rustplus/__init__.py b/rustplus/__init__.py index 3d3d03a..d79eabf 100644 --- a/rustplus/__init__.py +++ b/rustplus/__init__.py @@ -5,10 +5,10 @@ from .api import RustSocket from .commands import CommandOptions, Command from .exceptions import * -from .api.structures import EntityEvent, TeamEvent +from .api.structures import EntityEvent, TeamEvent, ChatEvent from .utils import entity_type_to_string __name__ = "rustplus" __author__ = "olijefferson" -__version__ = "5.1.1" +__version__ = "5.1.2" __support__ = "Discord: https://discord.gg/nQqJe8qvP8" diff --git a/rustplus/api/base_rust_api.py b/rustplus/api/base_rust_api.py index 621bd56..20339d9 100644 --- a/rustplus/api/base_rust_api.py +++ b/rustplus/api/base_rust_api.py @@ -118,6 +118,13 @@ def team_event(self, coro) -> None: self.remote.event_handler.register_event("team_changed", (coro, asyncio.get_event_loop())) + def chat_event(self, coro) -> None: + """ + A Decorator to register an event listener for chat messages + """ + + self.remote.event_handler.register_event("chat_message", (coro, asyncio.get_event_loop())) + async def hang(self) -> None: """This Will permanently put your script into a state of 'hanging' Cannot be Undone. Only do this in scripts using commands""" diff --git a/rustplus/api/remote/event_handler.py b/rustplus/api/remote/event_handler.py index 7f73a58..92edb55 100644 --- a/rustplus/api/remote/event_handler.py +++ b/rustplus/api/remote/event_handler.py @@ -1,6 +1,6 @@ import asyncio -from ..structures import EntityEvent, TeamEvent +from ..structures import EntityEvent, TeamEvent, ChatEvent class EventHandler: @@ -29,3 +29,9 @@ def run_team_event(self, app_message) -> None: if hasattr(self, "team_changed"): coro, loop = getattr(self, "team_changed") self._schedule_event(loop, coro, TeamEvent(app_message)) + + def run_chat_event(self, app_message) -> None: + + if hasattr(self, "chat_message"): + coro, loop = getattr(self, "chat_message") + self._schedule_event(loop, coro, ChatEvent(app_message)) diff --git a/rustplus/api/remote/rustws.py b/rustplus/api/remote/rustws.py index c29933f..05c73a1 100644 --- a/rustplus/api/remote/rustws.py +++ b/rustplus/api/remote/rustws.py @@ -69,7 +69,7 @@ def received_message(self, message): self.remote.event_handler.run_team_event(app_message) elif self.is_message(app_message): - return + self.remote.event_handler.run_chat_event(app_message) else: self.responses[app_message.response.seq] = app_message diff --git a/rustplus/api/structures/__init__.py b/rustplus/api/structures/__init__.py index 2f823e5..400a04e 100644 --- a/rustplus/api/structures/__init__.py +++ b/rustplus/api/structures/__init__.py @@ -7,4 +7,4 @@ from .rust_entity_info import RustEntityInfo from .rust_contents import RustContents from .rust_item import RustItem -from .event import EntityEvent, TeamEvent +from .event import EntityEvent, TeamEvent, ChatEvent diff --git a/rustplus/api/structures/event.py b/rustplus/api/structures/event.py index 91aa92c..e026685 100644 --- a/rustplus/api/structures/event.py +++ b/rustplus/api/structures/event.py @@ -1,5 +1,6 @@ from typing import List from .rust_team_info import RustTeamInfo +from .rust_chat_message import RustChatMessage class EntityEvent: @@ -28,3 +29,9 @@ def __init__(self, app_message) -> None: self.playerId : int = app_message.broadcast.teamChanged.playerId self.teamInfo = RustTeamInfo(app_message.broadcast.teamChanged.teamInfo) + +class ChatEvent: + + def __init__(self, app_message) -> None: + + self.message = RustChatMessage(app_message.broadcast.teamMessage.message)