mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-06 15:15:37 +01:00
Add listen_channel_chat_message
This commit is contained in:
@@ -4,7 +4,7 @@ from typing import NoReturn, Literal
|
|||||||
|
|
||||||
from twitchAPI.eventsub.webhook import EventSubWebhook
|
from twitchAPI.eventsub.webhook import EventSubWebhook
|
||||||
from twitchAPI.twitch import Twitch
|
from twitchAPI.twitch import Twitch
|
||||||
from twitchAPI.object.eventsub import StreamOnlineEvent, ChannelUpdateEvent
|
from twitchAPI.object.eventsub import StreamOnlineEvent, ChannelUpdateEvent, ChannelChatMessageEvent
|
||||||
from twitchAPI.oauth import validate_token
|
from twitchAPI.oauth import validate_token
|
||||||
|
|
||||||
from core.config import config
|
from core.config import config
|
||||||
@@ -41,33 +41,48 @@ class TwitchService:
|
|||||||
EventType.STREAM_ONLINE,
|
EventType.STREAM_ONLINE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def on_message(self, event: ChannelChatMessageEvent):
|
||||||
|
print(event)
|
||||||
|
|
||||||
async def subscribe_with_retry(
|
async def subscribe_with_retry(
|
||||||
self,
|
self,
|
||||||
method: Literal["listen_channel_update_v2"] | Literal["listen_stream_online"],
|
method: Literal["listen_channel_update_v2"]
|
||||||
|
| Literal["listen_stream_online"]
|
||||||
|
| Literal["listen_channel_chat_message"],
|
||||||
eventsub: EventSubWebhook,
|
eventsub: EventSubWebhook,
|
||||||
streamer: StreamerConfig,
|
streamer: StreamerConfig,
|
||||||
retry: int = 10
|
retry: int = 10
|
||||||
):
|
):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if method == "listen_channel_update_v2":
|
match method:
|
||||||
await eventsub.listen_channel_update_v2(str(streamer.twitch.id), self.on_channel_update)
|
case "listen_channel_update_v2":
|
||||||
elif method == "listen_stream_online":
|
await eventsub.listen_channel_update_v2(str(streamer.twitch.id), self.on_channel_update)
|
||||||
await eventsub.listen_stream_online(str(streamer.twitch.id), self.on_stream_online)
|
case "listen_stream_online":
|
||||||
else:
|
await eventsub.listen_stream_online(str(streamer.twitch.id), self.on_stream_online)
|
||||||
raise ValueError("Unknown method")
|
case "listen_channel_chat_message":
|
||||||
|
await eventsub.listen_channel_chat_message(
|
||||||
|
str(streamer.twitch.id),
|
||||||
|
str(config.TWITCH_ADMIN_USER_ID),
|
||||||
|
self.on_message
|
||||||
|
)
|
||||||
|
case _:
|
||||||
|
raise ValueError("Unknown method")
|
||||||
|
|
||||||
return
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if retry <= 0:
|
if retry <= 0:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
if method == "listen_channel_update_v2":
|
match method:
|
||||||
sub_type = "channel.update"
|
case "listen_channel_update_v2":
|
||||||
elif method == "listen_stream_online":
|
sub_type = "channel.update"
|
||||||
sub_type = "stream.online"
|
case "listen_stream_online":
|
||||||
else:
|
sub_type = "stream.online"
|
||||||
raise ValueError("Unknown method")
|
case "listen_channel_chat_message":
|
||||||
|
sub_type = "channel.chat.message"
|
||||||
|
case _:
|
||||||
|
raise ValueError("Unknown method")
|
||||||
|
|
||||||
subs = await self.twitch.get_eventsub_subscriptions(
|
subs = await self.twitch.get_eventsub_subscriptions(
|
||||||
user_id=str(streamer.twitch.id)
|
user_id=str(streamer.twitch.id)
|
||||||
@@ -84,7 +99,8 @@ class TwitchService:
|
|||||||
logger.info(f"Subscribe to events for {streamer.twitch.name}")
|
logger.info(f"Subscribe to events for {streamer.twitch.name}")
|
||||||
await gather(
|
await gather(
|
||||||
self.subscribe_with_retry("listen_channel_update_v2", eventsub, streamer),
|
self.subscribe_with_retry("listen_channel_update_v2", eventsub, streamer),
|
||||||
self.subscribe_with_retry("listen_stream_online", eventsub, streamer)
|
self.subscribe_with_retry("listen_stream_online", eventsub, streamer),
|
||||||
|
self.subscribe_with_retry("listen_channel_chat_message", eventsub, streamer),
|
||||||
)
|
)
|
||||||
logger.info(f"Subscribe to events for {streamer.twitch.name} done")
|
logger.info(f"Subscribe to events for {streamer.twitch.name} done")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user