From a5a8ec20eb2fa4adc9a1a85b61216222156d49db Mon Sep 17 00:00:00 2001 From: Thomas Erlang Date: Wed, 30 Oct 2024 23:09:01 +0100 Subject: [PATCH] Some emotes --- tbot/web/app.py | 1 + tbot/web/handlers/api/twitch/emotes.py | 19 ++++++++++++ tbot/web/ui/live_chat/components/chat.jsx | 3 +- .../ui/live_chat/components/parse_emotes.jsx | 30 +++++++++++++++++-- 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tbot/web/handlers/api/twitch/emotes.py diff --git a/tbot/web/app.py b/tbot/web/app.py index def8312..7ed3095 100644 --- a/tbot/web/app.py +++ b/tbot/web/app.py @@ -75,6 +75,7 @@ def App(): (r'/api/twitch/channels/([0-9]+)/check-extra-auth', handlers.api.twitch.check_extra_auth.Handler), (r'/api/twitch/channels/([0-9]+)/commercial', handlers.api.twitch.commercial.Handler), (r'/api/twitch/channels/([0-9]+)/self-subs', handlers.api.twitch.self_subs.Handler), + (r'/api/twitch/channels/([0-9]+)/emotes', handlers.api.twitch.emotes.EmotesHandler), (r'/api/live-chat/([0-9]+)', handlers.api.live_chat.LiveChatHandler), (r'/api/rtmp-auth', handlers.api.rtmp_auth.Handler), diff --git a/tbot/web/handlers/api/twitch/emotes.py b/tbot/web/handlers/api/twitch/emotes.py new file mode 100644 index 0000000..037eb20 --- /dev/null +++ b/tbot/web/handlers/api/twitch/emotes.py @@ -0,0 +1,19 @@ +from tbot.utils.twitch import twitch_request +from tbot.web.handlers.api.base import Api_handler + + +class EmotesHandler(Api_handler): + async def get(self, channel_id): + emotes = await twitch_request( + self.ahttp, url='https://api.twitch.tv/helix/chat/emotes/global' + ) + channel_emotes = await twitch_request( + self.ahttp, + url=f'https://api.twitch.tv/helix/chat/emotes?broadcaster_id={channel_id}', + ) + self.write_object( + { + 'global_emotes': emotes['data'], + 'channel_emotes': channel_emotes['data'], + } + ) diff --git a/tbot/web/ui/live_chat/components/chat.jsx b/tbot/web/ui/live_chat/components/chat.jsx index 2d9432d..3ccce5d 100644 --- a/tbot/web/ui/live_chat/components/chat.jsx +++ b/tbot/web/ui/live_chat/components/chat.jsx @@ -6,8 +6,7 @@ import "./chat.scss"; export function Chat({ channelId }) { const { lastJsonMessage } = useWebSocket(`/api/live-chat/${channelId}`, { - onOpen: () => console.log("opened"), - shouldReconnect: (closeEvent) => true, + shouldReconnect: () => true, }); const [messageHistory, setMessageHistory] = useState([]); const messagesEndRef = useRef(null); diff --git a/tbot/web/ui/live_chat/components/parse_emotes.jsx b/tbot/web/ui/live_chat/components/parse_emotes.jsx index 7abf7bb..cd7cec9 100644 --- a/tbot/web/ui/live_chat/components/parse_emotes.jsx +++ b/tbot/web/ui/live_chat/components/parse_emotes.jsx @@ -1,5 +1,11 @@ import { useState, useEffect, useCallback } from "react"; -import { EmoteFetcher, EmoteParser } from "@mkody/twitch-emoticons"; +import { + EmoteFetcher, + EmoteParser, + TwitchEmote, +} from "@mkody/twitch-emoticons"; + +import api from "tbot/twitch/api"; const fetcher = new EmoteFetcher(); const parser = new EmoteParser(fetcher, { @@ -12,7 +18,9 @@ export function useParseEmotes({ channelId }) { const [loadEmotes, setLoadEmotes] = useState(1); useEffect(() => { + if (loadEmotes > 5) return; Promise.all([ + api.get(`/api/twitch/channels/${channelId}/emotes`), fetcher.fetchBTTVEmotes(), fetcher.fetchBTTVEmotes(channelId), fetcher.fetchSevenTVEmotes(), @@ -20,13 +28,29 @@ export function useParseEmotes({ channelId }) { fetcher.fetchFFZEmotes(), fetcher.fetchFFZEmotes(channelId), ]) - .then(() => { + .then((data) => { + const emotes = []; + for (const emote of data[0].data.global_emotes) { + emote.formats = emote.format; + emote.code = emote.name; + emotes.push( + new TwitchEmote(fetcher.channels.get(channelId), emote.id, emote) + ); + } + for (const emote of data[0].data.channel_emotes) { + emote.formats = emote.format; + emote.code = emote.name; + emotes.push( + new TwitchEmote(fetcher.channels.get(channelId), emote.id, emote) + ); + } + fetcher.fromObject(emotes.map((emote) => emote.toObject())); setLoading(false); }) .catch((err) => { console.error("Error loading emotes..."); console.error(err); - setLoadEmotes((prev) => prev + 1); + setTimeout(() => setLoadEmotes((prev) => prev + 1), 500); }); }, [loadEmotes]);