Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/TwitchIO/TwitchIO
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmTomahawkx committed Oct 31, 2022
2 parents dc634c1 + c23f726 commit 62f03ad
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
21 changes: 18 additions & 3 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Master
- ext.commands
- Bug fixes
- Make sure double-quotes are properly tokenized for bot commands

- ext.pubsub
- Additions
- Websocket automatically handles "RECONNECT" requests by Twitch
- Bug fixes
- Bug fixes
- Unsubscribing from Pubsubevents works again
- Fix a forgotten nonce in :func:`~twitchio.ext.pubsub.websocket._send_topics`
- :class:`~twitchio.ext.pubsub.PubSubModerationActionChannelTerms` now uses the correct type data
Expand All @@ -53,10 +53,25 @@ Master
- :func:`~twitchio.ext.eventsub.event_eventsub_notification_channel_goal_begin`
- :func:`~twitchio.ext.eventsub.event_eventsub_notification_channel_goal_progress`
- :func:`~twitchio.ext.eventsub.event_eventsub_notification_channel_goal_end`

- Channel subscription end
- :func:`~twitchio.ext.eventsub.EventSubClient.subscribe_channel_subscription_end`
- User authorization grant
- :func:`~twitchio.ext.eventsub.EventSubClient.subscribe_user_authorization_granted`

- HypeTrainBeginProgressData now has the :attr:`~twitchio.ext.eventsub.HypeTrainBeginProgressData.level`


- Bug fixes
Correct typo in :class:`~twitchio.ext.eventsub.HypeTrainBeginProgressData` attribute :attr:`~twitchio.ext.eventsub.HypeTrainBeginProgressData.expires`
- Correct typo in :class:`~twitchio.ext.eventsub.HypeTrainBeginProgressData` attribute :attr:`~twitchio.ext.eventsub.HypeTrainBeginProgressData.expires`
- Correct typo "revokation" to "revocation" in server _message_types.

- ext.pubsub
- Bug fixes
- "type" of :class:`~twitchio.ext.pubsub.PubSubModerationActionChannelTerms` now uses the correct type data

- Correct typo in :class:`~twitchio.ext.eventsub.HypeTrainBeginProgressData` attribute :attr:`~twitchio.ext.eventsub.HypeTrainBeginProgressData.expires`


2.4.0
======
Expand Down
8 changes: 8 additions & 0 deletions docs/exts/eventsub.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Event Reference
----------------
This is a list of events dispatched by the eventsub ext.

.. function:: event_eventsub_notification_user_authorization_grant(event: UserAuthorizationGrantedData)

Called when your app has had access granted on a channel.

.. function:: event_eventsub_revokation(event: RevokationEvent)

Called when your app has had access revoked on a channel.
Expand All @@ -108,6 +112,10 @@ This is a list of events dispatched by the eventsub ext.

Called when someone subscribes to a channel that you've subscribed to.

.. function:: event_eventsub_notification_subscription_end(event: ChannelSubscriptionEndData)

Called when a subscription to a channel that you've subscribed to ends.

.. function:: event_eventsub_notification_subscription_gift(event: ChannelSubscriptionGiftData)

Called when someone gifts a subscription to a channel that you've subscribed to.
Expand Down
48 changes: 48 additions & 0 deletions twitchio/ext/eventsub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,31 @@ def __init__(self, client: EventSubClient, data: dict):
self.is_gift: bool = data["is_gift"]


class ChannelSubscriptionEndData(EventData):
"""
A Subscription End event
Attributes
-----------
user: :class:`twitchio.PartialUser`
The user who subscribed
broadcaster: :class:`twitchio.PartialUser`
The channel that was subscribed to
tier: :class:`int`
The tier of the subscription
is_gift: :class:`bool`
Whether the subscription was a gift or not
"""

__slots__ = "user", "broadcaster", "tier", "is_gift"

def __init__(self, client: EventSubClient, data: dict):
self.user = _transform_user(client, data, "user")
self.broadcaster = _transform_user(client, data, "broadcaster_user")
self.tier = int(data["tier"])
self.is_gift: bool = data["is_gift"]


class ChannelSubscriptionGiftData(EventData):
"""
A Subscription Gift event
Expand Down Expand Up @@ -1145,6 +1170,25 @@ def __init__(self, client: EventSubClient, data: dict):
self.broadcaster = _transform_user(client, data, "broadcaster_user")


class UserAuthorizationGrantedData(EventData):
"""
An Authorization Granted event
Attributes
-----------
user: :class:`twitchio.PartialUser`
The user that has granted authorization for your app
client_id: :class:`str`
The client id of the app that had its authorization granted
"""

__slots__ = "client_id", "user"

def __init__(self, client: EventSubClient, data: dict):
self.user = _transform_user(client, data, "user")
self.client_id: str = data["client_id"]


class UserAuthorizationRevokedData(EventData):
"""
An Authorization Revokation event
Expand Down Expand Up @@ -1274,6 +1318,7 @@ def __init__(self, client: EventSubClient, data: dict):
ChannelBanData,
ChannelUnbanData,
ChannelSubscribeData,
ChannelSubscriptionEndData,
ChannelSubscriptionGiftData,
ChannelSubscriptionMessageData,
ChannelCheerData,
Expand All @@ -1294,6 +1339,7 @@ def __init__(self, client: EventSubClient, data: dict):
PredictionEndData,
StreamOnlineData,
StreamOfflineData,
UserAuthorizationGrantedData,
UserAuthorizationRevokedData,
UserUpdateData,
]
Expand All @@ -1312,6 +1358,7 @@ class _SubscriptionTypes(metaclass=_SubTypesMeta):

follow = "channel.follow", 1, ChannelFollowData
subscription = "channel.subscribe", 1, ChannelSubscribeData
subscription_end = "channel.subscription.end", 1, ChannelSubscriptionEndData
subscription_gift = "channel.subscription.gift", 1, ChannelSubscriptionGiftData
subscription_message = "channel.subscription.message", 1, ChannelSubscriptionMessageData
cheer = "channel.cheer", 1, ChannelCheerData
Expand Down Expand Up @@ -1356,6 +1403,7 @@ class _SubscriptionTypes(metaclass=_SubTypesMeta):
stream_start = "stream.online", 1, StreamOnlineData
stream_end = "stream.offline", 1, StreamOfflineData

user_authorization_grant = "user.authorization.grant", 1, UserAuthorizationGrantedData
user_authorization_revoke = "user.authorization.revoke", 1, UserAuthorizationRevokedData

user_update = "user.update", 1, UserUpdateData
Expand Down
12 changes: 10 additions & 2 deletions twitchio/ext/eventsub/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_message_types = {
"webhook_callback_verification": models.ChallengeEvent,
"notification": models.NotificationEvent,
"revokation": models.RevokationEvent,
"revocation": models.RevokationEvent,
}


Expand Down Expand Up @@ -120,6 +120,9 @@ def subscribe_channel_unbans(self, broadcaster: Union[PartialUser, str, int]):
def subscribe_channel_subscriptions(self, broadcaster: Union[PartialUser, str, int]):
return self._subscribe_with_broadcaster(models.SubscriptionTypes.subscription, broadcaster)

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

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

Expand Down Expand Up @@ -211,6 +214,11 @@ def subscribe_channel_prediction_lock(self, broadcaster: Union[PartialUser, str,
def subscribe_channel_prediction_end(self, broadcaster: Union[PartialUser, str, int]):
return self._subscribe_with_broadcaster(models.SubscriptionTypes.prediction_end, broadcaster)

async def subscribe_user_authorization_granted(self):
return await self._http.create_subscription(
models.SubscriptionTypes.user_authorization_grant, {"client_id": self.client._http.client_id}
)

async def subscribe_user_authorization_revoked(self):
return await self._http.create_subscription(
models.SubscriptionTypes.user_authorization_revoke, {"client_id": self.client._http.client_id}
Expand All @@ -234,7 +242,7 @@ async def _callback(self, request: web.Request) -> web.Response:
self.client.run_event(
f"eventsub_notification_{models.SubscriptionTypes._name_map[event.subscription.type]}", event
)
elif typ == "revokation":
elif typ == "revocation":
self.client.run_event("eventsub_revokation", event)

return response
Expand Down
7 changes: 3 additions & 4 deletions twitchio/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,9 @@ async def _connect(self):

await asyncio.sleep(retry)
return asyncio.create_task(self._connect())
if time.time() > self._last_ping + 240 or self._reconnect_requested:
# Re-authenticate as we have surpassed a PING request or Twitch issued a RECONNECT.
await self.authenticate(self._initial_channels)
self._reconnect_requested = False

await self.authenticate(self._initial_channels)

self._keeper = asyncio.create_task(self._keep_alive()) # Create our keep alive.
self._ws_ready_event.set()

Expand Down

0 comments on commit 62f03ad

Please sign in to comment.