From 747341be8db05e8050810b096a9c1a7580a1ffba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E6=9F=B3=E7=85=9C?= <101934327+fangliuyu@users.noreply.github.com> Date: Mon, 5 Sep 2022 22:45:12 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E6=9D=83=E9=99=90=EF=BC=8C=E9=98=B2=E6=AD=A2=E6=B3=9B=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/ai_reply/ai_tts.go | 61 +++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index f9b9ef99ad..5396c9f299 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -125,8 +125,16 @@ func init() { "- @Bot 任意文本(任意一句话回复)\n" + "- 设置语音模式[原神人物]\n" + "- 设置默认语音模式[原神人物]\n" + + "- 恢复成默认语音模式\n" + "当前适用的原神人物含有以下:\n" + list(soundList[:], 5), }) + tts.RLock() + defer tts.RUnlock() + m, ok := control.Lookup(ttsServiceName) + if ok { + // m.Enable(-20220905) + tts.defaultSoundMode = soundList[m.GetData(-20220905)] + } engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { msg := ctx.ExtractPlainText() @@ -154,13 +162,14 @@ func init() { ))).Add("cache", 0) if record.Data == nil { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply)) + return } // 发送语音 if ID := ctx.SendChain(record); ID.ID() == 0 { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply)) } }) - engine.OnRegex(`^设置语音模式(.*)$`, func(ctx *zero.Ctx) bool { + engine.OnRegex(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool { param := ctx.State["regex_matched"].([]string)[1] if _, ok := testRecord[param]; !ok { return false @@ -188,7 +197,7 @@ func init() { time.Sleep(time.Second * 2) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,默认模式为", name)) }) - engine.OnRegex(`^设置默认语音模式(.*)$`, func(ctx *zero.Ctx) bool { + engine.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool { param := ctx.State["regex_matched"].([]string)[1] if _, ok := testRecord[param]; !ok { return false @@ -197,7 +206,11 @@ func init() { }).SetBlock(true).Handle(func(ctx *zero.Ctx) { param := ctx.State["regex_matched"].([]string)[1] // 保存设置 - tts.setDefaultSoundMode(param) + err := tts.setDefaultSoundMode(param) + if err != nil { + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err)) + return + } // 设置验证 name := tts.defaultSoundMode record := message.Record(fmt.Sprintf(cnapi, url.QueryEscape(name), url.QueryEscape(testRecord[name]))).Add("cache", 0) @@ -208,6 +221,36 @@ func init() { time.Sleep(time.Second * 2) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,默认模式为", name)) }) + engine.OnFullMatch("恢复成默认语音模式", zero.AdminPermission, func(ctx *zero.Ctx) bool { + gid := ctx.Event.GroupID + if gid == 0 { + gid = -ctx.Event.UserID + } + m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) + if ok { + tts.RLock() + defer tts.RUnlock() + index := m.GetData(gid) + if int(index) < len(tts.soundMode) { + return true + } + } + return false + }).SetBlock(true).Handle(func(ctx *zero.Ctx) { + gid := ctx.Event.GroupID + if gid == 0 { + gid = -ctx.Event.UserID + } + m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]) + err := m.SetData(gid, 255) + if err != nil { + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err)) + return + } + // 设置验证 + name := tts.getSoundMode(ctx) + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,当前默认语音模式为", name)) + }) } func (tts *ttsInstances) setSoundMode(ctx *zero.Ctx, name string) error { @@ -245,7 +288,7 @@ func (tts *ttsInstances) getSoundMode(ctx *zero.Ctx) (name string) { return tts.defaultSoundMode } -func (tts *ttsInstances) setDefaultSoundMode(name string) { +func (tts *ttsInstances) setDefaultSoundMode(name string) error { var index int64 tts.RLock() for i, s := range tts.soundMode { @@ -254,6 +297,14 @@ func (tts *ttsInstances) setDefaultSoundMode(name string) { break } } - tts.defaultSoundMode = tts.soundMode[index] tts.RUnlock() + m, ok := control.Lookup(ttsServiceName) + if !ok { + return fmt.Errorf("[error]no fund service") + } + err := m.SetData(-20220905, index) + if err == nil { + tts.defaultSoundMode = tts.soundMode[m.GetData(-20220905)] + } + return err } From d472bdd1159a7ec085042ea02809dd75b3f825ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E6=9F=B3=E7=85=9C?= <101934327+fangliuyu@users.noreply.github.com> Date: Mon, 5 Sep 2022 22:50:09 +0800 Subject: [PATCH 2/4] Update ai_tts.go --- plugin/ai_reply/ai_tts.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index 5396c9f299..06feb05ebe 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -1,6 +1,7 @@ package aireply import ( + "errors" "fmt" "net/url" "regexp" @@ -300,7 +301,7 @@ func (tts *ttsInstances) setDefaultSoundMode(name string) error { tts.RUnlock() m, ok := control.Lookup(ttsServiceName) if !ok { - return fmt.Errorf("[error]no fund service") + return errors.New("[error]no fund service") } err := m.SetData(-20220905, index) if err == nil { From ba05b0461fe6f8b10da42b4ee16d2824e7a6add9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E6=9F=B3=E7=85=9C?= <101934327+fangliuyu@users.noreply.github.com> Date: Sun, 11 Sep 2022 11:12:32 +0800 Subject: [PATCH 3/4] Update ai_tts.go --- plugin/ai_reply/ai_tts.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index 06feb05ebe..66ec1cbf68 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -133,8 +133,7 @@ func init() { defer tts.RUnlock() m, ok := control.Lookup(ttsServiceName) if ok { - // m.Enable(-20220905) - tts.defaultSoundMode = soundList[m.GetData(-20220905)] + tts.defaultSoundMode = soundList[m.GetData(-2905)] } engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { @@ -303,9 +302,13 @@ func (tts *ttsInstances) setDefaultSoundMode(name string) error { if !ok { return errors.New("[error]no fund service") } - err := m.SetData(-20220905, index) + err := m.SetData(-2905, index) if err == nil { - tts.defaultSoundMode = tts.soundMode[m.GetData(-20220905)] + soundMode := tts.soundMode[m.GetData(-2905)] + if soundMode != tts.soundMode[index] { + return errors.New("检验数据错误\n当前写入的数据为" + soundMode) + } + tts.defaultSoundMode = soundMode } return err } From 911866f9432c92104d865f7d76e95f5301e6ddc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E6=9F=B3=E7=85=9C?= <101934327+fangliuyu@users.noreply.github.com> Date: Sun, 11 Sep 2022 18:56:50 +0800 Subject: [PATCH 4/4] Update ai_tts.go --- plugin/ai_reply/ai_tts.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/ai_reply/ai_tts.go b/plugin/ai_reply/ai_tts.go index 66ec1cbf68..44f39bc333 100644 --- a/plugin/ai_reply/ai_tts.go +++ b/plugin/ai_reply/ai_tts.go @@ -300,13 +300,13 @@ func (tts *ttsInstances) setDefaultSoundMode(name string) error { tts.RUnlock() m, ok := control.Lookup(ttsServiceName) if !ok { - return errors.New("[error]no fund service") + return errors.New("[tts]service no fund ") } err := m.SetData(-2905, index) if err == nil { soundMode := tts.soundMode[m.GetData(-2905)] if soundMode != tts.soundMode[index] { - return errors.New("检验数据错误\n当前写入的数据为" + soundMode) + return errors.New("[tts]检验数据失败,当前写入的数据为" + soundMode) } tts.defaultSoundMode = soundMode }