diff --git a/plugin_ai_reply/tts.go b/plugin_ai_reply/tts.go index 61c03e68..86ab7e65 100644 --- a/plugin_ai_reply/tts.go +++ b/plugin_ai_reply/tts.go @@ -20,12 +20,20 @@ import ( "github.com/FloatTech/zbputils/control/order" ) -const ( - ttsServiceName = "tts" -) +const ttsServiceName = "tts" var ( - t *ttsInstances + t = &ttsInstances{ + m: map[string]tts.TTS{ + "百度女声": baidutts.NewBaiduTTS(0), + "百度男声": baidutts.NewBaiduTTS(1), + "百度度逍遥": baidutts.NewBaiduTTS(3), + "百度度丫丫": baidutts.NewBaiduTTS(4), + "拟声鸟阿梓": mockingbird.NewMockingBirdTTS(0), + "拟声鸟药水哥": mockingbird.NewMockingBirdTTS(1), + }, + l: []string{"拟声鸟阿梓", "拟声鸟药水哥", "百度女声", "百度男声", "百度度逍遥", "百度度丫丫"}, + } re = regexp.MustCompile(`(\-|\+)?\d+(\.\d+)?`) ) @@ -39,17 +47,6 @@ func (t *ttsInstances) List() []string { } func init() { - t = &ttsInstances{ - m: map[string]tts.TTS{ - "百度女声": baidutts.NewBaiduTTS(0), - "百度男声": baidutts.NewBaiduTTS(1), - "百度度逍遥": baidutts.NewBaiduTTS(3), - "百度度丫丫": baidutts.NewBaiduTTS(4), - "拟声鸟阿梓": mockingbird.NewMockingBirdTTS(0), - "拟声鸟药水哥": mockingbird.NewMockingBirdTTS(1), - }, - l: []string{"拟声鸟阿梓", "拟声鸟药水哥", "百度女声", "百度男声", "百度度逍遥", "百度度丫丫"}, - } engine := control.Register(ttsServiceName, order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "语音回复(包括拟声鸟和百度)\n- @Bot 任意文本(任意一句话回复)\n- 设置语音模式拟声鸟阿梓 | 设置语音模式拟声鸟药水哥 | 设置语音模式百度女声 | 设置语音模式百度男声| 设置语音模式百度度逍遥 | 设置语音模式百度度丫丫", @@ -58,7 +55,7 @@ func init() { Handle(func(ctx *zero.Ctx) { msg := ctx.ExtractPlainText() r := aireply.NewAIReply(getReplyMode(ctx)) - tts := newTTS(getSoundMode(ctx)) + tts := t.new(t.getSoundMode(ctx)) ctx.SendChain(message.Record(tts.Speak(ctx.Event.UserID, func() string { reply := r.TalkPlain(msg, zero.BotConfig.NickName[0]) reply = re.ReplaceAllStringFunc(reply, func(s string) string { @@ -76,7 +73,7 @@ func init() { engine.OnRegex(`^设置语音模式(.*)$`, ctxext.FirstValueInList(t)).SetBlock(true). Handle(func(ctx *zero.Ctx) { param := ctx.State["regex_matched"].([]string)[1] - err := setSoundMode(ctx, param) + err := t.setSoundMode(ctx, param) if err != nil { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err)) return @@ -85,12 +82,12 @@ func init() { }) } -// newTTS 语音简单工厂 -func newTTS(name string) tts.TTS { +// new 语音简单工厂 +func (t *ttsInstances) new(name string) tts.TTS { return t.m[name] } -func setSoundMode(ctx *zero.Ctx, name string) error { +func (t *ttsInstances) setSoundMode(ctx *zero.Ctx, name string) error { gid := ctx.Event.GroupID if gid == 0 { gid = -ctx.Event.UserID @@ -109,7 +106,7 @@ func setSoundMode(ctx *zero.Ctx, name string) error { return m.SetData(gid, index) } -func getSoundMode(ctx *zero.Ctx) (name string) { +func (t *ttsInstances) getSoundMode(ctx *zero.Ctx) (name string) { gid := ctx.Event.GroupID if gid == 0 { gid = -ctx.Event.UserID