|
| 1 | +// Package airecord 群应用:AI声聊 |
| 2 | +package airecord |
| 3 | + |
| 4 | +import ( |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/tidwall/gjson" |
| 10 | + |
| 11 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 12 | + "github.com/wdvxdr1123/ZeroBot/message" |
| 13 | + |
| 14 | + "github.com/FloatTech/AnimeAPI/airecord" |
| 15 | + ctrl "github.com/FloatTech/zbpctrl" |
| 16 | + "github.com/FloatTech/zbputils/control" |
| 17 | +) |
| 18 | + |
| 19 | +func init() { |
| 20 | + en := control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
| 21 | + DisableOnDefault: false, |
| 22 | + Extra: control.ExtraFromString("airecord"), |
| 23 | + Brief: "群应用:AI声聊", |
| 24 | + Help: "- 设置AI语音群号1048452984(tips:机器人任意所在群聊即可)\n" + |
| 25 | + "- 设置AI语音模型\n" + |
| 26 | + "- 查看AI语音配置\n" + |
| 27 | + "- 发送AI语音xxx", |
| 28 | + PrivateDataFolder: "airecord", |
| 29 | + }) |
| 30 | + |
| 31 | + en.OnPrefix("设置AI语音群号", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true). |
| 32 | + Handle(func(ctx *zero.Ctx) { |
| 33 | + u := strings.TrimSpace(ctx.State["args"].(string)) |
| 34 | + num, err := strconv.ParseInt(u, 10, 64) |
| 35 | + if err != nil { |
| 36 | + ctx.SendChain(message.Text("ERROR: parse gid err: ", err)) |
| 37 | + return |
| 38 | + } |
| 39 | + err = airecord.SetCustomGID(num) |
| 40 | + if err != nil { |
| 41 | + ctx.SendChain(message.Text("ERROR: set gid err: ", err)) |
| 42 | + return |
| 43 | + } |
| 44 | + ctx.SendChain(message.Text("设置AI语音群号为", num)) |
| 45 | + }) |
| 46 | + en.OnFullMatch("设置AI语音模型", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true). |
| 47 | + Handle(func(ctx *zero.Ctx) { |
| 48 | + next := zero.NewFutureEvent("message", 999, false, ctx.CheckSession()) |
| 49 | + recv, cancel := next.Repeat() |
| 50 | + defer cancel() |
| 51 | + jsonData := ctx.GetAICharacters(0, 1) |
| 52 | + |
| 53 | + // 转换为字符串数组 |
| 54 | + var names []string |
| 55 | + // 初始化两个映射表 |
| 56 | + nameToID := make(map[string]string) |
| 57 | + nameToURL := make(map[string]string) |
| 58 | + characters := jsonData.Get("#.characters") |
| 59 | + |
| 60 | + // 遍历每个角色对象 |
| 61 | + characters.ForEach(func(_, group gjson.Result) bool { |
| 62 | + group.ForEach(func(_, character gjson.Result) bool { |
| 63 | + // 提取当前角色的三个字段 |
| 64 | + name := character.Get("character_name").String() |
| 65 | + names = append(names, name) |
| 66 | + // 存入映射表(重复名称会覆盖,保留最后出现的条目) |
| 67 | + nameToID[name] = character.Get("character_id").String() |
| 68 | + nameToURL[name] = character.Get("preview_url").String() |
| 69 | + return true // 继续遍历 |
| 70 | + }) |
| 71 | + return true // 继续遍历 |
| 72 | + }) |
| 73 | + var builder strings.Builder |
| 74 | + // 写入开头文本 |
| 75 | + builder.WriteString("请选择语音模型序号:\n") |
| 76 | + |
| 77 | + // 遍历names数组,拼接序号和名称 |
| 78 | + for i, v := range names { |
| 79 | + // 将数字转换为字符串(不依赖fmt) |
| 80 | + numStr := strconv.Itoa(i) |
| 81 | + // 拼接格式:"序号. 名称\n" |
| 82 | + builder.WriteString(numStr) |
| 83 | + builder.WriteString(". ") |
| 84 | + builder.WriteString(v) |
| 85 | + builder.WriteString("\n") |
| 86 | + } |
| 87 | + // 获取最终字符串 |
| 88 | + ctx.SendChain(message.Text(builder.String())) |
| 89 | + for { |
| 90 | + select { |
| 91 | + case <-time.After(time.Second * 120): |
| 92 | + ctx.SendChain(message.Text("设置AI语音模型指令过期")) |
| 93 | + return |
| 94 | + case ct := <-recv: |
| 95 | + msg := ct.Event.Message.ExtractPlainText() |
| 96 | + num, err := strconv.Atoi(msg) |
| 97 | + if err != nil { |
| 98 | + ctx.SendChain(message.Text("请输入数字!")) |
| 99 | + continue |
| 100 | + } |
| 101 | + if num < 0 || num >= len(names) { |
| 102 | + ctx.SendChain(message.Text("序号非法!")) |
| 103 | + continue |
| 104 | + } |
| 105 | + err = airecord.SetRecordModel(names[num], nameToID[names[num]]) |
| 106 | + if err != nil { |
| 107 | + ctx.SendChain(message.Text("ERROR: set model err: ", err)) |
| 108 | + continue |
| 109 | + } |
| 110 | + ctx.SendChain(message.Text("已选择语音模型: ", names[num])) |
| 111 | + ctx.SendChain(message.Record(nameToURL[names[num]])) |
| 112 | + return |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | + en.OnFullMatch("查看AI语音配置", zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true). |
| 117 | + Handle(func(ctx *zero.Ctx) { |
| 118 | + ctx.SendChain(message.Text(airecord.PrintRecordConfig())) |
| 119 | + }) |
| 120 | + en.OnPrefix("发送AI语音", zero.UserOrGrpAdmin).SetBlock(true). |
| 121 | + Handle(func(ctx *zero.Ctx) { |
| 122 | + u := strings.TrimSpace(ctx.State["args"].(string)) |
| 123 | + recCfg := airecord.GetConfig() |
| 124 | + record := ctx.GetAIRecord(recCfg.ModelID, recCfg.Customgid, u) |
| 125 | + if record == "" { |
| 126 | + id := ctx.SendGroupAIRecord(recCfg.ModelID, ctx.Event.GroupID, u) |
| 127 | + if id == "" { |
| 128 | + ctx.SendChain(message.Text("ERROR: get record err: empty record")) |
| 129 | + return |
| 130 | + } |
| 131 | + } |
| 132 | + ctx.SendChain(message.Record(record)) |
| 133 | + }) |
| 134 | +} |
0 commit comments