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