Skip to content

Commit

Permalink
Add channel.ad_break.begin for EventSub (#464)
Browse files Browse the repository at this point in the history
* Introduce `channel.ad_break.begin` for EventSub

Implementation for this subscription type: https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#channelad_breakbegin

* Fix docs mess-up

Sorry, I m noob in docs and stuff
  • Loading branch information
Aluerie authored Sep 7, 2024
1 parent 8e91cec commit 77ef79d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/exts/eventsub.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ This is a list of events dispatched by the eventsub ext.

Called when a user donates to an active charity campaign.

.. function:: event_eventsub_notification_channel_ad_break_begin(event: ChannelAdBreakBeginData)

Called when a user runs a midroll commercial break, either manually or automatically via ads manager.

API Reference
--------------

Expand Down Expand Up @@ -543,6 +547,12 @@ API Reference
:members:
:inherited-members:

.. attributetable::: ChannelAdBreakBeginData
.. autoclass:: ChannelAdBreakBeginData
:members:
:inherited-members:

.. autoclass:: ChannelVIPAddRemove
:members:
:inherited-members:
Expand Down
33 changes: 33 additions & 0 deletions twitchio/ext/eventsub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,36 @@ def __init__(self, client: EventSubClient, data: dict) -> None:
self.trust_status: Literal["active_monitoring", "restricted", "none"] = data["low_trust_status"]


class ChannelAdBreakBeginData(EventData):
"""
An ad begin event.
Attributes
-----------
is_automatic: :class:`bool`
Whether the ad was run manually or automatically via ads manager.
broadcaster: :class:`~twitchio.PartialUser`
The channel where a midroll commercial break has started running.
requester: Optional[:class:`twitchio.PartialUser`]
The user who started the ad break. Will be ``None`` if ``is_automatic`` is ``True``.
duration: :class:`int`
The ad duration in seconds.
started_at: :class:`datetime.datetime`
When the ad began.
"""

__slots__ = ("is_automatic", "broadcaster", "requester", "duration", "started_at")

def __init__(self, client: EventSubClient, data: dict) -> None:
self.is_automatic: bool = data["is_automatic"]
self.broadcaster: PartialUser = _transform_user(client, data, "broadcaster_user")
self.requester: Optional[PartialUser] = (
None if self.is_automatic else _transform_user(client, data, "requester_user")
)
self.duration: int = data["duration_seconds"]
self.started_at: datetime.datetime = _parse_datetime(data["started_at"])


class AutoCustomReward:
"""
A reward object for an Auto Reward Redeem.
Expand Down Expand Up @@ -2318,6 +2348,7 @@ def __init__(self, client: EventSubClient, data: dict) -> None:
ChannelModerateData,
AutoRewardRedeem,
ChannelVIPAddRemove,
ChannelAdBreakBeginData,
]


Expand Down Expand Up @@ -2410,6 +2441,8 @@ class _SubscriptionTypes(metaclass=_SubTypesMeta):

suspicious_user_update = "channel.suspicious_user.update", 1, SuspiciousUserUpdateData

channel_ad_break_begin = "channel.ad_break.begin", 1, ChannelAdBreakBeginData


SubscriptionTypes = _SubscriptionTypes()

Expand Down
3 changes: 3 additions & 0 deletions twitchio/ext/eventsub/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ def subscribe_channel_vip_add(self, broadcaster: Union[PartialUser, str, int]):
def subscribe_channel_vip_remove(self, broadcaster: Union[PartialUser, str, int]):
return self._subscribe_with_broadcaster(models.SubscriptionTypes.channel_vip_remove, broadcaster)

def subscribe_channel_ad_break_begin(self, broadcaster: Union[PartialUser, str, int]):
return self._subscribe_with_broadcaster(models.SubscriptionTypes.channel_ad_break_begin, broadcaster)

async def subscribe_user_authorization_granted(self):
return await self._http.create_webhook_subscription(
models.SubscriptionTypes.user_authorization_grant, {"client_id": self.client._http.client_id}
Expand Down
3 changes: 3 additions & 0 deletions twitchio/ext/eventsub/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,6 @@ async def subscribe_channel_vip_add(self, broadcaster: Union[PartialUser, str, i

async def subscribe_channel_vip_remove(self, broadcaster: Union[PartialUser, str, int], token: str):
await self._subscribe_with_broadcaster(models.SubscriptionTypes.channel_vip_remove, broadcaster, token)

async def subscribe_channel_ad_break_begin(self, broadcaster: Union[PartialUser, str, int], token: str):
await self._subscribe_with_broadcaster(models.SubscriptionTypes.channel_ad_break_begin, broadcaster, token)

0 comments on commit 77ef79d

Please sign in to comment.