Skip to content

Commit

Permalink
Merge pull request #28 from lbmaian/chatfix
Browse files Browse the repository at this point in the history
Fixed parsing missing authorName
  • Loading branch information
KentoNishi authored Dec 3, 2021
2 parents 3472561 + 9dc4c7b commit 01349ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/ts/chat-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ const parseAddChatItemAction = (action: Ytc.AddChatItemAction, isReplay = false,

const item: Ytc.ParsedMessage = {
author: {
name: renderer.authorName.simpleText,
// It's apparently possible for there to be no author name (and only an author photo).
name: renderer.authorName?.simpleText ?? '',
id: renderer.authorExternalChannelId,
types: authorTypes,
customBadge
Expand Down Expand Up @@ -223,12 +224,17 @@ export const parseChatResponse = (response: string, isReplay: boolean): Ytc.Pars
actionsArray.forEach((action) => {
let parsedAction: Ytc.ParsedAction | undefined;

if (action.replayChatItemAction) {
const replayAction = action.replayChatItemAction;
const replayTimeMs = parseInt(replayAction.videoOffsetTimeMsec);
parsedAction = processCommonAction(replayAction.actions[0], isReplay, replayTimeMs);
} else {
parsedAction = processLiveAction(action, isReplay, liveTimeoutMs);
try {
if (action.replayChatItemAction) {
const replayAction = action.replayChatItemAction;
const replayTimeMs = parseInt(replayAction.videoOffsetTimeMsec);
parsedAction = processCommonAction(replayAction.actions[0], isReplay, replayTimeMs);
} else {
parsedAction = processLiveAction(action, isReplay, liveTimeoutMs);
}
} catch (error) {
console.error('Failed to parse action:', action);
throw error;
}

if (!parsedAction) {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/typings/ytc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ declare namespace Ytc {

interface TextMessageRenderer {
message?: RunsObj;
authorName: SimpleTextObj;
authorName?: SimpleTextObj;
authorBadges?: Array<{
liveChatAuthorBadgeRenderer: {
/** Changes based on YT language */
Expand Down

0 comments on commit 01349ec

Please sign in to comment.