diff --git a/kick/client.py b/kick/client.py index 874fd75..85be673 100644 --- a/kick/client.py +++ b/kick/client.py @@ -8,7 +8,7 @@ from .chatroom import Chatroom, PartialChatroom from .chatter import PartialChatter from .http import HTTPClient -from .livestream import PartialLivestream +from .livestream import PartialLivestream, LivestreamEnd from .message import Message from .users import ClientUser, PartialUser, User from .utils import MISSING, decorator, setup_logging @@ -329,7 +329,18 @@ async def on_livestream_start(self, livestream: PartialLivestream) -> None: livestream: `PartialLivestream` The livestream """ + async def on_livestream_end(self, livestream_End: LivestreamEnd) -> None: + """ + |coro| + on_livestream_end is an event that can be overriden with the `Client.event` decorator or with a subclass. + This is called when a user that is being watched ended streaming + + Parameters + ----------- + livestream: `LivestreamEnd` + The livestream + """ async def on_follow(self, streamer: User) -> None: """ |coro| diff --git a/kick/livestream.py b/kick/livestream.py index 282f9e1..b59adee 100644 --- a/kick/livestream.py +++ b/kick/livestream.py @@ -11,10 +11,10 @@ if TYPE_CHECKING: from .http import HTTPClient from .types.videos import LivestreamPayload - from .types.ws import PartialLivestreamPayload + from .types.ws import PartialLivestreamPayload, LivestreamEndPayload from .users import User -__all__ = ("Livestream", "PartialLivestream") +__all__ = ("Livestream", "PartialLivestream", "LivestreamEnd") class PartialLivestream: @@ -229,3 +229,29 @@ def __eq__(self, other: object) -> bool: def __repr__(self) -> str: return f"" + +class LivestreamEnd(HTTPDataclass["LivestreamEndPayload"]): + """ + A dataclass which represents a livestream end on kick. + + Attributes + ----------- + id: int + The livestream's id + channel_id: int + The livestream's channel id + title: str + The livestream's title + streamer: `User` | None + The livestream's streaner + """ + @property + def id(self) -> int: + return self._data["id"] + @property + def channel_id(self): + return self._data["channel"]["id"] + + @property + def streamer(self) -> User | None: + return self.http.client._watched_users.get(self.channel_id) \ No newline at end of file diff --git a/kick/types/ws.py b/kick/types/ws.py index 739c068..05b5c53 100644 --- a/kick/types/ws.py +++ b/kick/types/ws.py @@ -18,3 +18,8 @@ class FollowersUpdatePayload(TypedDict): username: None | str created_at: int followed: bool + + +class LivestreamEndPayload(TypedDict): + id: int + channel_id: int \ No newline at end of file diff --git a/kick/ws.py b/kick/ws.py index a1fbe11..73cf3a4 100644 --- a/kick/ws.py +++ b/kick/ws.py @@ -5,7 +5,7 @@ from aiohttp import ClientWebSocketResponse as WebSocketResponse -from .livestream import PartialLivestream +from .livestream import PartialLivestream, LivestreamEnd from .message import Message if TYPE_CHECKING: @@ -31,10 +31,10 @@ async def poll_event(self) -> None: match raw_data["event"]: case "App\\Events\\ChatMessageEvent": - msg = Message(data=data["livestream"], http=self.http) + msg = Message(data=data, http=self.http) self.http.client.dispatch("message", msg) case "App\\Events\\StreamerIsLive": - livestream = PartialLivestream(data=data, http=self.http) + livestream = PartialLivestream(data=data["livestream"], http=self.http) self.http.client.dispatch("livestream_start", livestream) case "App\\Events\\FollowersUpdated": user = self.http.client._watched_users[data["channel_id"]] @@ -46,7 +46,9 @@ async def poll_event(self) -> None: user._data["followers_count"] -= 1 self.http.client.dispatch(event, user) - + case "App\\Events\\StopStreamBroadcast": + livestream = LivestreamEnd(data=data["livestream"], http=self.http) + self.http.client.dispatch("livestream_end", livestream) async def start(self) -> None: while not self.ws.closed: await self.poll_event()