Guest User

Untitled

a guest
Jan 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. const { Room } = require('bilibili-live')
  2.  
  3. const config = {
  4. roomId: 30493,
  5. url: 30493,
  6. useWebsocket: true,
  7. useWSS: true
  8. }
  9.  
  10. const br = s => `\x1b[1m\x1b[31m${s}\x1b[0m`
  11. const bu = s => `\x1b[1m\x1b[4m${s}\x1b[0m`
  12.  
  13. new Room(config)
  14. .connect()
  15. .then(room => {
  16. room.on('danmaku.connect', () => {
  17. // console.log(`进入直播间: ${config.roomId}`)
  18. })
  19. room.on('danmaku.message', msg => {
  20.  
  21. if (msg.type == 'gift') {
  22. const { gift: { name: giftName, count }, user: { name: userName } } = msg
  23. console.log(`🎁 🎁 收到礼物: ${br(giftName + ' X ' + count)} 来自 ${userName}`)
  24. }
  25. else if (msg.type == 'online') {
  26. // console.log('当前在线人数' + msg.number)
  27. }
  28. else if (msg.type == 'comment') {
  29. let comment = ''
  30. if (msg.user.isVIP || msg.user.isSVIP) {
  31. comment += br('[年]')
  32. }
  33. if (msg.user.badge) { // 主播勋章
  34. comment += br(`[${msg.user.badge.title}${msg.user.badge.level}]`)
  35. }
  36. if (msg.user.level) { // 用户等级
  37. comment += `[Lv.${msg.user.level}]`
  38. }
  39. comment += `${msg.user.name}: ${bu(msg.comment)}`
  40. console.log(`==> ${comment}`)
  41. }
  42. else if (msg.type == 'welcome' || msg.type == 'welcomeGuard' || msg.type == 'guardBuy' || msg.type == 'block') {
  43. // console.log('others')
  44. }
  45. })
  46. room.on('newFans', msg => {
  47. console.log(`👏 👏 ${msg.user.name} 关注了直播间`)
  48. })
  49. room.on('danmaku.error', () => {
  50. console.log(br('something wrong'))
  51. })
  52. room.on('info', ({ liveStartTime, title }) => {
  53. console.log(`成功进入直播间: ${config.roomId} ${title}`)
  54. console.log(`直播开始时间: ${liveStartTime}`)
  55. })
  56. })
  57. .catch(e => {
  58. console.log(e)
  59. })
Add Comment
Please, Sign In to add comment