Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Room } = require('bilibili-live')
- const config = {
- roomId: 30493,
- url: 30493,
- useWebsocket: true,
- useWSS: true
- }
- const br = s => `\x1b[1m\x1b[31m${s}\x1b[0m`
- const bu = s => `\x1b[1m\x1b[4m${s}\x1b[0m`
- new Room(config)
- .connect()
- .then(room => {
- room.on('danmaku.connect', () => {
- // console.log(`进入直播间: ${config.roomId}`)
- })
- room.on('danmaku.message', msg => {
- if (msg.type == 'gift') {
- const { gift: { name: giftName, count }, user: { name: userName } } = msg
- console.log(`🎁 🎁 收到礼物: ${br(giftName + ' X ' + count)} 来自 ${userName}`)
- }
- else if (msg.type == 'online') {
- // console.log('当前在线人数' + msg.number)
- }
- else if (msg.type == 'comment') {
- let comment = ''
- if (msg.user.isVIP || msg.user.isSVIP) {
- comment += br('[年]')
- }
- if (msg.user.badge) { // 主播勋章
- comment += br(`[${msg.user.badge.title}${msg.user.badge.level}]`)
- }
- if (msg.user.level) { // 用户等级
- comment += `[Lv.${msg.user.level}]`
- }
- comment += `${msg.user.name}: ${bu(msg.comment)}`
- console.log(`==> ${comment}`)
- }
- else if (msg.type == 'welcome' || msg.type == 'welcomeGuard' || msg.type == 'guardBuy' || msg.type == 'block') {
- // console.log('others')
- }
- })
- room.on('newFans', msg => {
- console.log(`👏 👏 ${msg.user.name} 关注了直播间`)
- })
- room.on('danmaku.error', () => {
- console.log(br('something wrong'))
- })
- room.on('info', ({ liveStartTime, title }) => {
- console.log(`成功进入直播间: ${config.roomId} ${title}`)
- console.log(`直播开始时间: ${liveStartTime}`)
- })
- })
- .catch(e => {
- console.log(e)
- })
Add Comment
Please, Sign In to add comment