Skip to content

Commit dd13832

Browse files
author
Eviee Py
committed
Remove channel. from EventSub dispatched events
1 parent 21918ce commit dd13832

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

twitchio/eventsub/websockets.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
MISSING,
4949
_from_json, # type: ignore
5050
)
51+
from .subscriptions import _SUB_MAPPING
5152

5253

5354
if TYPE_CHECKING:
@@ -408,11 +409,11 @@ async def _process_revocation(self, data: RevocationMessage) -> None:
408409
self._subscriptions.pop(payload.id, None)
409410

410411
async def _process_notification(self, data: NotificationMessage) -> None:
411-
subscription_type = data["metadata"]["subscription_type"]
412-
event: str = subscription_type.replace("channel.channel_", "channel.").replace(".", "_")
412+
sub_type = data["metadata"]["subscription_type"]
413+
event = _SUB_MAPPING.get(sub_type, sub_type.removeprefix("channel."))
413414

414415
try:
415-
payload_class = create_event_instance(subscription_type, data["payload"]["event"], http=self._http)
416+
payload_class = create_event_instance(sub_type, data["payload"]["event"], http=self._http)
416417
except ValueError:
417418
logger.warning("Websocket '%s' received an unhandled eventsub event: '%s'.", self, event)
418419
return

twitchio/web/aio_adapter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from aiohttp import web
3535

3636
from ..authentication import Scopes
37+
from ..eventsub.subscriptions import _SUB_MAPPING
3738
from ..models.eventsub_ import SubscriptionRevoked, create_event_instance
3839
from ..types_.eventsub import EventSubHeaders
3940
from ..utils import _from_json, parse_timestamp # type: ignore
@@ -176,11 +177,11 @@ async def eventsub_callback(self, request: web.Request) -> web.Response:
176177
return web.Response(text=data["challenge"], status=200, headers={"Content-Type": "text/plain"})
177178

178179
elif msg_type == "notification":
179-
subscription_type: str = data["subscription"]["type"]
180-
event: str = subscription_type.replace("channel.channel_", "channel.").replace(".", "_")
180+
sub_type: str = data["subscription"]["type"]
181+
event = _SUB_MAPPING.get(sub_type, sub_type.removeprefix("channel."))
181182

182183
try:
183-
payload_class = create_event_instance(subscription_type, data["event"], http=self.client._http)
184+
payload_class = create_event_instance(sub_type, data["event"], http=self.client._http)
184185
except ValueError:
185186
logger.warning("Webhook '%s' received an unhandled eventsub event: '%s'.", self, event)
186187
return web.Response(status=200)

twitchio/web/starlette_adapter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from starlette.routing import Route
3838

3939
from ..authentication import Scopes
40+
from ..eventsub.subscriptions import _SUB_MAPPING
4041
from ..models.eventsub_ import SubscriptionRevoked, create_event_instance
4142
from ..types_.eventsub import EventSubHeaders
4243
from ..utils import _from_json, parse_timestamp # type: ignore
@@ -189,11 +190,11 @@ async def eventsub_callback(self, request: Request) -> Response:
189190
return Response(data["challenge"], status_code=200, headers={"Content-Type": "text/plain"})
190191

191192
elif msg_type == "notification":
192-
subscription_type: str = data["subscription"]["type"]
193-
event: str = subscription_type.replace("channel.channel_", "channel.").replace(".", "_")
193+
sub_type: str = data["subscription"]["type"]
194+
event = _SUB_MAPPING.get(sub_type, sub_type.removeprefix("channel."))
194195

195196
try:
196-
payload_class = create_event_instance(subscription_type, data["event"], http=self.client._http)
197+
payload_class = create_event_instance(sub_type, data["event"], http=self.client._http)
197198
except ValueError:
198199
logger.warning("Webhook '%s' received an unhandled eventsub event: '%s'.", self, event)
199200
return Response(status_code=200)

0 commit comments

Comments
 (0)