Skip to content

Commit

Permalink
减少太多请求错误
Browse files Browse the repository at this point in the history
  • Loading branch information
yxw21 committed Dec 25, 2022
1 parent ee1a0d5 commit 3ecf828
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
7 changes: 1 addition & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type Config struct {
FriendAddPolicy string
TokenFile string
DeviceFile string
MsgRetry int
}

var (
Instance *Config
Session *chatgpt.Session
Browser *chatgpt.Browser
Chats = make(map[int64]*chatgpt.Chat)
)

Expand All @@ -35,7 +35,6 @@ func init() {
FriendAddPolicy: os.Getenv("QQ_CHAT_GPT_POLICY"),
TokenFile: "qq.token",
DeviceFile: "device.json",
MsgRetry: 3,
}
if Instance.AIUsername != "" && Instance.AIPassword != "" && Instance.Key != "" {
Session = chatgpt.NewSessionWithCredential(Instance.AIUsername, Instance.AIPassword, Instance.Key).AutoRefresh()
Expand All @@ -46,8 +45,4 @@ func init() {
if err == nil {
Instance.QQ = qq
}
msgRetry, err := strconv.Atoi(os.Getenv("QQ_MSG_RETRY"))
if err == nil && msgRetry > 0 {
Instance.MsgRetry = msgRetry
}
}
24 changes: 10 additions & 14 deletions event/groupmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@ func GroupMessage(client *client.QQClient, event *message.GroupMessage) {
isAt, content := helpers.GetTextElementFromElements(client.Uin, event.Elements)
if isAt {
if _, ok := config.Chats[event.Sender.Uin]; !ok {
config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Session)
config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Browser, config.Session)
}
for i := 0; i < config.Instance.MsgRetry; i++ {
res, err := config.Chats[event.Sender.Uin].Send(content)
if err == nil {
client.SendGroupMessage(event.GroupCode, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(res.Message.Content.Parts[0])},
})
break
}
if i == config.Instance.MsgRetry-1 {
client.SendGroupMessage(event.GroupCode, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(err.Error())},
})
}
res, err := config.Chats[event.Sender.Uin].Send(content)
if err != nil {
client.SendGroupMessage(event.GroupCode, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(err.Error())},
})
} else {
client.SendGroupMessage(event.GroupCode, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewAt(event.Sender.Uin), message.NewText(res.Message.Content.Parts[0])},
})
}
}
}
24 changes: 10 additions & 14 deletions event/privatemessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ func PrivateMessage(client *client.QQClient, event *message.PrivateMessage) {
return
}
if _, ok := config.Chats[event.Sender.Uin]; !ok {
config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Session)
config.Chats[event.Sender.Uin] = chatgpt.NewChat(config.Browser, config.Session)
}
for i := 0; i < config.Instance.MsgRetry; i++ {
res, err := config.Chats[event.Sender.Uin].Send(content)
if err == nil {
client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewText(res.Message.Content.Parts[0])},
})
break
}
if i == config.Instance.MsgRetry-1 {
client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewText(err.Error())},
})
}
res, err := config.Chats[event.Sender.Uin].Send(content)
if err != nil {
client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewText(err.Error())},
})
} else {
client.SendPrivateMessage(event.Sender.Uin, &message.SendingMessage{
Elements: []message.IMessageElement{message.NewText(res.Message.Content.Parts[0])},
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f
github.com/Mrs4s/MiraiGo v0.0.0-20221206193101-cf66e2864798
github.com/tuotoo/qrcode v0.0.0-20220425170535-52ccc2bebf5d
github.com/yxw21/chatgpt v0.6.0
github.com/yxw21/chatgpt v0.7.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/yxw21/chatgpt v0.6.0 h1:lUPHh7fgH46UIw2G2oaFg7I/GFrnuWHCdXsXrVMG0dA=
github.com/yxw21/chatgpt v0.6.0/go.mod h1:40HZqYFtcZoUNXsG0LKCgzea+KHdW1XJ7MnVvhFm8z4=
github.com/yxw21/chatgpt v0.7.0 h1:7NVjO6+pyXof340nrFR1eXd8KcbDP/PRBXuzieKxMEA=
github.com/yxw21/chatgpt v0.7.0/go.mod h1:40HZqYFtcZoUNXsG0LKCgzea+KHdW1XJ7MnVvhFm8z4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
Expand Down
7 changes: 7 additions & 0 deletions qqchatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"github.com/Mrs4s/MiraiGo/client"
"github.com/yxw21/chatgpt"
"log"
"qqgpt/config"
"qqgpt/event"
Expand All @@ -14,6 +15,12 @@ func main() {
qq *client.QQClient
err error
)
browser, closeBrowser, err := chatgpt.NewBrowser("")
if err != nil {
log.Fatal(err)
}
defer closeBrowser()
config.Browser = browser
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err = helpers.AutoLoadDevice(); err != nil {
Expand Down

0 comments on commit 3ecf828

Please sign in to comment.