From f20045646047c472837d90a773d5c004016907ff Mon Sep 17 00:00:00 2001 From: Latent Logic Date: Mon, 14 Oct 2024 18:44:38 -0700 Subject: [PATCH 1/2] Add message filter for shared chat from other rooms --- twitchAPI/chat/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/twitchAPI/chat/__init__.py b/twitchAPI/chat/__init__.py index f987e4b..9bc738e 100644 --- a/twitchAPI/chat/__init__.py +++ b/twitchAPI/chat/__init__.py @@ -556,7 +556,8 @@ def __init__(self, is_verified_bot: bool = False, initial_channel: Optional[List[str]] = None, callback_loop: Optional[asyncio.AbstractEventLoop] = None, - no_message_reset_time: Optional[float] = 10): + no_message_reset_time: Optional[float] = 10, + no_shared_chat_messages: bool = True): """ :param twitch: A Authenticated twitch instance :param connection_url: alternative connection url |default|:code:`None` @@ -568,6 +569,8 @@ def __init__(self, :param no_message_reset_time: How many minutes of mo messages from Twitch before the connection is considered dead. Twitch sends a PING just under every 5 minutes and the bot must respond to them for Twitch to keep the connection active. At 10 minutes we've definitely missed at least one PING |default|:code:`10` + :param no_shared_chat_messages: Filter out Twitch shared chat messages from other channels. This will only + listen for messages that were sent in the chat room that the bot is listening in. """ self.logger: Logger = getLogger('twitchAPI.chat') """The logger used for Chat related log messages""" @@ -584,6 +587,7 @@ def __init__(self, """Jitter in seconds for ping messages. This should usually not be changed.""" self._callback_loop = callback_loop self.no_message_reset_time: Optional[float] = no_message_reset_time + self.no_shared_chat_messages: bool = no_shared_chat_messages self.listen_confirm_timeout: int = 30 """Time in second that any :code:`listen_` should wait for its subscription to be completed.""" self.reconnect_delay_steps: List[int] = [0, 1, 2, 4, 8, 16, 32, 64, 128] @@ -1091,6 +1095,9 @@ async def _handle_ready(self, parsed: dict): t.add_done_callback(self._task_callback) async def _handle_msg(self, parsed: dict): + if self.no_shared_chat_messages and "source-room-id" in parsed["tags"]: + if parsed["tags"]["source-room-id"] != parsed["tags"].get("room-id"): + return async def _can_execute_command(_com: ChatCommand, _name: str) -> bool: for mid in self._command_middleware + self._command_specific_middleware.get(_name, []): From 23a050575ce4d6e4f73f7e5c90897998444c1e6e Mon Sep 17 00:00:00 2001 From: Latent Logic Date: Fri, 25 Oct 2024 16:59:26 -0700 Subject: [PATCH 2/2] Add shared chat middleware as another option for filtering --- twitchAPI/chat/middleware.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/twitchAPI/chat/middleware.py b/twitchAPI/chat/middleware.py index e28443f..e35d89e 100644 --- a/twitchAPI/chat/middleware.py +++ b/twitchAPI/chat/middleware.py @@ -230,3 +230,16 @@ async def can_execute(self, command: 'ChatCommand') -> bool: async def was_executed(self, command: 'ChatCommand'): self._last_executed[command.name] = datetime.now() + + +class SharedChatOnlyCurrent(BaseCommandMiddleware): + """Restricts commands to only current chat room in Shared Chat streams""" + + async def can_execute(self, command: ChatCommand) -> bool: + if "source-room-id" in command._parsed["tags"]: + if command._parsed["tags"]["source-room-id"] != command._parsed["tags"].get("room-id"): + return False + return True + + async def was_executed(self, command: ChatCommand): + pass