Skip to content

Commit 14271d2

Browse files
committed
fetch_cheermotes and pubsub bits corrections
fetch_cheermotes color attribute corrected PubSubBitsMessage, PubSubBitsBadgeMessage now all return correct data and models Added some Optional typings for user friendliness
1 parent 397ed62 commit 14271d2

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

docs/changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ Massive documentation updates
99
- Fix bug where # prefixed channel names in initial_channels would not trigger :func:`twitchio.Client.event_ready`
1010
- :func:`User.create_clip` has been fixed by converting bool to string in http request
1111
- Poll endpoints added :func:`User.fetch_polls` :func:`User.create_poll` and :func:`User.end_poll`
12+
- :func:`Client.fetch_cheermotes` color attribute corrected
1213

1314
- ext.commands
1415
- :func:`Bot.handle_commands` now also invokes on threads / replies
1516
- Cooldowns are now handled correctly per bucket.
1617
- Fix issue with :func:`Bot.reload_module` where module is reloaded incorrectly if exception occurs
1718

1819
- ext.pubsub
19-
- Channel subscription model fixes.
20+
- Channel subscription model fixes and additional type hints for Optional return values
21+
- PubSubBitsMessage model updated to return correct data and updated typing
22+
- PubSubBitsBadgeMessage model updated to return correct data and updated typing
23+
- PubSubChatMessage now correctly returns a string rather than int for the Bits Events
2024

2125
2.2.0
2226
=====

twitchio/ext/pubsub/models.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class PubSubChatMessage:
7979

8080
def __init__(self, content: str, id: str, type: str):
8181
self.content = content
82-
self.id = int(id)
82+
self.id = id
8383
self.type = type
8484

8585

@@ -133,8 +133,8 @@ class PubSubBitsMessage(PubSubMessage):
133133
The amount of bits used.
134134
channel_id: :class:`int`
135135
The channel the bits were given to.
136-
user: :class:`twitchio.PartialUser`
137-
The user giving the bits.
136+
user: Optional[:class:`twitchio.PartialUser`]
137+
The user giving the bits. Can be None if anonymous.
138138
version: :class:`str`
139139
The event version.
140140
"""
@@ -144,16 +144,21 @@ class PubSubBitsMessage(PubSubMessage):
144144
def __init__(self, client: Client, topic: str, data: dict):
145145
super().__init__(client, topic, data)
146146

147-
self.message = PubSubChatMessage(data["chat_message"], data["message_id"], data["message_type"])
147+
data = data["message"]
148+
self.message = PubSubChatMessage(data["data"]["chat_message"], data["message_id"], data["message_type"])
148149
self.badge_entitlement = (
149-
PubSubBadgeEntitlement(data["badge_entitlement"]["new_version"], data["badge_entitlement"]["old_version"])
150-
if data["badge_entitlement"]
150+
PubSubBadgeEntitlement(
151+
data["data"]["badge_entitlement"]["new_version"], data["data"]["badge_entitlement"]["old_version"]
152+
)
153+
if data["data"]["badge_entitlement"]
151154
else None
152155
)
153-
self.bits_used: int = data["bits_used"]
154-
self.channel_id: int = int(data["channel_id"])
156+
self.bits_used: int = data["data"]["bits_used"]
157+
self.channel_id: int = int(data["data"]["channel_id"])
155158
self.user = (
156-
PartialUser(client._http, data["user_id"], data["user_name"]) if data["user_id"] is not None else None
159+
PartialUser(client._http, data["data"]["user_id"], data["data"]["user_name"])
160+
if data["data"]["user_id"]
161+
else None
157162
)
158163
self.version: str = data["version"]
159164

@@ -180,6 +185,7 @@ class PubSubBitsBadgeMessage(PubSubMessage):
180185

181186
def __init__(self, client: Client, topic: str, data: dict):
182187
super().__init__(client, topic, data)
188+
data = data["message"]
183189
self.user = PartialUser(client._http, data["user_id"], data["user_name"])
184190
self.channel: Channel = client.get_channel(data["channel_name"]) or Channel(
185191
name=data["channel_name"], websocket=client._connection
@@ -349,15 +355,15 @@ class PubSubChannelSubscribe(PubSubMessage):
349355
Channel that has been subscribed or subgifted.
350356
context: :class:`str`
351357
Event type associated with the subscription product.
352-
user: :class:`twitchio.PartialUser`
353-
The person who subscribed or sent a gift subscription.
358+
user: Optional[:class:`twitchio.PartialUser`]
359+
The person who subscribed or sent a gift subscription. Can be None if anonymous.
354360
message: :class:`str`
355361
Message sent with the sub/resub.
356-
emotes: List[:class:`dict`]
362+
emotes: Optional[List[:class:`dict`]]
357363
Message sent with the sub/resub.
358364
is_gift: :class:`bool`
359365
If this sub message was caused by a gift subscription.
360-
recipient: :class:`twitchio.PartialUser`
366+
recipient: Optional[:class:`twitchio.PartialUser`]
361367
The person the who received the gift subscription.
362368
sub_plan: :class:`str`
363369
Subscription Plan ID.
@@ -367,9 +373,9 @@ class PubSubChannelSubscribe(PubSubMessage):
367373
Time when the subscription or gift was completed. RFC 3339 format.
368374
cumulative_months: :class:`int`
369375
Cumulative number of tenure months of the subscription.
370-
streak_months: :class:`int`
376+
streak_months: Optional[:class:`int`]
371377
Denotes the user's most recent (and contiguous) subscription tenure streak in the channel.
372-
multi_month_duration: :class:`int`
378+
multi_month_duration: Optional[:class:`int`]
373379
Number of months gifted as part of a single, multi-month gift OR number of months purchased as part of a multi-month subscription.
374380
"""
375381

@@ -398,21 +404,18 @@ def __init__(self, client: Client, topic: str, data: dict):
398404
name=subscription["channel_name"], websocket=client._connection
399405
)
400406
self.context: str = subscription["context"]
401-
402407
try:
403408
self.user = PartialUser(client._http, int(subscription["user_id"]), subscription["user_name"])
404409
except KeyError:
405410
self.user = None
406411

407412
self.message: str = subscription["sub_message"]["message"]
408-
409413
try:
410414
self.emotes = subscription["sub_message"]["emotes"]
411415
except KeyError:
412416
self.emotes = None
413417

414418
self.is_gift: bool = subscription["is_gift"]
415-
416419
try:
417420
self.recipient = PartialUser(
418421
client._http, int(subscription["recipient_id"]), subscription["recipient_user_name"]
@@ -423,9 +426,18 @@ def __init__(self, client: Client, topic: str, data: dict):
423426
self.sub_plan: str = subscription["sub_plan"]
424427
self.sub_plan_name: str = subscription["sub_plan_name"]
425428
self.time = parse_timestamp(subscription["time"])
426-
self.cumulative_months: int = int(subscription["cumulative_months"])
427-
self.streak_months = int(subscription["streak_months"])
428-
self.multi_month_duration = int(subscription["multi_month_duration"])
429+
try:
430+
self.cumulative_months = int(subscription["cumulative_months"])
431+
except KeyError:
432+
self.cumulative_months = None
433+
try:
434+
self.streak_months = int(subscription["streak_months"])
435+
except KeyError:
436+
self.streak_months = None
437+
try:
438+
self.multi_month_duration = int(subscription["multi_month_duration"])
439+
except KeyError:
440+
self.multi_month_duration = None
429441

430442

431443
class PubSubModerationActionModeratorAdd(PubSubMessage):

twitchio/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ class CheerEmoteTier:
121121
Indicates whether twitch hides the emote from the bits card.
122122
"""
123123

124-
__slots__ = "min_bits", "id", "colour", "images", "can_cheer", "show_in_bits_card"
124+
__slots__ = "min_bits", "id", "color", "images", "can_cheer", "show_in_bits_card"
125125

126126
def __init__(self, data: dict):
127127
self.min_bits: int = data["min_bits"]
128128
self.id: str = data["id"]
129-
self.colour: str = data["colour"]
129+
self.color: str = data["color"]
130130
self.images = data["images"] # TODO types
131131
self.can_cheer: bool = data["can_cheer"]
132132
self.show_in_bits_card: bool = data["show_in_bits_card"]

0 commit comments

Comments
 (0)