Skip to content

Commit

Permalink
feat: add msg type INTERACT_WORD
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Sep 24, 2022
1 parent e8dca52 commit 5863789
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "blive-message-listener",
"version": "0.1.9",
"version": "0.1.10",
"description": "Bilibili-live danmu listener with type",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -9,5 +9,5 @@ export const startListen = (roomId: number, handler: MsgHandler) => {
}

export type { MsgHandler }
export type { Message, GuardLevel } from './types/app'
export type { Message, GuardLevel, User } from './types/app'
export * from './types/message'
10 changes: 10 additions & 0 deletions src/listener/index.ts
Expand Up @@ -2,6 +2,7 @@ import {
HEARTBEAT, type AttentionChangeMsgHandler,
DANMU_MSG, type DanmuMsgHandler,
GUARD_BUY, type GuardBuyHandler,
INTERACT_WORD, type NewComerMsgHandler,
SEND_GIFT, type GiftHandler,
SUPER_CHAT_MESSAGE, type SuperChatHandler,
WATCHED_CHANGE, type WatchedChangeHandler,
Expand All @@ -13,6 +14,7 @@ export type MsgHandler = Partial<
& AttentionChangeMsgHandler
& DanmuMsgHandler
& GuardBuyHandler
& NewComerMsgHandler
& GiftHandler
& SuperChatHandler
& WatchedChangeHandler
Expand Down Expand Up @@ -58,6 +60,14 @@ export const listenAll = (instance: KeepLiveTCP, roomId: number, handler?: MsgHa
})
}

// INTERACT_WORD
if (handler[INTERACT_WORD.handlerName]) {
instance.on(INTERACT_WORD.eventName, (data: any) => {
const parsedData = INTERACT_WORD.parser(data, roomId)
handler[INTERACT_WORD.handlerName]?.(normalizeDanmu(INTERACT_WORD.eventName, parsedData))
})
}

// SEND_GIFT
if (handler[SEND_GIFT.handlerName]) {
instance.on(SEND_GIFT.eventName, (data: any) => {
Expand Down
41 changes: 41 additions & 0 deletions src/parser/INTERACT_WORD.ts
@@ -0,0 +1,41 @@
import { intToColorHex } from '../utils/color'
import type { Message, User } from '../types/app'

export interface NewComerMsg {
user: User
/** 入场时间·*/
timestamp: number
}

const parser = (data: any, roomId: number): NewComerMsg => {
const rawData = data.data
return {
user: {
uid: rawData.uid,
uname: rawData.username,
badge: rawData.fans_medal?.target_id ? {
active: rawData.fans_medal?.is_lighted,
name: rawData.fans_medal?.medal_name,
level: rawData.fans_medal?.medal_level,
color: intToColorHex(rawData.fans_medal?.medal_color_start),
anchor: {
uid: rawData.fans_medal?.target_id,
uname: '',
room_id: rawData.fans_medal?.anchor_roomid,
is_same_room: rawData.fans_medal?.anchor_roomid === roomId,
}
} : undefined,
},
timestamp: rawData.timestamp,
}
}

export const INTERACT_WORD = {
parser,
eventName: 'INTERACT_WORD' as const,
handlerName: 'onNewComer' as const,
}

export type Handler = {
onNewComer: (msg: Message<NewComerMsg>) => void
}
1 change: 1 addition & 0 deletions src/parser/index.ts
@@ -1,6 +1,7 @@
export { HEARTBEAT, Handler as AttentionChangeMsgHandler, AttentionChangeMsg } from './HEARTBEAT'
export { DANMU_MSG, Handler as DanmuMsgHandler, DanmuMsg } from './DANMU_MSG'
export { GUARD_BUY, Handler as GuardBuyHandler, GuardBuyMsg } from './GUARD_BUY'
export { INTERACT_WORD, Handler as NewComerMsgHandler, NewComerMsg } from './INTERACT_WORD'
export { SEND_GIFT, Handler as GiftHandler, GiftMsg } from './SEND_GIFT'
export { SUPER_CHAT_MESSAGE, Handler as SuperChatHandler, SuperChatMsg } from './SUPER_CHAT_MESSAGE'
export { WATCHED_CHANGE, Handler as WatchedChangeHandler, WatchedChangeMsg } from './WATCHED_CHANGE'
Expand Down
1 change: 1 addition & 0 deletions src/types/message.ts
Expand Up @@ -2,6 +2,7 @@ export type {
AttentionChangeMsg,
DanmuMsg,
GuardBuyMsg,
NewComerMsg,
GiftMsg,
SuperChatMsg,
WatchedChangeMsg,
Expand Down

0 comments on commit 5863789

Please sign in to comment.