Skip to content

Commit

Permalink
Added emotes, twitch emotes to come
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaserlang committed Oct 30, 2024
1 parent f888783 commit 6c8168f
Show file tree
Hide file tree
Showing 8 changed files with 779 additions and 95 deletions.
777 changes: 699 additions & 78 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"webpack-cli": "^4.8.0"
},
"dependencies": {
"@mkody/twitch-emoticons": "^2.8.9",
"axios": "^0.21.3",
"downshift": "^6.1.7",
"moment": "^2.29.1",
Expand All @@ -42,6 +43,7 @@
"react-dom": "^17.0.2",
"react-router-config": "^5.1.1",
"react-router-dom": "^5.3.0",
"react-use-websocket": "^3.0.0"
"react-use-websocket": "^3.0.0",
"sanitize-html": "^2.13.1"
}
}
15 changes: 13 additions & 2 deletions tbot/twitch_bot/tasks/chatlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async def message(nick, target, message, **kwargs):
kwargs['id'],
kwargs['display-name'],
kwargs['color'],
kwargs['emotes'],
)
)

Expand All @@ -33,7 +34,16 @@ async def message(nick, target, message, **kwargs):


async def save(
type_, channel, channel_id, user, user_id, message, msg_id, display_name, user_color
type_,
channel,
channel_id,
user,
user_id,
message,
msg_id,
display_name,
user_color,
emotes,
):
try:
now = datetime.utcnow()
Expand Down Expand Up @@ -116,7 +126,8 @@ async def save(
'user': display_name,
'message': message,
'created_at': datetime.now(tz=timezone.utc).isoformat(),
'color': user_color,
'user_color': user_color,
'emotes': emotes,
},
)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion tbot/twitch_bot/tasks/youtube_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def parse_chatmessages(channel_id: str, live_chat_id: str, chat: dict):
'user': m['authorDetails']['displayName'],
'message': m['snippet']['displayMessage'],
'created_at': m['snippet']['publishedAt'],
'color': '',
'user_color': '',
},
)

Expand Down
6 changes: 4 additions & 2 deletions tbot/web/handlers/api/twitch/chatlog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from tornado import web
from ..base import Api_handler
from dateutil.parser import parse
from tornado import web

from tbot import utils

from ..base import Api_handler


class Handler(Api_handler):

Expand Down
27 changes: 17 additions & 10 deletions tbot/web/ui/live_chat/components/chat.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import React, { useState, useEffect, useRef } from "react";
import useWebSocket, { ReadyState } from "react-use-websocket";
import { useParseEmotes } from "./parse_emotes";
import useWebSocket from "react-use-websocket";
import sanitizeHtml from "sanitize-html";
import "./chat.scss";

export function Chat({ channelId }) {
const {
sendMessage,
sendJsonMessage,
lastMessage,
lastJsonMessage,
readyState,
getWebSocket,
} = useWebSocket(`/api/live-chat/${channelId}`, {
const { lastJsonMessage } = useWebSocket(`/api/live-chat/${channelId}`, {
onOpen: () => console.log("opened"),
shouldReconnect: (closeEvent) => true,
});
const [messageHistory, setMessageHistory] = useState([]);
const messagesEndRef = useRef(null);
const { parseEmoteMessage } = useParseEmotes({ channelId });

useEffect(() => {
if (lastJsonMessage !== null) {
Expand All @@ -41,7 +37,18 @@ export function Chat({ channelId }) {
<span className="username" style={{ color: msg.color }}>
{msg.user}
</span>
: <span className="text">{msg.message}</span>
:{" "}
<span
className="text"
dangerouslySetInnerHTML={{
__html: sanitizeHtml(parseEmoteMessage(msg.message), {
allowedTags: ["img"],
allowedAttributes: {
img: ["src", "title", "class"],
},
}),
}}
></span>
</div>
))}
<div ref={messagesEndRef} />
Expand Down
2 changes: 1 addition & 1 deletion tbot/web/ui/live_chat/components/chat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
.message {
padding: 2px 0;
display: flex;
align-items: baseline;
align-items: center;
}

.username {
Expand Down
41 changes: 41 additions & 0 deletions tbot/web/ui/live_chat/components/parse_emotes.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState, useEffect, useCallback } from "react";
import { EmoteFetcher, EmoteParser } from "@mkody/twitch-emoticons";

const fetcher = new EmoteFetcher();
const parser = new EmoteParser(fetcher, {
template: '<img class="emote" alt="{name}" src="{link}">',
match: /(\w+)+?/g,
});

export function useParseEmotes({ channelId }) {
const [loading, setLoading] = useState(true);
const [loadEmotes, setLoadEmotes] = useState(1);

useEffect(() => {
Promise.all([
fetcher.fetchBTTVEmotes(),
fetcher.fetchBTTVEmotes(channelId),
fetcher.fetchSevenTVEmotes(),
fetcher.fetchSevenTVEmotes(channelId),
fetcher.fetchFFZEmotes(),
fetcher.fetchFFZEmotes(channelId),
])
.then(() => {
setLoading(false);
})
.catch((err) => {
console.error("Error loading emotes...");
console.error(err);
setLoadEmotes((prev) => prev + 1);
});
}, [loadEmotes]);

const parseEmoteMessage = useCallback((message) => {
if (!loading) {
return message;
}
return parser.parse(message);
}, []);

return { parseEmoteMessage, loading };
}

0 comments on commit 6c8168f

Please sign in to comment.