Skip to content

Commit 049bd46

Browse files
committed
feat: 用户点赞直播间消息
link #20
1 parent 8b4ef46 commit 049bd46

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const instance = startListen(652581, handler)
3939
instance.close()
4040
```
4141

42-
> 需传入房间的长id。短id需转为长id才能接收到消息。(#19 )
42+
> 需传入房间的长id。短id需转为长id才能接收到消息。(https://github.com/ddiu8081/blive-message-listener/issues/19)
4343
4444
## Handlers & Type Definitions
4545

@@ -308,20 +308,20 @@ export interface RankCountChangeMsg {
308308

309309
##### handler.onUserAction
310310

311-
用户进入、关注、分享直播间
311+
用户进入、关注、分享、点赞直播间
312312

313313
- 舰长进入直播间时,有几率会触发两次
314314
- 舰长进入直播间时,uname 超长可能会省略号截断
315315

316316
```ts
317317
export type Handler = {
318-
/** 用户进入、关注、分享直播间 */
318+
/** 用户进入、关注、分享、点赞直播间 */
319319
onUserAction: (msg: Message<UserActionMsg>) => void
320320
}
321321

322-
type msgType = 'INTERACT_WORD' | 'ENTRY_EFFECT'
322+
type msgType = 'INTERACT_WORD' | 'ENTRY_EFFECT' | 'LIKE_INFO_V3_CLICK'
323323

324-
type UserAction = 'enter' | 'follow' | 'share' | 'unknown'
324+
type UserAction = 'enter' | 'follow' | 'share' | 'like' | 'unknown'
325325

326326
export interface UserActionMsg {
327327
user: User

src/listener/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
PREPARING, type LiveStopMsgHandler,
55
DANMU_MSG, type DanmuMsgHandler,
66
GUARD_BUY, type GuardBuyHandler,
7-
INTERACT_WORD, ENTRY_EFFECT, type UserActionMsgHandler,
7+
INTERACT_WORD, ENTRY_EFFECT, LIKE_INFO_V3_CLICK, type UserActionMsgHandler,
88
LIKE_INFO_V3_UPDATE, type LikedChangeMsgHandler,
99
ONLINE_RANK_COUNT, type RankCountChangeMsgHandler,
1010
ROOM_CHANGE, type RoomInfoChangeHandler,
@@ -119,7 +119,7 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
119119
})
120120
}
121121

122-
// INTERACT_WORD, ENTRY_EFFECT
122+
// INTERACT_WORD, ENTRY_EFFECT, LIKE_INFO_V3_CLICK
123123
if (handler[INTERACT_WORD.handlerName] || handler[ENTRY_EFFECT.handlerName] || rawHandlerNames.has(INTERACT_WORD.eventName) || rawHandlerNames.has(ENTRY_EFFECT.eventName)) {
124124
rawHandlerNames.delete(INTERACT_WORD.eventName)
125125
rawHandlerNames.delete(ENTRY_EFFECT.eventName)
@@ -133,6 +133,11 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
133133
const parsedData = ENTRY_EFFECT.parser(data.data, roomId)
134134
handler[ENTRY_EFFECT.handlerName]?.(normalizeDanmu(ENTRY_EFFECT.eventName, parsedData, data.data))
135135
})
136+
instance.on(LIKE_INFO_V3_CLICK.eventName, (data: WSMessage<any>) => {
137+
isHandleRaw && rawHandler[LIKE_INFO_V3_CLICK.eventName]?.(data.data)
138+
const parsedData = LIKE_INFO_V3_CLICK.parser(data.data, roomId)
139+
handler[LIKE_INFO_V3_CLICK.handlerName]?.(normalizeDanmu(LIKE_INFO_V3_CLICK.eventName, parsedData, data.data))
140+
})
136141
}
137142

138143
// LIKE_INFO_V3_UPDATE

src/parser/INTERACT_WORD_ENTRY_EFFECT.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { intToColorHex } from '../utils/color'
22
import type { Message, User } from '../types/app'
33

4-
type UserAction = 'enter' | 'follow' | 'share' | 'unknown'
4+
type UserAction = 'enter' | 'follow' | 'share' | 'like' | 'unknown'
55

66
export interface UserActionMsg {
77
user: User
@@ -72,10 +72,41 @@ const parserGuard = (data: any, roomId: number): UserActionMsg => {
7272
}
7373
}
7474

75+
const parserLike = (data: any, roomId: number): UserActionMsg => {
76+
const rawData = data.data
77+
return {
78+
user: {
79+
uid: rawData.uid,
80+
uname: rawData.uname,
81+
badge: rawData.fans_medal?.target_id ? {
82+
active: rawData.fans_medal?.is_lighted,
83+
name: rawData.fans_medal?.medal_name,
84+
level: rawData.fans_medal?.medal_level,
85+
color: intToColorHex(rawData.fans_medal?.medal_color),
86+
gradient: [
87+
intToColorHex(rawData.fans_medal?.medal_color_start),
88+
intToColorHex(rawData.fans_medal?.medal_color_start),
89+
intToColorHex(rawData.fans_medal?.medal_color_end),
90+
],
91+
anchor: {
92+
uid: rawData.fans_medal?.target_id,
93+
uname: '',
94+
room_id: rawData.fans_medal?.anchor_roomid, // 返回为 0
95+
is_same_room: rawData.fans_medal?.anchor_roomid === roomId,
96+
}
97+
} : undefined,
98+
},
99+
action: 'like',
100+
timestamp: Date.now(),
101+
}
102+
}
103+
75104
const parser = (data: any, roomId: number): UserActionMsg => {
76105
const msgType = data.cmd
77106
if (msgType === 'ENTRY_EFFECT') {
78107
return parserGuard(data, roomId)
108+
} else if (msgType === 'LIKE_INFO_V3_CLICK') {
109+
return parserLike(data, roomId)
79110
} else {
80111
// INTERACT_WORD
81112
return parserNormal(data, roomId)
@@ -94,7 +125,13 @@ export const ENTRY_EFFECT = {
94125
handlerName: 'onUserAction' as const,
95126
}
96127

128+
export const LIKE_INFO_V3_CLICK = {
129+
parser,
130+
eventName: 'LIKE_INFO_V3_CLICK' as const,
131+
handlerName: 'onUserAction' as const,
132+
}
133+
97134
export type Handler = {
98-
/** 用户进入、关注、分享直播间 */
135+
/** 用户进入、关注、分享、点赞直播间 */
99136
onUserAction: (msg: Message<UserActionMsg>) => void
100137
}

src/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { LIVE, type Handler as LiveStartMsgHandler, type LiveStartMsg } from './
33
export { PREPARING, type Handler as LiveStopMsgHandler, type LiveEndMsg } from './PREPARING'
44
export { DANMU_MSG, type Handler as DanmuMsgHandler, type DanmuMsg } from './DANMU_MSG'
55
export { GUARD_BUY, type Handler as GuardBuyHandler, type GuardBuyMsg } from './GUARD_BUY'
6-
export { INTERACT_WORD, ENTRY_EFFECT, type Handler as UserActionMsgHandler, type UserActionMsg } from './INTERACT_WORD_ENTRY_EFFECT'
6+
export { INTERACT_WORD, ENTRY_EFFECT, LIKE_INFO_V3_CLICK, type Handler as UserActionMsgHandler, type UserActionMsg } from './INTERACT_WORD_ENTRY_EFFECT'
77
export { LIKE_INFO_V3_UPDATE, type Handler as LikedChangeMsgHandler, type LikedChangeMsg } from './LIKE_INFO_V3_UPDATE'
88
export { ONLINE_RANK_COUNT, type Handler as RankCountChangeMsgHandler, type RankCountChangeMsg } from './ONLINE_RANK_COUNT'
99
export { ROOM_CHANGE, type Handler as RoomInfoChangeHandler, type RoomInfoChangeMsg } from './ROOM_CHANGE'

0 commit comments

Comments
 (0)