Skip to content

Commit

Permalink
feat(live-mini): 礼物消息展示
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Oct 24, 2022
1 parent 688244b commit ad5f6a0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
22 changes: 22 additions & 0 deletions packages/live-mini/src/component/msg/gift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ansis from 'ansis'
import type { GiftMsg } from 'blive-message-listener'
import { usernameCom } from '../username'

const sendMaster = (msg: GiftMsg) => {
if (msg.send_master?.uname) {
return `给 ${ ansis.magenta(msg.send_master.uname) }`
}
return ''
}

export const giftMsgCom = (msg: GiftMsg) => {
const components = [
usernameCom(msg.user),
'赠送了',
ansis.magenta(`(¥${ msg.price / 1000 }) ${ msg.gift_name }`),
`* ${ msg.amount }`,
sendMaster(msg),
].filter(Boolean)

return components.join(' ')
}
2 changes: 2 additions & 0 deletions packages/live-mini/src/component/msg/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './danmu'
export * from './superChat'
export * from './gift'
2 changes: 1 addition & 1 deletion packages/live-mini/src/component/msg/superChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ansis from 'ansis'
import type { SuperChatMsg } from 'blive-message-listener'
import { usernameCom } from '../username'

export const danmuMsgCom = (msg: SuperChatMsg) => {
export const superChatMsgCom = (msg: SuperChatMsg) => {
const components = [
`${usernameCom(msg.user)}:`,
`(¥${ msg.price })`,
Expand Down
12 changes: 6 additions & 6 deletions packages/live-mini/src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { startListen } from 'blive-message-listener'
import type {
MsgHandler, Message,
DanmuMsg, SuperChatMsg, GiftMsg, GuardBuyMsg, UserActionMsg
} from 'blive-message-listener'
import { danmuMsgCom } from './component/msg'
import type { MsgHandler } from 'blive-message-listener'
import { danmuMsgCom, superChatMsgCom, giftMsgCom } from './component/msg'

export const startInstance = (roomId: number, options: AppOptions) => {
const handler: MsgHandler = {
onIncomeDanmu: (msg) => {
!msg.body.lottery && console.log(danmuMsgCom(msg.body))
},
onIncomeSuperChat: (msg) => {
console.log(danmuMsgCom(msg.body))
console.log(superChatMsgCom(msg.body))
},
onGift: (msg) => {
console.log(giftMsgCom(msg.body))
}
}
startListen(roomId, handler)
Expand Down

0 comments on commit ad5f6a0

Please sign in to comment.