diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index eeb4f46e..a69c5eb3 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -12,7 +12,7 @@ import ( ) const ( - cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&id=%d&text=%s" + cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&id=%d&text=%s&code=%s" ) // 每个角色的测试文案 @@ -132,7 +132,8 @@ func getReplyMode(ctx *zero.Ctx) (name string) { *************************************************************/ type ttsmode struct { sync.RWMutex - mode map[int64]int64 + apikey string + mode map[int64]int64 } func list(list []string, num int) string { @@ -164,6 +165,24 @@ func newttsmode() *ttsmode { return tts } +func (tts *ttsmode) getAPIKey() string { + return tts.apikey +} + +func (tts *ttsmode) setAPIKey(ctx *zero.Ctx, key string) error { + gid := ctx.Event.GroupID + if gid == 0 { + gid = -ctx.Event.UserID + } + m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) + err := m.Manager.SetExtra(gid, &key) + if err != nil { + return errors.New("内部错误") + } + tts.apikey = key + return nil +} + func (tts *ttsmode) setSoundMode(ctx *zero.Ctx, name string) error { gid := ctx.Event.GroupID if gid == 0 { diff --git a/plugin/ai_reply/main.go b/plugin/ai_reply/main.go index 23acf9de..7ff19268 100644 --- a/plugin/ai_reply/main.go +++ b/plugin/ai_reply/main.go @@ -20,7 +20,7 @@ import ( var replyModes = [...]string{"青云客", "小爱"} func init() { // 插件主体 - enOftts := control.Register("tts", &ctrl.Options[*zero.Ctx]{ + ent := control.Register("tts", &ctrl.Options[*zero.Ctx]{ DisableOnDefault: true, Brief: "人工智能语音回复", Help: "- @Bot 任意文本(任意一句话回复)\n" + @@ -30,7 +30,7 @@ func init() { // 插件主体 "当前适用的原神人物含有以下:\n" + list(soundList[:], 5), }) tts := newttsmode() - enOfreply := control.Register("aireply", &ctrl.Options[*zero.Ctx]{ + enr := control.Register("aireply", &ctrl.Options[*zero.Ctx]{ DisableOnDefault: false, Brief: "人工智能回复", Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]", @@ -38,7 +38,7 @@ func init() { // 插件主体 /************************************************************* *******************************AIreply************************ *************************************************************/ - enOfreply.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). + enr.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { aireply := aireply.NewAIReply(getReplyMode(ctx)) reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0])) @@ -51,7 +51,7 @@ func init() { // 插件主体 } ctx.Send(reply) }) - enOfreply.OnPrefix("设置回复模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { + enr.OnPrefix("设置回复模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { param := ctx.State["args"].(string) err := setReplyMode(ctx, param) if err != nil { @@ -63,7 +63,7 @@ func init() { // 插件主体 /************************************************************* ***********************tts************************************ *************************************************************/ - enOftts.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). + ent.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { msg := ctx.ExtractPlainText() // 获取回复模式 @@ -82,13 +82,13 @@ func init() { // 插件主体 } return numcn.EncodeFromFloat64(f) }), - ))).Add("cache", 0) + ), tts.getAPIKey())).Add("cache", 0) // 发送语音 if ID := ctx.SendChain(record); ID.ID() == 0 { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply)) } }) - enOftts.OnRegex(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool { + ent.OnRegex(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool { param := ctx.State["regex_matched"].([]string)[1] if _, ok := testRecord[param]; !ok { return false @@ -108,7 +108,7 @@ func init() { // 插件主体 ctx.SendChain(message.Text("配置的语音人物数据丢失!请重新设置语音人物。")) return } - record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]))).Add("cache", 0) + record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]), tts.getAPIKey())).Add("cache", 0) if ID := ctx.SendChain(record); ID.ID() == 0 { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语音,请重试。")) return @@ -116,7 +116,7 @@ func init() { // 插件主体 time.Sleep(time.Second * 2) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功")) }) - enOftts.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool { + ent.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool { param := ctx.State["regex_matched"].([]string)[1] if _, ok := testRecord[param]; !ok { return false @@ -132,7 +132,7 @@ func init() { // 插件主体 } ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功")) }) - enOftts.OnFullMatch("恢复成默认语音模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { + ent.OnFullMatch("恢复成默认语音模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { err := tts.resetSoundMode(ctx) if err != nil { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err)) @@ -142,4 +142,12 @@ func init() { // 插件主体 index := tts.getSoundMode(ctx) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,当前为", soundList[index])) }) + ent.OnRegex(`^设置原神语音\s*api\s*key\s*([0-9a-zA-Z-_]{54}==)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { + err := tts.setAPIKey(ctx, ctx.State["regex_matched"].([]string)[1]) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + ctx.SendChain(message.Text("设置成功")) + }) } diff --git a/plugin/moegoe/main.go b/plugin/moegoe/main.go index 83d189fb..6cff5aeb 100644 --- a/plugin/moegoe/main.go +++ b/plugin/moegoe/main.go @@ -16,7 +16,7 @@ import ( const ( jpapi = "https://moegoe.azurewebsites.net/api/speak?text=%s&id=%d" krapi = "https://moegoe.azurewebsites.net/api/speakkr?text=%s&id=%d" - cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&text=%s&id=%d" + cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&text=%s&id=%d&code=%s" ) var speakers = map[string]uint{ @@ -49,6 +49,21 @@ func init() { Handle(func(ctx *zero.Ctx) { text := ctx.State["regex_matched"].([]string)[2] id := speakers[ctx.State["regex_matched"].([]string)[1]] - ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id))) + c, ok := control.Lookup("tts") + if !ok { + ctx.SendChain(message.Text("ERROR: plugin tts not found")) + return + } + var key string + gid := ctx.Event.GroupID + if gid == 0 { + gid = -ctx.Event.UserID + } + err := c.Manager.GetExtra(gid, &key) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id, key))) }) }