diff --git a/.gitignore b/.gitignore index 58e97b9..2df58b9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ go.work bot_test.go +/test diff --git a/bot.go b/bot.go index 46da3c5..3116aa4 100644 --- a/bot.go +++ b/bot.go @@ -1,7 +1,6 @@ package nano import ( - "crypto/tls" "encoding/base64" "encoding/json" "net" @@ -76,9 +75,9 @@ func (bot *Bot) reveive() (payload WebsocketPayload, err error) { // Connect 连接到 Gateway + 鉴权连接 // // https://bot.q.qq.com/wiki/develop/api/gateway/reference.html#_1-%E8%BF%9E%E6%8E%A5%E5%88%B0-gateway -func (bot *Bot) Connect() { +func (bot *Bot) Connect() *Bot { network, address := resolveURI(bot.gateway) - log.Infoln("[bot] 开始尝试连接到网关:", network, ", AppID:", bot.AppID) + log.Infoln("[bot] 开始尝试连接到网关:", address, ", AppID:", bot.AppID) dialer := websocket.Dialer{ NetDial: func(_, addr string) (net.Conn, error) { if network == "unix" { @@ -91,7 +90,7 @@ func (bot *Bot) Connect() { addr = BytesToString(filepath) } } - return tls.Dial(network, addr, websocket.DefaultDialer.TLSClientConfig) // support unix socket transport + return net.Dial(network, addr) // support unix socket transport }, } for { @@ -153,9 +152,10 @@ func (bot *Bot) Connect() { } break } - clients.Store(bot.ready.User.ID, bot) + clients.Store(bot.AppID, bot) log.Infoln("[bot] 连接到网关成功, 用户名:", bot.ready.User.Username) go bot.doheartbeat() + return bot } // doheartbeat 按指定间隔进行心跳包发送 @@ -181,3 +181,20 @@ func (bot *Bot) doheartbeat() { } } } + +// Listen 监听事件 +func (bot *Bot) Listen() { + log.Infoln("[bot] 开始监听", bot.ready.User.Username, "的事件") + payload := WebsocketPayload{} + for { + err := bot.conn.ReadJSON(&payload) + if err != nil { // reconnect + clients.Delete(bot.AppID) + log.Warn("[bot]", bot.ready.User.Username, "的网关连接断开...") + time.Sleep(time.Millisecond * time.Duration(3)) + bot.Connect() + continue + } + log.Debugln("[bot] 接收到事件:", payload.Op, ", 类型:", payload.T) + } +} diff --git a/helper.go b/helper.go index d266746..7958e86 100644 --- a/helper.go +++ b/helper.go @@ -23,12 +23,12 @@ func getFuncNameWithSkip(n int) string { // getThisFuncName 获取正在执行的函数名 func getThisFuncName() string { - return getFuncNameWithSkip(1) + return getFuncNameWithSkip(2) } // getCallerFuncName 获取调用者函数名 func getCallerFuncName() string { - return getFuncNameWithSkip(2) + return getFuncNameWithSkip(3) } // MessageEscape 消息转义 diff --git a/intent.go b/intent.go index c5ea273..16bb02e 100644 --- a/intent.go +++ b/intent.go @@ -18,4 +18,7 @@ const ( IntentAll = IntentGuilds | IntentGuildMembers | IntentGuildMessages | IntentGuildMessageReactions | IntentDirectMessage | IntentOpenForumsEvent | IntentAudioOrLiveChannelMember | IntentInteraction | IntentMessageAudit | IntentForumsEvent | IntentAudioAction | IntentPublicGuildMessages + IntentPublic = IntentGuilds | IntentGuildMembers | IntentGuildMessageReactions | + IntentDirectMessage | IntentOpenForumsEvent | IntentAudioOrLiveChannelMember | IntentInteraction | + IntentMessageAudit | IntentAudioAction | IntentPublicGuildMessages ) diff --git a/payload.go b/payload.go index 318d39b..f8cba02 100644 --- a/payload.go +++ b/payload.go @@ -19,7 +19,7 @@ type WebsocketPayload struct { // GetHeartbeatInterval OpCodeHello 获得心跳周期 单位毫秒 func (wp *WebsocketPayload) GetHeartbeatInterval() (uint32, error) { if wp.Op != OpCodeHello { - return 0, errors.New(getThisFuncName() + " unexpected OpCode " + strconv.Itoa(int(wp.Op))) + return 0, errors.New(getThisFuncName() + " unexpected OpCode " + strconv.Itoa(int(wp.Op)) + ", T: " + wp.T + ", D: " + BytesToString(wp.D)) } data := &struct { H uint32 `json:"heartbeat_interval"` @@ -52,11 +52,11 @@ type EventReady struct { // GetEventReady OpCodeDispatch READY func (wp *WebsocketPayload) GetEventReady() (er EventReady, err error) { if wp.Op != OpCodeDispatch { - err = errors.New(getThisFuncName() + " unexpected OpCode " + strconv.Itoa(int(wp.Op))) + err = errors.New(getThisFuncName() + " unexpected OpCode " + strconv.Itoa(int(wp.Op)) + ", T: " + wp.T + ", D: " + BytesToString(wp.D)) return } if wp.T != "READY" { - err = errors.New(getThisFuncName() + " unexpected event type " + wp.T) + err = errors.New(getThisFuncName() + " unexpected event type " + wp.T + ", T: " + wp.T + ", D: " + BytesToString(wp.D)) return } err = json.Unmarshal(wp.D, &er)