mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-07 20:45:53 +08:00
新增多次自动重连
This commit is contained in:
parent
cefbfb64ce
commit
4d376c0518
@ -22,8 +22,11 @@ go-cqhttp 支持导入CQHTTP的配置文件, 具体步骤为:
|
|||||||
"password_encrypted": "",
|
"password_encrypted": "",
|
||||||
"enable_db": true,
|
"enable_db": true,
|
||||||
"access_token": "",
|
"access_token": "",
|
||||||
"relogin": false,
|
"relogin": {
|
||||||
"relogin_delay": 0,
|
"enabled": true,
|
||||||
|
"relogin_delay": 3,
|
||||||
|
"max_relogin_times": 0
|
||||||
|
},
|
||||||
"post_message_format": "string",
|
"post_message_format": "string",
|
||||||
"ignore_invalid_cqcode": false,
|
"ignore_invalid_cqcode": false,
|
||||||
"force_fragmented": true,
|
"force_fragmented": true,
|
||||||
@ -62,6 +65,7 @@ go-cqhttp 支持导入CQHTTP的配置文件, 具体步骤为:
|
|||||||
| access_token | string | 同CQHTTP的 `access_token` 用于身份验证 |
|
| access_token | string | 同CQHTTP的 `access_token` 用于身份验证 |
|
||||||
| relogin | bool | 是否自动重新登录 |
|
| relogin | bool | 是否自动重新登录 |
|
||||||
| relogin_delay | int | 重登录延时(秒) |
|
| relogin_delay | int | 重登录延时(秒) |
|
||||||
|
| max_relogin_times | uint | 最大重登录次数,若0则不设置上限 |
|
||||||
| post_message_format | string | 上报信息类型 |
|
| post_message_format | string | 上报信息类型 |
|
||||||
| ignore_invalid_cqcode| bool | 是否忽略错误的CQ码 |
|
| ignore_invalid_cqcode| bool | 是否忽略错误的CQ码 |
|
||||||
| force_fragmented | bool | 是否强制分片发送群长消息 |
|
| force_fragmented | bool | 是否强制分片发送群长消息 |
|
||||||
|
@ -8,14 +8,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type JsonConfig struct {
|
type JsonConfig struct {
|
||||||
Uin int64 `json:"uin"`
|
Uin int64 `json:"uin"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
EncryptPassword bool `json:"encrypt_password"`
|
EncryptPassword bool `json:"encrypt_password"`
|
||||||
PasswordEncrypted string `json:"password_encrypted"`
|
PasswordEncrypted string `json:"password_encrypted"`
|
||||||
EnableDB bool `json:"enable_db"`
|
EnableDB bool `json:"enable_db"`
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"`
|
||||||
ReLogin bool `json:"relogin"`
|
ReLogin struct {
|
||||||
ReLoginDelay int `json:"relogin_delay"`
|
Enabled bool `json:"enabled"`
|
||||||
|
ReLoginDelay int `json:"relogin_delay"`
|
||||||
|
MaxReloginTimes uint `json:"max_relogin_times"`
|
||||||
|
} `json:"relogin"`
|
||||||
IgnoreInvalidCQCode bool `json:"ignore_invalid_cqcode"`
|
IgnoreInvalidCQCode bool `json:"ignore_invalid_cqcode"`
|
||||||
ForceFragmented bool `json:"force_fragmented"`
|
ForceFragmented bool `json:"force_fragmented"`
|
||||||
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
|
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
|
||||||
@ -70,9 +73,16 @@ type GoCQReverseWebsocketConfig struct {
|
|||||||
|
|
||||||
func DefaultConfig() *JsonConfig {
|
func DefaultConfig() *JsonConfig {
|
||||||
return &JsonConfig{
|
return &JsonConfig{
|
||||||
EnableDB: true,
|
EnableDB: true,
|
||||||
ReLogin: true,
|
ReLogin: struct {
|
||||||
ReLoginDelay: 3,
|
Enabled bool `json:"enabled"`
|
||||||
|
ReLoginDelay int `json:"relogin_delay"`
|
||||||
|
MaxReloginTimes uint `json:"max_relogin_times"`
|
||||||
|
}{
|
||||||
|
Enabled: true,
|
||||||
|
ReLoginDelay: 3,
|
||||||
|
MaxReloginTimes: 0,
|
||||||
|
},
|
||||||
PostMessageFormat: "string",
|
PostMessageFormat: "string",
|
||||||
ForceFragmented: true,
|
ForceFragmented: true,
|
||||||
HttpConfig: &GoCQHttpConfig{
|
HttpConfig: &GoCQHttpConfig{
|
||||||
|
73
main.go
73
main.go
@ -7,16 +7,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
|
||||||
"github.com/Mrs4s/MiraiGo/client"
|
|
||||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
|
||||||
"github.com/Mrs4s/go-cqhttp/server"
|
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
||||||
"github.com/rifflock/lfshook"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
easy "github.com/t-tomalak/logrus-easy-formatter"
|
|
||||||
asciiart "github.com/yinghau76/go-ascii-art"
|
|
||||||
"image"
|
"image"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -26,6 +16,18 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/server"
|
||||||
|
|
||||||
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
|
"github.com/rifflock/lfshook"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
easy "github.com/t-tomalak/logrus-easy-formatter"
|
||||||
|
asciiart "github.com/yinghau76/go-ascii-art"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -144,7 +146,7 @@ func main() {
|
|||||||
switch conf.LogLevel {
|
switch conf.LogLevel {
|
||||||
case "warn":
|
case "warn":
|
||||||
logPathMap = lfshook.PathMap{
|
logPathMap = lfshook.PathMap{
|
||||||
log.WarnLevel: path.Join("logs", date+"-warn.log"),
|
log.WarnLevel: path.Join("logs", date+"-warn.log"),
|
||||||
log.ErrorLevel: path.Join("logs", date+"-warn.log"),
|
log.ErrorLevel: path.Join("logs", date+"-warn.log"),
|
||||||
log.FatalLevel: path.Join("logs", date+"-warn.log"),
|
log.FatalLevel: path.Join("logs", date+"-warn.log"),
|
||||||
log.PanicLevel: path.Join("logs", date+"-warn.log"),
|
log.PanicLevel: path.Join("logs", date+"-warn.log"),
|
||||||
@ -157,7 +159,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
logPathMap = lfshook.PathMap{
|
logPathMap = lfshook.PathMap{
|
||||||
log.WarnLevel: path.Join("logs", date+"-warn.log"),
|
log.WarnLevel: path.Join("logs", date+"-warn.log"),
|
||||||
log.ErrorLevel: path.Join("logs", date+"-warn.log"),
|
log.ErrorLevel: path.Join("logs", date+"-warn.log"),
|
||||||
log.FatalLevel: path.Join("logs", date+"-warn.log"),
|
log.FatalLevel: path.Join("logs", date+"-warn.log"),
|
||||||
log.PanicLevel: path.Join("logs", date+"-warn.log"),
|
log.PanicLevel: path.Join("logs", date+"-warn.log"),
|
||||||
@ -278,24 +280,39 @@ func main() {
|
|||||||
log.Info("资源初始化完成, 开始处理信息.")
|
log.Info("资源初始化完成, 开始处理信息.")
|
||||||
log.Info("アトリは、高性能ですから!")
|
log.Info("アトリは、高性能ですから!")
|
||||||
cli.OnDisconnected(func(bot *client.QQClient, e *client.ClientDisconnectedEvent) {
|
cli.OnDisconnected(func(bot *client.QQClient, e *client.ClientDisconnectedEvent) {
|
||||||
if conf.ReLogin {
|
if conf.ReLogin.Enabled {
|
||||||
log.Warnf("Bot已离线 (%v),将在 %v 秒后尝试重连.", e.Message, conf.ReLoginDelay)
|
var times uint = 1
|
||||||
time.Sleep(time.Second * time.Duration(conf.ReLoginDelay))
|
for {
|
||||||
rsp, err := cli.Login()
|
|
||||||
if err != nil {
|
if conf.ReLogin.MaxReloginTimes == 0 {
|
||||||
log.Fatalf("重连失败: %v", err)
|
} else if times > conf.ReLogin.MaxReloginTimes {
|
||||||
}
|
break
|
||||||
if !rsp.Success {
|
|
||||||
switch rsp.Error {
|
|
||||||
case client.NeedCaptcha:
|
|
||||||
log.Fatalf("重连失败: 需要验证码. (验证码处理正在开发中)")
|
|
||||||
case client.UnsafeDeviceError:
|
|
||||||
log.Fatalf("重连失败: 设备锁")
|
|
||||||
default:
|
|
||||||
log.Fatalf("重连失败: %v", rsp.ErrorMessage)
|
|
||||||
}
|
}
|
||||||
|
log.Warnf("Bot已离线 (%v),将在 %v 秒后尝试重连. 重连次数:%v",
|
||||||
|
e.Message, conf.ReLogin.ReLoginDelay, times)
|
||||||
|
times++
|
||||||
|
time.Sleep(time.Second * time.Duration(conf.ReLogin.ReLoginDelay))
|
||||||
|
rsp, err := cli.Login()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("重连失败: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !rsp.Success {
|
||||||
|
switch rsp.Error {
|
||||||
|
case client.NeedCaptcha:
|
||||||
|
log.Fatalf("重连失败: 需要验证码. (验证码处理正在开发中)")
|
||||||
|
case client.UnsafeDeviceError:
|
||||||
|
log.Fatalf("重连失败: 设备锁")
|
||||||
|
default:
|
||||||
|
log.Errorf("重连失败: %v", rsp.ErrorMessage)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Info("重连成功")
|
||||||
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
log.Fatal("重连失败: 重连次数达到设置的上限值")
|
||||||
}
|
}
|
||||||
b.Release()
|
b.Release()
|
||||||
log.Fatalf("Bot已离线:%v", e.Message)
|
log.Fatalf("Bot已离线:%v", e.Message)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user