Skip to content

Commit

Permalink
Merge pull request Teekeks#334 from Latent-Logic/no-shared-chat
Browse files Browse the repository at this point in the history
Add message filter for shared chat from other rooms
  • Loading branch information
Teekeks authored Dec 19, 2024
2 parents 4d4b976 + 23a0505 commit 47e95a5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion twitchAPI/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,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`
Expand All @@ -578,6 +579,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"""
Expand All @@ -594,6 +597,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]
Expand Down Expand Up @@ -1101,6 +1105,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, []):
Expand Down
13 changes: 13 additions & 0 deletions twitchAPI/chat/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 47e95a5

Please sign in to comment.