mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-06 15:20:22 +00:00
fix #525: chatgpt add UA/CF config
This commit is contained in:
@@ -35,7 +35,7 @@ func init() { // 插件主体
|
||||
enr := control.Register("aireply", &ctrl.Options[*zero.Ctx]{
|
||||
DisableOnDefault: false,
|
||||
Brief: "人工智能回复",
|
||||
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱|ChatGPT]\n- 设置 ChatGPT SessionToken xxx\n- 重置ChatGPT连接",
|
||||
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱|ChatGPT]\n- 设置 ChatGPT SessionToken xxx\n- 设置 ChatGPT UA xxx\n- 设置 ChatGPT CF xxx\n- 重置ChatGPT连接",
|
||||
PrivateDataFolder: "aireply",
|
||||
})
|
||||
|
||||
@@ -64,19 +64,28 @@ func init() { // 插件主体
|
||||
})
|
||||
|
||||
chatgptfile := enr.DataFolder() + "chatgpt.txt"
|
||||
uafile := enr.DataFolder() + "ua.txt"
|
||||
cffile := enr.DataFolder() + "cf.txt"
|
||||
cfg := &chatgpt.Config{
|
||||
UA: chatgpt.UA,
|
||||
RefreshInterval: time.Hour,
|
||||
Timeout: time.Minute,
|
||||
}
|
||||
data, err := os.ReadFile(chatgptfile)
|
||||
if err == nil {
|
||||
cfg.SessionToken = binary.BytesToString(data)
|
||||
chats = aireply.NewChatGPT(cfg)
|
||||
data, err = os.ReadFile(uafile)
|
||||
if err == nil {
|
||||
cfg.UA = binary.BytesToString(data)
|
||||
data, err = os.ReadFile(cffile)
|
||||
if err == nil {
|
||||
cfg.CFClearance = binary.BytesToString(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
chats = aireply.NewChatGPT(cfg)
|
||||
go func() {
|
||||
for range time.NewTicker(time.Hour).C {
|
||||
if chats == nil {
|
||||
if chats == nil || cfg.SessionToken == "" {
|
||||
continue
|
||||
}
|
||||
err := os.WriteFile(chatgptfile, binary.StringToBytes(cfg.SessionToken), 0644)
|
||||
@@ -99,16 +108,49 @@ func init() { // 插件主体
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
chats = aireply.NewChatGPT(&chatgpt.Config{
|
||||
UA: chatgpt.UA,
|
||||
SessionToken: token,
|
||||
RefreshInterval: time.Hour,
|
||||
Timeout: time.Minute,
|
||||
})
|
||||
cfg.SessionToken = token
|
||||
ctx.SendChain(message.Text("设置成功"))
|
||||
})
|
||||
|
||||
enr.OnRegex(`^设置\s*ChatGPT\s*UA\s*(.*)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||
ua := ctx.State["regex_matched"].([]string)[1]
|
||||
f, err := os.Create(uafile)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = f.WriteString(ua)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
cfg.UA = ua
|
||||
ctx.SendChain(message.Text("设置成功"))
|
||||
})
|
||||
|
||||
enr.OnRegex(`^设置\s*ChatGPT\s*CF\s*(.*)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||
cf := ctx.State["regex_matched"].([]string)[1]
|
||||
f, err := os.Create(cffile)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = f.WriteString(cf)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
cfg.CFClearance = cf
|
||||
ctx.SendChain(message.Text("设置成功"))
|
||||
})
|
||||
|
||||
enr.OnFullMatch("重置ChatGPT连接").SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||
if chats == nil {
|
||||
ctx.SendChain(message.Text("ERROR: chats 为空"))
|
||||
return
|
||||
}
|
||||
err := chats.Reset(ctx.Event.UserID)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
|
||||
Reference in New Issue
Block a user