diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index a69c5eb3..ab787135 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -2,6 +2,7 @@ package aireply import ( "errors" + "net/url" "regexp" "sync" @@ -131,9 +132,9 @@ func getReplyMode(ctx *zero.Ctx) (name string) { ***********************tts************************************ *************************************************************/ type ttsmode struct { - sync.RWMutex - apikey string - mode map[int64]int64 + sync.RWMutex `json:"-"` + APIKey string + mode map[int64]int64 } func list(list []string, num int) string { @@ -165,8 +166,16 @@ func newttsmode() *ttsmode { return tts } -func (tts *ttsmode) getAPIKey() string { - return tts.apikey +func (tts *ttsmode) getAPIKey(ctx *zero.Ctx) string { + if tts.APIKey == "" { + m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) + gid := ctx.Event.GroupID + if gid == 0 { + gid = -ctx.Event.UserID + } + _ = m.Manager.GetExtra(gid, &tts) + } + return url.QueryEscape(tts.APIKey) } func (tts *ttsmode) setAPIKey(ctx *zero.Ctx, key string) error { @@ -179,7 +188,7 @@ func (tts *ttsmode) setAPIKey(ctx *zero.Ctx, key string) error { if err != nil { return errors.New("内部错误") } - tts.apikey = key + tts.APIKey = key return nil } diff --git a/plugin/ai_reply/main.go b/plugin/ai_reply/main.go index 7ff19268..30207c92 100644 --- a/plugin/ai_reply/main.go +++ b/plugin/ai_reply/main.go @@ -27,6 +27,7 @@ func init() { // 插件主体 "- 设置语音模式[原神人物]\n" + "- 设置默认语音模式[原神人物]\n" + "- 恢复成默认语音模式\n" + + "- 设置原神语音 api key xxxxxx\n" + "当前适用的原神人物含有以下:\n" + list(soundList[:], 5), }) tts := newttsmode() @@ -82,7 +83,7 @@ func init() { // 插件主体 } return numcn.EncodeFromFloat64(f) }), - ), tts.getAPIKey())).Add("cache", 0) + ), tts.getAPIKey(ctx))) // 发送语音 if ID := ctx.SendChain(record); ID.ID() == 0 { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply)) @@ -108,7 +109,7 @@ func init() { // 插件主体 ctx.SendChain(message.Text("配置的语音人物数据丢失!请重新设置语音人物。")) return } - record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]), tts.getAPIKey())).Add("cache", 0) + record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]), tts.getAPIKey(ctx))).Add("cache", 0) if ID := ctx.SendChain(record); ID.ID() == 0 { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语音,请重试。")) return diff --git a/plugin/moegoe/main.go b/plugin/moegoe/main.go index 6cff5aeb..2a0df85f 100644 --- a/plugin/moegoe/main.go +++ b/plugin/moegoe/main.go @@ -54,7 +54,9 @@ func init() { ctx.SendChain(message.Text("ERROR: plugin tts not found")) return } - var key string + var key struct { + APIKey string + } gid := ctx.Event.GroupID if gid == 0 { gid = -ctx.Event.UserID @@ -64,6 +66,6 @@ func init() { ctx.SendChain(message.Text("ERROR: ", err)) return } - ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id, key))) + ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id, url.QueryEscape(key.APIKey)))) }) }