Skip to content

Commit ffde7e3

Browse files
committed
feat: add is_same_room in user badge; add random id
1 parent 8f5a0d5 commit ffde7e3

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blive-message-listener",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"description": "Bilibili-live danmu listener with type",
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { listenAll, type MsgHandler } from './listener'
55
export const startListen = (roomId: number, handler: MsgHandler) => {
66
const live = new KeepLiveTCP(roomId)
77

8-
listenAll(live, handler)
8+
listenAll(live, roomId, handler)
99
}
1010

1111
export type { MsgHandler }

src/listener/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export type MsgHandler = Partial<
2020

2121
const normalizeDanmu = <T>(msgType: string, body: T): Message<T> => {
2222
const timestamp = Date.now()
23+
const randomText = Math.floor(Math.random() * 100).toString()
2324
// @ts-ignore
24-
const id = `${timestamp}_${msgType}_${body.user?.uid}`
25+
const id = `${timestamp}_${msgType}_${body.user?.uid}_${randomText}`
2526
return {
2627
id,
2728
timestamp,
@@ -30,7 +31,7 @@ const normalizeDanmu = <T>(msgType: string, body: T): Message<T> => {
3031
}
3132
}
3233

33-
export const listenAll = (instance: KeepLiveTCP, handler?: MsgHandler) => {
34+
export const listenAll = (instance: KeepLiveTCP, roomId: number, handler?: MsgHandler) => {
3435
if (!handler) return
3536

3637
// HEARTBEAT
@@ -44,7 +45,7 @@ export const listenAll = (instance: KeepLiveTCP, handler?: MsgHandler) => {
4445
// DANMU_MSG
4546
if (handler[DANMU_MSG.handlerName]) {
4647
instance.on(DANMU_MSG.eventName, (data: any) => {
47-
const parsedData = DANMU_MSG.parser(data)
48+
const parsedData = DANMU_MSG.parser(data, roomId)
4849
handler[DANMU_MSG.handlerName]?.(normalizeDanmu(DANMU_MSG.eventName, parsedData))
4950
})
5051
}
@@ -68,7 +69,7 @@ export const listenAll = (instance: KeepLiveTCP, handler?: MsgHandler) => {
6869
// SUPER_CHAT_MESSAGE
6970
if (handler[SUPER_CHAT_MESSAGE.handlerName]) {
7071
instance.on(SUPER_CHAT_MESSAGE.eventName, (data: any) => {
71-
const parsedData = SUPER_CHAT_MESSAGE.parser(data)
72+
const parsedData = SUPER_CHAT_MESSAGE.parser(data, roomId)
7273
handler[SUPER_CHAT_MESSAGE.handlerName]?.(normalizeDanmu(SUPER_CHAT_MESSAGE.eventName, parsedData))
7374
})
7475
}

src/parser/DANMU_MSG.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface DanmuMsg {
1313
}
1414
}
1515

16-
const parser = (data: any): DanmuMsg => {
16+
const parser = (data: any, roomId: number): DanmuMsg => {
1717
const content = data.info[1]
1818
const username = data.info[2][1]
1919
const badge: DanmuMsg['user']['badge'] = data.info[3].length ? {
@@ -25,6 +25,7 @@ const parser = (data: any): DanmuMsg => {
2525
uid: data.info[3][12],
2626
uname: data.info[3][2],
2727
room_id: data.info[3][3],
28+
is_same_room: data.info[3][3] === roomId,
2829
},
2930
} : undefined
3031
return {

src/parser/SUPER_CHAT_MESSAGE.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface SuperChatMsg {
1212
time: number
1313
}
1414

15-
const parser = (data: any): SuperChatMsg => {
15+
const parser = (data: any, roomId: number): SuperChatMsg => {
1616
const rawData = data.data
1717
const { medal_info, user_info } = data.data
1818
return {
@@ -28,6 +28,7 @@ const parser = (data: any): SuperChatMsg => {
2828
uid: medal_info.target_id,
2929
uname: medal_info.anchor_uname,
3030
room_id: medal_info.anchor_roomid,
31+
is_same_room: medal_info.anchor_roomid === roomId,
3132
},
3233
} : undefined,
3334
identity: {

src/types/app.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ export interface User {
1717
color: string
1818
/** 主播信息 */
1919
anchor: {
20+
/** 主播uid */
2021
uid: number
22+
/** 主播用户名 */
2123
uname: string
24+
/** 主播房间号 */
2225
room_id: number
26+
/** 是否为本直播间 */
27+
is_same_room?: boolean
2328
}
2429
}
2530
/** 用户身份 */

0 commit comments

Comments
 (0)