diff --git a/coolq/bot.go b/coolq/bot.go index 6f36aa2..d27ff7b 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -5,28 +5,30 @@ import ( "encoding/gob" "encoding/json" "fmt" - "github.com/Mrs4s/MiraiGo/binary" - "github.com/Mrs4s/MiraiGo/client" - "github.com/Mrs4s/MiraiGo/message" - "github.com/Mrs4s/go-cqhttp/global" - log "github.com/sirupsen/logrus" - "github.com/tidwall/gjson" - "github.com/xujiajun/nutsdb" "hash/crc32" "path" "sync" "time" + + "github.com/Mrs4s/MiraiGo/binary" + "github.com/Mrs4s/MiraiGo/client" + "github.com/Mrs4s/MiraiGo/message" + "github.com/Mrs4s/go-cqhttp/global" + + log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" + "github.com/xujiajun/nutsdb" ) type CQBot struct { Client *client.QQClient - events []func(MSG) - db *nutsdb.DB - friendReqCache sync.Map - invitedReqCache sync.Map - joinReqCache sync.Map - tempMsgCache sync.Map + events []func(MSG) + db *nutsdb.DB + friendReqCache sync.Map + invitedReqCache sync.Map + joinReqCache sync.Map + tempMsgCache sync.Map } type MSG map[string]interface{} @@ -67,15 +69,20 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot { bot.Client.OnGroupInvited(bot.groupInvitedEvent) bot.Client.OnUserWantJoinGroup(bot.groupJoinReqEvent) go func() { + i := conf.HeartbeatInterval + if i < 1 { + log.Warn("警告: 心跳功能已关闭,若非预期,请检查配置文件。") + return + } for { - time.Sleep(time.Second * 5) + time.Sleep(time.Second * i) bot.dispatchEventMessage(MSG{ "time": time.Now().Unix(), "self_id": bot.Client.Uin, "post_type": "meta_event", "meta_event_type": "heartbeat", "status": nil, - "interval": 5000, + "interval": 1000 * i, }) } }() diff --git a/docs/config.md b/docs/config.md index d3f0373..7ee7c56 100644 --- a/docs/config.md +++ b/docs/config.md @@ -27,6 +27,7 @@ go-cqhttp 支持导入CQHTTP的配置文件, 具体步骤为: "post_message_format": "string", "ignore_invalid_cqcode": false, "force_fragmented": true, + "heartbeat_interval": 5, "http_config": { "enabled": true, "host": "0.0.0.0", @@ -55,17 +56,18 @@ go-cqhttp 支持导入CQHTTP的配置文件, 具体步骤为: | ------------------ | -------- | ------------------------------------------------------------------- | | uin | int64 | 登录用QQ号 | | password | string | 登录用密码 | -| encrypt_password | bool | 是否对密码进行加密. | +| encrypt_password | bool | 是否对密码进行加密. | | password_encrypted | string | 加密后的密码(请勿修改) | -| enable_db | bool | 是否开启内置数据库, 关闭后将无法使用 **回复/撤回** 等上下文相关接口 | -| access_token | string | 同CQHTTP的 `access_token` 用于身份验证 | -| relogin | bool | 是否自动重新登录 | -| relogin_delay | int | 重登录延时(秒) | -| post_message_format | string | 上报信息类型 | -| ignore_invalid_cqcode| bool | 是否忽略错误的CQ码 | +| enable_db | bool | 是否开启内置数据库, 关闭后将无法使用 **回复/撤回** 等上下文相关接口 | +| access_token | string | 同CQHTTP的 `access_token` 用于身份验证 | +| relogin | bool | 是否自动重新登录 | +| relogin_delay | int | 重登录延时(秒) | +| post_message_format | string | 上报信息类型 | +| ignore_invalid_cqcode| bool | 是否忽略错误的CQ码 | | force_fragmented | bool | 是否强制分片发送群长消息 | -| http_config | object | HTTP API配置 | -| ws_config | object | Websocket API 配置 | +| heartbeat_interval | int64 | 心跳间隔时间,单位秒,若0则关闭心跳 | +| http_config | object | HTTP API配置 | +| ws_config | object | Websocket API 配置 | | ws_reverse_servers | object[] | 反向 Websocket API 配置 | | log_level | string | 指定日志收集级别,将收集的日志单独存放到固定文件中,便于查看日志线索 当前支持 warn,error| @@ -73,4 +75,6 @@ go-cqhttp 支持导入CQHTTP的配置文件, 具体步骤为: > 解密后密码将储存在内存中,用于自动重连等功能. 所以此加密并不能防止内存读取. > 解密密钥在使用完成后并不会留存在内存中, 所以可用相对简单的字符串作为密钥 -> 注2: 分片发送为原酷Q发送长消息的老方案, 发送速度更优/兼容性更好。关闭后将优先使用新方案, 能发送更长的消息, 但发送速度更慢,在部分老客户端将无法解析. \ No newline at end of file +> 注2: 分片发送为原酷Q发送长消息的老方案, 发送速度更优/兼容性更好。关闭后将优先使用新方案, 能发送更长的消息, 但发送速度更慢,在部分老客户端将无法解析. + +> 注3:关闭心跳服务可能引起断线,请谨慎关闭 \ No newline at end of file diff --git a/global/config.go b/global/config.go index e384137..bd57850 100644 --- a/global/config.go +++ b/global/config.go @@ -2,6 +2,8 @@ package global import ( "encoding/json" + "time" + log "github.com/sirupsen/logrus" ) @@ -16,6 +18,7 @@ type JsonConfig struct { ReLoginDelay int `json:"relogin_delay"` IgnoreInvalidCQCode bool `json:"ignore_invalid_cqcode"` ForceFragmented bool `json:"force_fragmented"` + HeartbeatInterval time.Duration `json:"heartbeat_interval"` HttpConfig *GoCQHttpConfig `json:"http_config"` WSConfig *GoCQWebsocketConfig `json:"ws_config"` ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`