Skip to content

Commit

Permalink
fix(twitch): send bot messages to simple chat in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Jan 17, 2024
1 parent 67b6c10 commit 58037ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/overlays/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import { ioServer } from '~/helpers/panel.js';
import { parseTextWithEmotes } from '~/helpers/parseTextWithEmotes.js';
import { adminEndpoint } from '~/helpers/socket.js';

export const getBadgeImagesFromBadgeSet = (badgesMap: Map<string, string>) => {
const badgeImages: {url: string }[] = [];
for (const messageBadgeId of badgesMap.keys()) {
const badge = badgesCache.find(o => o.id === messageBadgeId);
if (badge) {
const badgeImage = badge.getVersion(badgesMap.get(messageBadgeId) as string)?.getImageUrl(4);
if (badgeImage) {
badgeImages.push({ url: badgeImage });
}
}
}
return badgeImages;
};

class Chat extends Overlay {
showInUI = false;

Expand All @@ -23,24 +37,14 @@ class Chat extends Overlay {
if (!message.sender) {
return;
}
const badgeImages: {url: string }[] = [];
for (const messageBadgeId of message.sender.badges.keys()) {
const badge = badgesCache.find(o => o.id === messageBadgeId);
if (badge) {
const badgeImage = badge.getVersion(message.sender.badges.get(messageBadgeId) as string)?.getImageUrl(4);
if (badgeImage) {
badgeImages.push({ url: badgeImage });
}
}
}
ioServer?.of('/overlays/chat').emit('message', {
id: randomUUID(),
timestamp: message.timestamp,
displayName: message.sender.displayName.toLowerCase() === message.sender.userName ? message.sender.displayName : `${message.sender.displayName} (${message.sender.userName})`,
userName: message.sender.userName,
message: data,
show: false,
badges: badgeImages,
badges: getBadgeImagesFromBadgeSet(message.sender.badges),
color: message.sender.color,
service: 'twitch',
});
Expand Down
16 changes: 16 additions & 0 deletions src/services/twitch/chat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomUUID } from 'node:crypto';
import util from 'util';

import type { EmitData } from '@entity/overlay.js';
Expand Down Expand Up @@ -33,13 +34,15 @@ import { warning } from '~/helpers/log.js';
import {
chatIn, debug, error, info, resub, subcommunitygift, subgift, whisperIn,
} from '~/helpers/log.js';
import { ioServer } from '~/helpers/panel.js';
import { linesParsedIncrement, setStatus } from '~/helpers/parser.js';
import { tmiEmitter } from '~/helpers/tmi/index.js';
import * as changelog from '~/helpers/user/changelog.js';
import getNameById from '~/helpers/user/getNameById.js';
import { isOwner } from '~/helpers/user/index.js';
import { isBot, isBotId } from '~/helpers/user/isBot.js';
import { isIgnored, isIgnoredSafe } from '~/helpers/user/isIgnored.js';
import { getBadgeImagesFromBadgeSet } from '~/overlays/chat.js';
import eventlist from '~/overlays/eventlist.js';
import { Parser } from '~/parser.js';
import alerts from '~/registries/alerts.js';
Expand Down Expand Up @@ -353,6 +356,19 @@ class Chat {
client.onMessage(async (_channel, user, message, msg) => {
const userstate = msg.userInfo;
if (isBotId(userstate.userId)) {
// send to dashboard and ignore
ioServer?.of('/widgets/chat').emit('bot-message', {
id: randomUUID(),
timestamp: Date.now(),
displayName: msg.userInfo.displayName.toLowerCase() === msg.userInfo.userName ? msg.userInfo.displayName : `${msg.userInfo.displayName} (${msg.userInfo.userName})`,
userName: msg.userInfo.userName,
message: message,
show: false,
badges: getBadgeImagesFromBadgeSet(msg.userInfo.badges),
color: msg.userInfo.color,
service: 'twitch',
isBot: true,
});
return;
}

Expand Down

0 comments on commit 58037ce

Please sign in to comment.