mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-10 21:24:43 +08:00
fix
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -21,3 +21,4 @@
|
|||||||
go.work
|
go.work
|
||||||
|
|
||||||
bot_test.go
|
bot_test.go
|
||||||
|
/test
|
||||||
|
|||||||
27
bot.go
27
bot.go
@@ -1,7 +1,6 @@
|
|||||||
package nano
|
package nano
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
@@ -76,9 +75,9 @@ func (bot *Bot) reveive() (payload WebsocketPayload, err error) {
|
|||||||
// Connect 连接到 Gateway + 鉴权连接
|
// Connect 连接到 Gateway + 鉴权连接
|
||||||
//
|
//
|
||||||
// https://bot.q.qq.com/wiki/develop/api/gateway/reference.html#_1-%E8%BF%9E%E6%8E%A5%E5%88%B0-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)
|
network, address := resolveURI(bot.gateway)
|
||||||
log.Infoln("[bot] 开始尝试连接到网关:", network, ", AppID:", bot.AppID)
|
log.Infoln("[bot] 开始尝试连接到网关:", address, ", AppID:", bot.AppID)
|
||||||
dialer := websocket.Dialer{
|
dialer := websocket.Dialer{
|
||||||
NetDial: func(_, addr string) (net.Conn, error) {
|
NetDial: func(_, addr string) (net.Conn, error) {
|
||||||
if network == "unix" {
|
if network == "unix" {
|
||||||
@@ -91,7 +90,7 @@ func (bot *Bot) Connect() {
|
|||||||
addr = BytesToString(filepath)
|
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 {
|
for {
|
||||||
@@ -153,9 +152,10 @@ func (bot *Bot) Connect() {
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
clients.Store(bot.ready.User.ID, bot)
|
clients.Store(bot.AppID, bot)
|
||||||
log.Infoln("[bot] 连接到网关成功, 用户名:", bot.ready.User.Username)
|
log.Infoln("[bot] 连接到网关成功, 用户名:", bot.ready.User.Username)
|
||||||
go bot.doheartbeat()
|
go bot.doheartbeat()
|
||||||
|
return bot
|
||||||
}
|
}
|
||||||
|
|
||||||
// doheartbeat 按指定间隔进行心跳包发送
|
// 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 获取正在执行的函数名
|
// getThisFuncName 获取正在执行的函数名
|
||||||
func getThisFuncName() string {
|
func getThisFuncName() string {
|
||||||
return getFuncNameWithSkip(1)
|
return getFuncNameWithSkip(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getCallerFuncName 获取调用者函数名
|
// getCallerFuncName 获取调用者函数名
|
||||||
func getCallerFuncName() string {
|
func getCallerFuncName() string {
|
||||||
return getFuncNameWithSkip(2)
|
return getFuncNameWithSkip(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MessageEscape 消息转义
|
// MessageEscape 消息转义
|
||||||
|
|||||||
@@ -18,4 +18,7 @@ const (
|
|||||||
IntentAll = IntentGuilds | IntentGuildMembers | IntentGuildMessages | IntentGuildMessageReactions |
|
IntentAll = IntentGuilds | IntentGuildMembers | IntentGuildMessages | IntentGuildMessageReactions |
|
||||||
IntentDirectMessage | IntentOpenForumsEvent | IntentAudioOrLiveChannelMember | IntentInteraction |
|
IntentDirectMessage | IntentOpenForumsEvent | IntentAudioOrLiveChannelMember | IntentInteraction |
|
||||||
IntentMessageAudit | IntentForumsEvent | IntentAudioAction | IntentPublicGuildMessages
|
IntentMessageAudit | IntentForumsEvent | IntentAudioAction | IntentPublicGuildMessages
|
||||||
|
IntentPublic = IntentGuilds | IntentGuildMembers | IntentGuildMessageReactions |
|
||||||
|
IntentDirectMessage | IntentOpenForumsEvent | IntentAudioOrLiveChannelMember | IntentInteraction |
|
||||||
|
IntentMessageAudit | IntentAudioAction | IntentPublicGuildMessages
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type WebsocketPayload struct {
|
|||||||
// GetHeartbeatInterval OpCodeHello 获得心跳周期 单位毫秒
|
// GetHeartbeatInterval OpCodeHello 获得心跳周期 单位毫秒
|
||||||
func (wp *WebsocketPayload) GetHeartbeatInterval() (uint32, error) {
|
func (wp *WebsocketPayload) GetHeartbeatInterval() (uint32, error) {
|
||||||
if wp.Op != OpCodeHello {
|
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 {
|
data := &struct {
|
||||||
H uint32 `json:"heartbeat_interval"`
|
H uint32 `json:"heartbeat_interval"`
|
||||||
@@ -52,11 +52,11 @@ type EventReady struct {
|
|||||||
// GetEventReady OpCodeDispatch READY
|
// GetEventReady OpCodeDispatch READY
|
||||||
func (wp *WebsocketPayload) GetEventReady() (er EventReady, err error) {
|
func (wp *WebsocketPayload) GetEventReady() (er EventReady, err error) {
|
||||||
if wp.Op != OpCodeDispatch {
|
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
|
return
|
||||||
}
|
}
|
||||||
if wp.T != "READY" {
|
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
|
return
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(wp.D, &er)
|
err = json.Unmarshal(wp.D, &er)
|
||||||
|
|||||||
Reference in New Issue
Block a user