22package aireply
33
44import (
5- "errors"
65 "fmt"
76 "net/url"
87 "strconv"
@@ -26,7 +25,7 @@ const (
2625var replyModes = [... ]string {"青云客" , "小爱" }
2726
2827func init () { // 插件主体
29- engine := control .Register (ttsServiceName , & ctrl.Options [* zero.Ctx ]{
28+ enOftts := control .Register (ttsServiceName , & ctrl.Options [* zero.Ctx ]{
3029 DisableOnDefault : true ,
3130 Help : "语音回复(大家一起来炼丹)\n " +
3231 "- @Bot 任意文本(任意一句话回复)\n " +
@@ -36,15 +35,49 @@ func init() { // 插件主体
3635 "当前适用的原神人物含有以下:\n " + list (soundList [:], 5 ),
3736 })
3837 tts := newttsmode ()
39- engine .OnMessage (zero .OnlyToMe ).SetBlock (true ).Limit (ctxext .LimitByUser ).
38+ enOfreply := control .Register (replyServiceName , & ctrl.Options [* zero.Ctx ]{
39+ DisableOnDefault : false ,
40+ Help : "人工智能回复\n " +
41+ "- @Bot 任意文本(任意一句话回复)\n - 设置回复模式[青云客|小爱]" ,
42+ })
43+ /*************************************************************
44+ *******************************AIreply************************
45+ *************************************************************/
46+ enOfreply .OnMessage (zero .OnlyToMe ).SetBlock (true ).Limit (ctxext .LimitByUser ).
47+ Handle (func (ctx * zero.Ctx ) {
48+ aireply := aireply .NewAIReply (getReplyMode (ctx ))
49+ reply := message .ParseMessageFromString (aireply .Talk (ctx .ExtractPlainText (), zero .BotConfig .NickName [0 ]))
50+ // 回复
51+ time .Sleep (time .Second * 1 )
52+ if zero .OnlyPublic (ctx ) {
53+ reply = append (reply , message .Reply (ctx .Event .MessageID ))
54+ ctx .Send (reply )
55+ return
56+ }
57+ ctx .Send (reply )
58+ })
59+ enOfreply .OnPrefix ("设置回复模式" , zero .AdminPermission ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
60+ param := ctx .State ["args" ].(string )
61+ err := setReplyMode (ctx , param )
62+ if err != nil {
63+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (err ))
64+ return
65+ }
66+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("成功" ))
67+ })
68+ /*************************************************************
69+ ***********************tts************************************
70+ *************************************************************/
71+ enOftts .OnMessage (zero .OnlyToMe ).SetBlock (true ).Limit (ctxext .LimitByUser ).
4072 Handle (func (ctx * zero.Ctx ) {
4173 msg := ctx .ExtractPlainText ()
4274 // 获取回复模式
4375 r := aireply .NewAIReply (getReplyMode (ctx ))
4476 // 获取回复的文本
4577 reply := r .TalkPlain (msg , zero .BotConfig .NickName [0 ])
4678 // 获取语音
47- record := message .Record (fmt .Sprintf (cnapi , tts .getSoundMode (ctx ), url .QueryEscape (
79+ index := tts .getSoundMode (ctx )
80+ record := message .Record (fmt .Sprintf (cnapi , index , url .QueryEscape (
4881 // 将数字转文字
4982 re .ReplaceAllStringFunc (reply , func (s string ) string {
5083 f , err := strconv .ParseFloat (s , 64 )
@@ -55,16 +88,12 @@ func init() { // 插件主体
5588 return numcn .EncodeFromFloat64 (f )
5689 }),
5790 ))).Add ("cache" , 0 )
58- if record .Data == nil {
59- ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (reply ))
60- return
61- }
6291 // 发送语音
6392 if ID := ctx .SendChain (record ); ID .ID () == 0 {
6493 ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (reply ))
6594 }
6695 })
67- engine .OnRegex (`^设置语音模式(.*)$` , zero .AdminPermission , func (ctx * zero.Ctx ) bool {
96+ enOftts .OnRegex (`^设置语音模式(.*)$` , zero .AdminPermission , func (ctx * zero.Ctx ) bool {
6897 param := ctx .State ["regex_matched" ].([]string )[1 ]
6998 if _ , ok := testRecord [param ]; ! ok {
7099 return false
@@ -92,7 +121,7 @@ func init() { // 插件主体
92121 time .Sleep (time .Second * 2 )
93122 ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("设置成功" ))
94123 })
95- engine .OnRegex (`^设置默认语音模式(.*)$` , zero .SuperUserPermission , func (ctx * zero.Ctx ) bool {
124+ enOftts .OnRegex (`^设置默认语音模式(.*)$` , zero .SuperUserPermission , func (ctx * zero.Ctx ) bool {
96125 param := ctx .State ["regex_matched" ].([]string )[1 ]
97126 if _ , ok := testRecord [param ]; ! ok {
98127 return false
@@ -101,85 +130,21 @@ func init() { // 插件主体
101130 }).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
102131 param := ctx .State ["regex_matched" ].([]string )[1 ]
103132 // 保存设置
104- err := setDefaultSoundMode (param )
133+ err := tts . setDefaultSoundMode (param )
105134 if err != nil {
106135 ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (err ))
107136 return
108137 }
109138 ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("设置成功" ))
110139 })
111- engine .OnFullMatch ("恢复成默认语音模式" , zero .AdminPermission ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
112- tts .resetSoundMode (ctx )
140+ enOftts .OnFullMatch ("恢复成默认语音模式" , zero .AdminPermission ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
141+ err := tts .resetSoundMode (ctx )
142+ if err != nil {
143+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (err ))
144+ return
145+ }
113146 // 设置验证
114- name := tts .getSoundMode (ctx )
115- ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("设置成功,当前为" , name ))
147+ index := tts .getSoundMode (ctx )
148+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("设置成功,当前为" , soundList [ index ] ))
116149 })
117- engine = control .Register (replyServiceName , & ctrl.Options [* zero.Ctx ]{
118- DisableOnDefault : false ,
119- Help : "人工智能回复\n " +
120- "- @Bot 任意文本(任意一句话回复)\n - 设置回复模式[青云客|小爱]" ,
121- })
122- // 回复 @和包括名字
123- engine .OnMessage (zero .OnlyToMe ).SetBlock (true ).Limit (ctxext .LimitByUser ).
124- Handle (func (ctx * zero.Ctx ) {
125- aireply := aireply .NewAIReply (getReplyMode (ctx ))
126- reply := message .ParseMessageFromString (aireply .Talk (ctx .ExtractPlainText (), zero .BotConfig .NickName [0 ]))
127- // 回复
128- time .Sleep (time .Second * 1 )
129- if zero .OnlyPublic (ctx ) {
130- reply = append (reply , message .Reply (ctx .Event .MessageID ))
131- ctx .Send (reply )
132- return
133- }
134- ctx .Send (reply )
135- })
136- engine .OnPrefix ("设置回复模式" ).SetBlock (true ).
137- Handle (func (ctx * zero.Ctx ) {
138- param := ctx .State ["args" ].(string )
139- err := setReplyMode (ctx , param )
140- if err != nil {
141- ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text (err ))
142- return
143- }
144- ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("成功" ))
145- })
146- }
147-
148- func setReplyMode (ctx * zero.Ctx , name string ) error {
149- gid := ctx .Event .GroupID
150- if gid == 0 {
151- gid = - ctx .Event .UserID
152- }
153- var ok bool
154- var index int64
155- for i , s := range replyModes {
156- if s == name {
157- ok = true
158- index = int64 (i )
159- break
160- }
161- }
162- if ! ok {
163- return errors .New ("no such mode" )
164- }
165- m , ok := ctx .State ["manager" ].(* ctrl.Control [* zero.Ctx ])
166- if ! ok {
167- return errors .New ("no such plugin" )
168- }
169- return m .SetData (gid , index )
170- }
171-
172- func getReplyMode (ctx * zero.Ctx ) (name string ) {
173- gid := ctx .Event .GroupID
174- if gid == 0 {
175- gid = - ctx .Event .UserID
176- }
177- m , ok := ctx .State ["manager" ].(* ctrl.Control [* zero.Ctx ])
178- if ok {
179- index := m .GetData (gid )
180- if int (index ) < len (replyModes ) {
181- return replyModes [index ]
182- }
183- }
184- return "青云客"
185150}
0 commit comments