Skip to content

Commit

Permalink
feat: Detect being timeouted by moderator when sending a chat
Browse files Browse the repository at this point in the history
  • Loading branch information
uetchy committed May 26, 2022
1 parent c64ebb6 commit 0c0ba71
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Don't overwrite `isLive` unless it is undefined
- Reintroduce timeout drift adjustment
- Treat `ERR_REQUEST_ABORTED` as aborted
- Detect being timeouted by moderator when sending a chat

## v0.15.0

Expand Down
11 changes: 11 additions & 0 deletions src/interfaces/yt/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ export interface YTActionResponse {
responseContext: YTResponseContext;
success: boolean;
actions: YTAction[];
timeoutDurationUsec?: string;
errorMessage?: YTLiveChatTextActionsErrorMessageRenderer;
}

export interface YTLiveChatTextActionsErrorMessageRenderer {
errorText: YTRunContainer;
editMessageText: YTRunContainer;
clickToDismissText: YTRunContainer;
originalRichMessage: {
textSegments: YTTextRun[];
};
}

// Interfaces
Expand Down
8 changes: 8 additions & 0 deletions src/masterchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,14 @@ export class Masterchat extends EventEmitter {
body
);

if (res.timeoutDurationUsec) {
// You are timeouted
const timeoutSec = usecToSeconds(res.timeoutDurationUsec);
throw new Error(
`You have been placed in timeout for ${timeoutSec} seconds`
);
}

const item = res.actions?.[0].addChatItemAction?.item;
if (!(item && "liveChatTextMessageRenderer" in item)) {
throw new Error(`Invalid response: ` + item);
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export function tsToNumber(timestampUsec: string): number {
return Number(BigInt(timestampUsec) / BigInt(1000));
}

export function usecToSeconds(usec: string): number {
return Number(BigInt(usec) / BigInt(1000 * 2));
}

export function formatColor(color: Color, format: ColorFormat = "hex"): string {
switch (format) {
case "rgb":
Expand Down

0 comments on commit 0c0ba71

Please sign in to comment.