mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-05 02:30:23 +08:00
fix
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -21,3 +21,4 @@
|
||||
go.work
|
||||
|
||||
bot_test.go
|
||||
/test
|
||||
|
||||
27
bot.go
27
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 消息转义
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user