Skip to content

Commit 01d4072

Browse files
committed
Merge branch 'master' of https://github.com/TwitchIO/TwitchIO
2 parents e59a4f3 + f179bd8 commit 01d4072

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

docs/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ Master
5151

5252
- ext.eventsub
5353
- Additions
54+
- Added Gift Subcriptions subscriptions for gifting other users Subs:
55+
- Subscribed via :func:`twitchio.ext.eventsub.EventSubClient.subscribe_channel_subscription_gifts`
56+
- Callback function is :func:`twitchio.ext.eventsub.event_eventsub_notification_subscription_gift`
57+
- Added Resubscription Message subscriptions for Resub messages:
58+
- Subscribed via :func:`twitchio.ext.eventsub.EventSubClient.subscribe_channel_subscription_messages`
59+
- Callback function is :func:`twitchio.ext.eventsub.event_eventsub_notification_subscription_message`
60+
- Added :func:`twitchio.ext.eventsub.EventSubClient.delete_all_active_subscriptions` for convenience
5461
- Created an Eventsub-specific :class:`~twitchio.ext.eventsub.CustomReward` model
5562

5663
2.3.0

docs/exts/eventsub.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ This is a list of events dispatched by the eventsub ext.
108108

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

111+
.. function:: event_eventsub_notification_subscription_gift(event: ChannelSubscriptionGiftData)
112+
113+
Called when someone gifts a subscription to a channel that you've subscribed to.
114+
115+
.. function:: event_eventsub_notification_subscription_message(event: ChannelSubscriptionMessageData)
116+
117+
Called when someone resubscribes with a message to a channel that you've subscribed to.
118+
111119
.. function:: event_eventsub_notification_cheer(event: ChannelCheerData)
112120

113121
Called when someone cheers on a channel you've subscribed to.
@@ -152,8 +160,7 @@ API Reference
152160

153161
.. autoclass:: EventSubClient
154162
:members:
155-
156-
(The above is broken, and does not show members)
163+
:undoc-members:
157164

158165
.. attributetable:: Subscription
159166

@@ -179,6 +186,18 @@ API Reference
179186
:members:
180187
:inherited-members:
181188

189+
.. attributetable::: ChannelSubscriptionGiftData
190+
191+
.. autoclass:: ChannelSubscriptionGiftData
192+
:members:
193+
:inherited-members:
194+
195+
.. attributetable::: ChannelSubscriptionMessageData
196+
197+
.. autoclass:: ChannelSubscriptionMessageData
198+
:members:
199+
:inherited-members:
200+
182201
.. attributetable::: ChannelCheerData
183202
184203
.. autoclass:: ChannelCheerData

twitchio/ext/eventsub/models.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,77 @@ def __init__(self, client: EventSubClient, data: dict):
237237
self.is_gift: bool = data["is_gift"]
238238

239239

240+
class ChannelSubscriptionGiftData(EventData):
241+
"""
242+
A Subscription Gift event
243+
Explicitly, the act of giving another user a Subscription.
244+
Receiving a gift-subscription uses ChannelSubscribeData above, with is_gift is ``True``
245+
246+
Attributes
247+
-----------
248+
is_anonymous: :class:`bool`
249+
Whether the gift sub was anonymous
250+
user: Optional[:class:`twitchio.PartialUser`]
251+
The user that gifted subs. Will be ``None`` if ``is_anonymous`` is ``True``
252+
broadcaster: :class:`twitchio.PartialUser`
253+
The channel that was subscribed to
254+
tier: :class:`int`
255+
The tier of the subscription
256+
total: :class:`int`
257+
The total number of subs gifted by a user at once
258+
cumulative_total: Optional[:class:`int`]
259+
The total number of subs gifted by a user overall. Will be ``None`` if ``is_anonymous`` is ``True``
260+
"""
261+
262+
__slots__ = "is_anonymous", "user", "broadcaster", "tier", "total", "cumulative"
263+
264+
def __init__(self, client: EventSubClient, data: dict):
265+
self.is_anonymous: bool = data["is_anonymous"]
266+
self.user: Optional[PartialUser] = _transform_user(client, data, "user") if not self.is_anonymous else None
267+
self.broadcaster: Optional[PartialUser] = _transform_user(client, data, "broadcaster_user")
268+
self.tier = int(data["tier"])
269+
self.total = int(data["total"])
270+
self.cumulative_total: Optional[int] = int(data["cumulative_total"]) if not self.is_anonymous else None
271+
272+
273+
class ChannelSubscriptionMessageData(EventData):
274+
"""
275+
A Subscription Message event.
276+
A combination of resubscriptions + the messages users type as part of the resub.
277+
278+
Attributes
279+
-----------
280+
user: :class:`twitchio.PartialUser`
281+
The user who subscribed
282+
broadcaster: :class:`twitchio.PartialUser`
283+
The channel that was subscribed to
284+
tier: :class:`int`
285+
The tier of the subscription
286+
message: :class:`str`
287+
The user's resubscription message
288+
emote_data: :class:`list`
289+
emote data within the user's resubscription message. Not the emotes themselves
290+
cumulative_months: :class:`int`
291+
The total number of months a user has subscribed to the channel
292+
streak: Optional[:class:`int`]
293+
The total number of months subscribed in a row. ``None`` if the user declines to share it.
294+
duration: :class:`int`
295+
The length of the subscription. Typically 1, but some users may buy subscriptions for several months.
296+
"""
297+
298+
__slots__ = "user", "broadcaster", "tier", "message", "emote_data", "cumulative", "streak", "duration"
299+
300+
def __init__(self, client: EventSubClient, data: dict):
301+
self.user = _transform_user(client, data, "user")
302+
self.broadcaster = _transform_user(client, data, "broadcaster_user")
303+
self.tier = int(data["tier"])
304+
self.message: str = data["message"]["text"]
305+
self.emote_data: List[Dict] = data["message"].get("emotes", [])
306+
self.cumulative_months: int = data["cumulative_months"]
307+
self.streak: Optional[int] = data["streak_months"]
308+
self.duration: int = data["duration_months"]
309+
310+
240311
class ChannelCheerData(EventData):
241312
"""
242313
A Cheer event
@@ -1092,6 +1163,8 @@ def __init__(self, client: EventSubClient, data: dict):
10921163
ChannelBanData,
10931164
ChannelUnbanData,
10941165
ChannelSubscribeData,
1166+
ChannelSubscriptionGiftData,
1167+
ChannelSubscriptionMessageData,
10951168
ChannelCheerData,
10961169
ChannelUpdateData,
10971170
ChannelFollowData,
@@ -1126,6 +1199,8 @@ class _SubscriptionTypes(metaclass=_SubTypesMeta):
11261199

11271200
follow = "channel.follow", 1, ChannelFollowData
11281201
subscription = "channel.subscribe", 1, ChannelSubscribeData
1202+
subscription_gift = "channel.subscription.gift", 1, ChannelSubscriptionGiftData
1203+
subscription_message = "channel.subscription.message", 1, ChannelSubscriptionMessageData
11291204
cheer = "channel.cheer", 1, ChannelCheerData
11301205
raid = "channel.raid", 1, ChannelRaidData
11311206
ban = "channel.ban", 1, ChannelBanData

twitchio/ext/eventsub/server.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def stop(self):
4646
async def delete_subscription(self, subscription_id: str):
4747
await self._http.delete_subscription(subscription_id)
4848

49+
async def delete_all_active_subscriptions(self):
50+
# A convenience method
51+
active_subscriptions = await self.get_subscriptions("enabled")
52+
for subscription_id in active_subscriptions:
53+
await self.delete_subscription(subscription_id)
54+
4955
async def get_subscriptions(self, status: str = None):
5056
# All possible statuses are:
5157
#
@@ -114,6 +120,12 @@ def subscribe_channel_unbans(self, broadcaster: Union[PartialUser, str, int]):
114120
def subscribe_channel_subscriptions(self, broadcaster: Union[PartialUser, str, int]):
115121
return self._subscribe_with_broadcaster(models.SubscriptionTypes.subscription, broadcaster)
116122

123+
def subscribe_channel_subscription_gifts(self, broadcaster: Union[PartialUser, str, int]):
124+
return self._subscribe_with_broadcaster(models.SubscriptionTypes.subscription_gift, broadcaster)
125+
126+
def subscribe_channel_subscription_messages(self, broadcaster: Union[PartialUser, str, int]):
127+
return self._subscribe_with_broadcaster(models.SubscriptionTypes.subscription_message, broadcaster)
128+
117129
def subscribe_channel_cheers(self, broadcaster: Union[PartialUser, str, int]):
118130
return self._subscribe_with_broadcaster(models.SubscriptionTypes.cheer, broadcaster)
119131

0 commit comments

Comments
 (0)