补充一部分cmd信息,增加部分注释 (#25)

Co-authored-by: daofeng <jcb20150301@qq.com>
This commit is contained in:
2025-04-07 17:51:18 +08:00
committed by GitHub
parent ad7bc5fe32
commit 74a602e7bf
9 changed files with 189 additions and 140 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
.idea
.vscode
scripts
scripts

View File

@@ -21,7 +21,8 @@ type eventHandlers struct {
superChatHandlers []func(*message.SuperChat)
giftHandlers []func(*message.Gift)
guardBuyHandlers []func(*message.GuardBuy)
liveHandlers []func(*message.Live)
liveStartHandlers []func(start *message.LiveStart)
liveStopHandlers []func(start *message.LiveStop)
userToastHandlers []func(*message.UserToast)
}
@@ -61,11 +62,17 @@ func (c *Client) OnGuardBuy(f func(*message.GuardBuy)) {
c.eventHandlers.guardBuyHandlers = append(c.eventHandlers.guardBuyHandlers, f)
}
// OnLive 添加 开播事件 的处理器
func (c *Client) OnLive(f func(*message.Live)) {
c.eventHandlers.liveHandlers = append(c.eventHandlers.liveHandlers, f)
// OnLiveStart 添加 开播事件 的处理器
func (c *Client) OnLiveStart(f func(start *message.LiveStart)) {
c.eventHandlers.liveStartHandlers = append(c.eventHandlers.liveStartHandlers, f)
}
// OnLiveStop 添加 关播事件 的处理器
func (c *Client) OnLiveStop(f func(start *message.LiveStop)) {
c.eventHandlers.liveStopHandlers = append(c.eventHandlers.liveStopHandlers, f)
}
// OnUserToast 添加 UserToast 的处理
// OnUserToast 添加 UserToast 的处理器
func (c *Client) OnUserToast(f func(*message.UserToast)) {
c.eventHandlers.userToastHandlers = append(c.eventHandlers.userToastHandlers, f)
@@ -88,36 +95,49 @@ func (c *Client) Handle(p packet.Packet) {
return
}
switch cmd {
// 弹幕
case "DANMU_MSG":
d := new(message.Danmaku)
d.Parse(p.Body)
for _, fn := range c.eventHandlers.danmakuMessageHandlers {
go cover(func() { fn(d) })
}
// 醒目留言
case "SUPER_CHAT_MESSAGE":
s := new(message.SuperChat)
s.Parse(p.Body)
for _, fn := range c.eventHandlers.superChatHandlers {
go cover(func() { fn(s) })
}
// 礼物
case "SEND_GIFT":
g := new(message.Gift)
g.Parse(p.Body)
for _, fn := range c.eventHandlers.giftHandlers {
go cover(func() { fn(g) })
}
// 大航海
case "GUARD_BUY":
g := new(message.GuardBuy)
g.Parse(p.Body)
for _, fn := range c.eventHandlers.guardBuyHandlers {
go cover(func() { fn(g) })
}
// 开播
case "LIVE":
l := new(message.Live)
l := new(message.LiveStart)
l.Parse(p.Body)
for _, fn := range c.eventHandlers.liveHandlers {
for _, fn := range c.eventHandlers.liveStartHandlers {
go cover(func() { fn(l) })
}
//下播
case "PREPARING":
l := new(message.LiveStop)
l.Parse(p.Body)
for _, fn := range c.eventHandlers.liveStopHandlers {
go cover(func() { fn(l) })
}
// 用户 toast
case "USER_TOAST_MSG":
u := new(message.UserToast)
u.Parse(p.Body)

View File

@@ -13,8 +13,8 @@ import (
func main() {
log.SetLevel(log.DebugLevel)
c := client.NewClient(6)
c.SetCookie("this is a example cookie.")
c := client.NewClient(23943357)
c.SetCookie("")
//弹幕事件
c.OnDanmaku(func(danmaku *message.Danmaku) {
if danmaku.Type == message.EmoticonDanmaku {

View File

@@ -67,6 +67,7 @@ func (d *Danmaku) Parse(data []byte) {
ext := new(Extra)
emo := new(Emoticon)
//扩展字段
err := utils.UnmarshalStr(info.Get("0.15.extra").String(), ext)
if err != nil {
log.Error("parse danmaku extra failed")
@@ -77,27 +78,30 @@ func (d *Danmaku) Parse(data []byte) {
}
i2 := info.Get("2")
i3 := info.Get("3")
d.Content = info.Get("1").String()
d.Sender = &User{
Uid: int(i2.Get("0").Int()),
Uname: i2.Get("1").String(),
Admin: i2.Get("2").Bool(),
d.Content = info.Get("1").String() //弹幕内容
d.Sender = &User{ //用户信息
Uid: int(i2.Get("0").Int()), //用户uid
Uname: i2.Get("1").String(), //用户昵称
UserLevel: i2.Get("16.0").Int(), //用户等级
Admin: i2.Get("2").Bool(), //是否为管理者
Urank: int(i2.Get("5").Int()),
MobileVerify: i2.Get("6").Bool(),
GuardLevel: int(info.Get("7").Int()),
MobileVerify: i2.Get("6").Bool(), //是否绑定手机
GuardLevel: int(info.Get("7").Int()), //舰队等级
//勋章信息
Medal: &Medal{
Level: int(i3.Get("0").Int()),
Name: i3.Get("1").String(),
UpName: i3.Get("2").String(),
UpRoomId: int(i3.Get("3").Int()),
Color: int(i3.Get("4").Int()),
UpUid: int(i3.Get("12").Int()),
Level: int(i3.Get("0").Int()), //勋章等级
Name: i3.Get("1").String(), //勋章名称
UpName: i3.Get("2").String(), //勋章上主播昵称
UpRoomId: int(i3.Get("3").Int()), //上主播房间id
Color: int(i3.Get("4").Int()), //勋章颜色
UpUid: int(i3.Get("12").Int()), //上主播uid
},
}
d.Extra = ext
d.Emoticon = emo
d.Type = int(info.Get("0.12").Int())
d.Timestamp = info.Get("0.4").Int()
d.Type = int(info.Get("0.12").Int()) //弹幕类型
d.Timestamp = info.Get("0.4").Int() //时间戳
d.Raw = sb
dmv2Content := parsed.Get("dm_v2").String()

View File

@@ -6,109 +6,112 @@ import (
"github.com/tidwall/gjson"
)
// 礼物消息结构体
type Gift struct {
Action string `json:"action"`
BatchComboId string `json:"batch_combo_id"`
BatchComboSend interface{} `json:"batch_combo_send"`
BeatId string `json:"beatId"`
BizSource string `json:"biz_source"`
BlindGift interface{} `json:"blind_gift"`
BroadcastId int `json:"broadcast_id"`
CoinType string `json:"coin_type"`
ComboResourcesId int `json:"combo_resources_id"`
ComboSend interface{} `json:"combo_send"`
ComboStayTime int `json:"combo_stay_time"`
ComboTotalCoin int `json:"combo_total_coin"`
CritProb int `json:"crit_prob"`
Demarcation int `json:"demarcation"`
DiscountPrice int `json:"discount_price"`
Dmscore int `json:"dmscore"`
Draw int `json:"draw"`
Effect int `json:"effect"`
EffectBlock int `json:"effect_block"`
Face string `json:"face"`
FloatScResourceId int `json:"float_sc_resource_id"`
GiftId int `json:"giftId"`
GiftName string `json:"giftName"`
GiftType int `json:"giftType"`
Gold int `json:"gold"`
GuardLevel int `json:"guard_level"`
IsFirst bool `json:"is_first"`
IsSpecialBatch int `json:"is_special_batch"`
Magnification float64 `json:"magnification"`
Action string `json:"action"` // 操作类型
BatchComboId string `json:"batch_combo_id"` // 批量组合ID
BatchComboSend interface{} `json:"batch_combo_send"` // 批量组合发送信息
BeatId string `json:"beatId"` // Beat ID
BizSource string `json:"biz_source"` // 业务来源
BlindGift interface{} `json:"blind_gift"` // 盲盒礼物信息
BroadcastId int `json:"broadcast_id"` // 广播ID
CoinType string `json:"coin_type"` // 币种类型
ComboResourcesId int `json:"combo_resources_id"` // 组合资源ID
ComboSend interface{} `json:"combo_send"` // 组合发送信息
ComboStayTime int `json:"combo_stay_time"` // 组合停留时间
ComboTotalCoin int `json:"combo_total_coin"` // 组合总硬币数
CritProb int `json:"crit_prob"` // 批判概率
Demarcation int `json:"demarcation"` // 分界线
DiscountPrice int `json:"discount_price"` // 折扣价格
Dmscore int `json:"dmscore"` // DM分数
Draw int `json:"draw"` // 抽奖
Effect int `json:"effect"` // 效果
EffectBlock int `json:"effect_block"` // 效果块
Face string `json:"face"` // 头像URL
FloatScResourceId int `json:"float_sc_resource_id"` // 浮动资源ID
GiftId int `json:"giftId"` // 礼物ID
GiftName string `json:"giftName"` // 礼物名称
GiftType int `json:"giftType"` // 礼物类型
Gold int `json:"gold"` // 金币数量
GuardLevel int `json:"guard_level"` // 守护等级
IsFirst bool `json:"is_first"` // 是否首次赠送
IsSpecialBatch int `json:"is_special_batch"` // 是否特殊批量
Magnification float64 `json:"magnification"` // 放大倍数
MedalInfo struct {
AnchorRoomid int `json:"anchor_roomid"`
AnchorUname string `json:"anchor_uname"`
GuardLevel int `json:"guard_level"`
IconId int `json:"icon_id"`
IsLighted int `json:"is_lighted"`
MedalColor int `json:"medal_color"`
MedalColorBorder int `json:"medal_color_border"`
MedalColorEnd int `json:"medal_color_end"`
MedalColorStart int `json:"medal_color_start"`
MedalLevel int `json:"medal_level"`
MedalName string `json:"medal_name"`
Special string `json:"special"`
TargetId int `json:"target_id"`
AnchorRoomid int `json:"anchor_roomid"` // 主播房间ID
AnchorUname string `json:"anchor_uname"` // 主播用户名
GuardLevel int `json:"guard_level"` // 守护等级
IconId int `json:"icon_id"` // 图标ID
IsLighted int `json:"is_lighted"` // 是否点亮
MedalColor int `json:"medal_color"` // 勋章颜色
MedalColorBorder int `json:"medal_color_border"` // 勋章边框颜色
MedalColorEnd int `json:"medal_color_end"` // 勋章结束颜色
MedalColorStart int `json:"medal_color_start"` // 勋章开始颜色
MedalLevel int `json:"medal_level"` // 勋章等级
MedalName string `json:"medal_name"` // 勋章名称
Special string `json:"special"` // 特殊标记
TargetId int `json:"target_id"` // 目标ID
} `json:"medal_info"`
NameColor string `json:"name_color"`
Num int `json:"num"`
OriginalGiftName string `json:"original_gift_name"`
Price int `json:"price"`
Rcost int `json:"rcost"`
Remain int `json:"remain"`
Rnd string `json:"rnd"`
SendMaster interface{} `json:"send_master"`
Silver int `json:"silver"`
Super int `json:"super"`
SuperBatchGiftNum int `json:"super_batch_gift_num"`
SuperGiftNum int `json:"super_gift_num"`
SvgaBlock int `json:"svga_block"`
TagImage string `json:"tag_image"`
Tid string `json:"tid"`
Timestamp int `json:"timestamp"`
TopList interface{} `json:"top_list"`
TotalCoin int `json:"total_coin"`
Uid int `json:"uid"`
Uname string `json:"uname"`
NameColor string `json:"name_color"` // 名称颜色
Num int `json:"num"` // 数量
OriginalGiftName string `json:"original_gift_name"` // 原始礼物名称
Price int `json:"price"` // 价格
Rcost int `json:"rcost"` // 实际花费
Remain int `json:"remain"` // 剩余数量
Rnd string `json:"rnd"` // 随机数
SendMaster interface{} `json:"send_master"` // 发送者信息
Silver int `json:"silver"` // 银币数量
Super int `json:"super"` // 是否超级礼物
SuperBatchGiftNum int `json:"super_batch_gift_num"` // 超级批量礼物数量
SuperGiftNum int `json:"super_gift_num"` // 超级礼物数量
SvgaBlock int `json:"svga_block"` // SVGA块
TagImage string `json:"tag_image"` // 标签图片URL
Tid string `json:"tid"` // TID
Timestamp int `json:"timestamp"` // 时间戳
TopList interface{} `json:"top_list"` // 顶级列表
TotalCoin int `json:"total_coin"` // 总硬币数
Uid int `json:"uid"` // 用户ID
Uname string `json:"uname"` // 用户名
}
// 组合发送结构体
type ComboSend struct {
Action string `json:"action"`
BatchComboId string `json:"batch_combo_id"`
BatchComboNum int `json:"batch_combo_num"`
ComboId string `json:"combo_id"`
ComboNum int `json:"combo_num"`
ComboTotalCoin int `json:"combo_total_coin"`
Dmscore int `json:"dmscore"`
GiftId int `json:"gift_id"`
GiftName string `json:"gift_name"`
GiftNum int `json:"gift_num"`
IsShow int `json:"is_show"`
Action string `json:"action"` // 操作类型
BatchComboId string `json:"batch_combo_id"` // 批量组合ID
BatchComboNum int `json:"batch_combo_num"` // 批量组合数量
ComboId string `json:"combo_id"` // 组合ID
ComboNum int `json:"combo_num"` // 组合数量
ComboTotalCoin int `json:"combo_total_coin"` // 组合总硬币数
Dmscore int `json:"dmscore"` // DM分数
GiftId int `json:"gift_id"` // 礼物ID
GiftName string `json:"gift_name"` // 礼物名称
GiftNum int `json:"gift_num"` // 礼物数量
IsShow int `json:"is_show"` // 是否显示
MedalInfo struct {
AnchorRoomid int `json:"anchor_roomid"`
AnchorUname string `json:"anchor_uname"`
GuardLevel int `json:"guard_level"`
IconId int `json:"icon_id"`
IsLighted int `json:"is_lighted"`
MedalColor int `json:"medal_color"`
MedalColorBorder int `json:"medal_color_border"`
MedalColorEnd int `json:"medal_color_end"`
MedalColorStart int `json:"medal_color_start"`
MedalLevel int `json:"medal_level"`
MedalName string `json:"medal_name"`
Special string `json:"special"`
TargetId int `json:"target_id"`
AnchorRoomid int `json:"anchor_roomid"` // 主播房间ID
AnchorUname string `json:"anchor_uname"` // 主播用户名
GuardLevel int `json:"guard_level"` // 守护等级
IconId int `json:"icon_id"` // 图标ID
IsLighted int `json:"is_lighted"` // 是否点亮
MedalColor int `json:"medal_color"` // 勋章颜色
MedalColorBorder int `json:"medal_color_border"` // 勋章边框颜色
MedalColorEnd int `json:"medal_color_end"` // 勋章结束颜色
MedalColorStart int `json:"medal_color_start"` // 勋章开始颜色
MedalLevel int `json:"medal_level"` // 勋章等级
MedalName string `json:"medal_name"` // 勋章名称
Special string `json:"special"` // 特殊标记
TargetId int `json:"target_id"` // 目标ID
} `json:"medal_info"`
NameColor string `json:"name_color"`
RUname string `json:"r_uname"`
Ruid int `json:"ruid"`
SendMaster interface{} `json:"send_master"`
TotalNum int `json:"total_num"`
Uid int `json:"uid"`
Uname string `json:"uname"`
NameColor string `json:"name_color"` // 名称颜色
RUname string `json:"r_uname"` // 接收者用户名
Ruid int `json:"ruid"` // 接收者用户ID
SendMaster interface{} `json:"send_master"` // 发送者信息
TotalNum int `json:"total_num"` // 总数量
Uid int `json:"uid"` // 用户ID
Uname string `json:"uname"` // 用户名
}
// 解析礼物消息数据
func (g *Gift) Parse(data []byte) {
sb := utils.BytesToString(data)
sd := gjson.Get(sb, "data").String()

View File

@@ -6,16 +6,17 @@ import (
"github.com/tidwall/gjson"
)
// GuardBuy 购买守护消息结构体
type GuardBuy struct {
Uid int `json:"uid"`
Username string `json:"username"`
GuardLevel int `json:"guard_level"`
Num int `json:"num"`
Price int `json:"price"`
GiftId int `json:"gift_id"`
GiftName string `json:"gift_name"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
Uid int `json:"uid"` // 用户ID
Username string `json:"username"` // 用户名
GuardLevel int `json:"guard_level"` // 守护等级
Num int `json:"num"` // 数量
Price int `json:"price"` // 价格
GiftId int `json:"gift_id"` // 礼物ID
GiftName string `json:"gift_name"` // 礼物名称
StartTime int `json:"start_time"` // 开始时间戳
EndTime int `json:"end_time"` // 结束时间戳
}
func (g *GuardBuy) Parse(data []byte) {

View File

@@ -5,27 +5,47 @@ import (
log "github.com/sirupsen/logrus"
)
// StopLiveRoomList 停止直播房间列表结构体
type StopLiveRoomList struct {
RoomIdList []int `json:"room_id_list"`
RoomIdList []int `json:"room_id_list"` // 房间ID列表
}
type Live struct {
Cmd string `json:"cmd"`
LiveKey string `json:"live_key"`
VoiceBackground string `json:"voice_background"`
SubSessionKey string `json:"sub_session_key"`
LivePlatform string `json:"live_platform"`
LiveModel int `json:"live_model"`
LiveTime int `json:"live_time"`
Roomid int `json:"roomid"`
// Live 直播消息结构体
type LiveStart struct {
Cmd string `json:"cmd"` // 命令
LiveKey string `json:"live_key"` // 直播密钥
VoiceBackground string `json:"voice_background"` // 语音背景
SubSessionKey string `json:"sub_session_key"` // 子会话密钥
LivePlatform string `json:"live_platform"` // 直播平台
LiveModel int `json:"live_model"` // 直播模式
LiveTime int `json:"live_time"` // 直播时间
Roomid int `json:"roomid"` // 房间ID
}
// 停止直播
type LiveStop struct {
Cmd string `json:"cmd"`
MsgId string `json:"msg_id"`
PIsAck bool `json:"p_is_ack"`
PMsgType int `json:"p_msg_type"`
Roomid string `json:"roomid"`
Round int `json:"round"` //开启轮播时存在,轮播状态: 1正在轮播 0未轮播
SendTime int64 `json:"send_time"`
}
// Preparing 直播准备中消息结构体
type Preparing struct {
Cmd string `json:"cmd"`
Roomid string `json:"roomid"`
Cmd string `json:"cmd"` // 命令
Roomid string `json:"roomid"` // 房间ID
}
func (l *Live) Parse(data []byte) {
func (l *LiveStart) Parse(data []byte) {
err := json.Unmarshal(data, l)
if err != nil {
log.Error("parse live failed")
}
}
func (l *LiveStop) Parse(data []byte) {
err := json.Unmarshal(data, l)
if err != nil {
log.Error("parse live failed")

View File

@@ -6,7 +6,7 @@ import (
"github.com/tidwall/gjson"
)
// SuperChat
// SuperChat 超级弹幕消息结构体
// message_jpn: 消息日文翻译目前只出现在SUPER_CHAT_MESSAGE_JPN
// id_: str消息ID删除时用
type SuperChat struct {

View File

@@ -8,6 +8,7 @@ type User struct {
MobileVerify bool
Medal *Medal
GuardLevel int
UserLevel int64
}
type Medal struct {