将mockingbird统一到tts (#131)

* fix:处理回复文本

* feat:将以mockingbird统一到tts里

* fix:修lint

* fix:换一个正则函数

* fix:去掉ok

Co-authored-by: Guohuiyuan <haibaraguo@yeahka.com>
This commit is contained in:
himawari 2022-02-23 11:15:42 +08:00 committed by GitHub
parent e468ac1933
commit 45f0f5cd6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 13 deletions

1
go.mod
View File

@ -17,6 +17,7 @@ require (
github.com/fumiama/sqlite3 v1.14.6 github.com/fumiama/sqlite3 v1.14.6
github.com/jinzhu/gorm v1.9.16 github.com/jinzhu/gorm v1.9.16
github.com/mroth/weightedrand v0.4.1 github.com/mroth/weightedrand v0.4.1
github.com/pkumza/numcn v1.0.0
github.com/shirou/gopsutil/v3 v3.21.12 github.com/shirou/gopsutil/v3 v3.21.12
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/tidwall/gjson v1.13.0 github.com/tidwall/gjson v1.13.0

2
go.sum
View File

@ -117,6 +117,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkumza/numcn v1.0.0 h1:ZT5cf9IJkUZgRgEtCiNNykk0RwsrKXSTsvDHOwUTzgE=
github.com/pkumza/numcn v1.0.0/go.mod h1:QSeH+al9dWCd8di5HZM/ZqHqhZmUKfph572e9Ev/ETc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=

View File

@ -15,13 +15,13 @@ import (
) )
const ( const (
serviceName = "aireply" replyServiceName = "aireply"
) )
var modes = [...]string{"青云客", "小爱"} var replyModes = [...]string{"青云客", "小爱"}
func init() { // 插件主体 func init() { // 插件主体
engine := control.Register(serviceName, order.AcquirePrio(), &control.Options{ engine := control.Register(replyServiceName, order.AcquirePrio(), &control.Options{
DisableOnDefault: false, DisableOnDefault: false,
Help: "人工智能回复\n" + Help: "人工智能回复\n" +
"- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客 | 小爱]\n- ", "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客 | 小爱]\n- ",
@ -59,7 +59,7 @@ func setReplyMode(ctx *zero.Ctx, name string) error {
} }
var ok bool var ok bool
var index int64 var index int64
for i, s := range modes { for i, s := range replyModes {
if s == name { if s == name {
ok = true ok = true
index = int64(i) index = int64(i)
@ -69,7 +69,7 @@ func setReplyMode(ctx *zero.Ctx, name string) error {
if !ok { if !ok {
return errors.New("no such mode") return errors.New("no such mode")
} }
m, ok := control.Lookup(serviceName) m, ok := control.Lookup(replyServiceName)
if !ok { if !ok {
return errors.New("no such plugin") return errors.New("no such plugin")
} }
@ -81,11 +81,11 @@ func getReplyMode(ctx *zero.Ctx) (name string) {
if gid == 0 { if gid == 0 {
gid = -ctx.Event.UserID gid = -ctx.Event.UserID
} }
m, ok := control.Lookup(serviceName) m, ok := control.Lookup(replyServiceName)
if ok { if ok {
index := m.GetData(gid) index := m.GetData(gid)
if int(index) < len(modes) { if int(index) < len(replyModes) {
return modes[index] return replyModes[index]
} }
} }
return "青云客" return "青云客"

View File

@ -1,10 +1,17 @@
package aireply package aireply
import ( import (
"errors"
"github.com/pkumza/numcn"
log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"regexp"
"strconv"
"github.com/FloatTech/AnimeAPI/aireply" "github.com/FloatTech/AnimeAPI/aireply"
"github.com/FloatTech/AnimeAPI/tts"
"github.com/FloatTech/AnimeAPI/tts/baidutts"
"github.com/FloatTech/AnimeAPI/tts/mockingbird" "github.com/FloatTech/AnimeAPI/tts/mockingbird"
"github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/ctxext"
@ -12,16 +19,107 @@ import (
"github.com/FloatTech/zbputils/control/order" "github.com/FloatTech/zbputils/control/order"
) )
const (
ttsServiceName = "tts"
)
var (
reNumber = "(\\-|\\+)?\\d+(\\.\\d+)?"
t *ttsInstances
)
type ttsInstances struct {
m map[string]tts.TTS
l []string
}
func (t *ttsInstances) List() []string {
return t.l
}
func init() { func init() {
control.Register("mockingbird", order.AcquirePrio(), &control.Options{ 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, DisableOnDefault: false,
Help: "拟声鸟\n- @Bot 任意文本(任意一句话回复)", Help: "语音回复(包括拟声鸟和百度)\n- @Bot 任意文本(任意一句话回复)\n- 设置语音模式拟声鸟阿梓 | 设置语音模式拟声鸟药水哥 | 设置语音模式百度女声 | 设置语音模式百度男声| 设置语音模式百度度逍遥 | 设置语音模式百度度丫丫",
}).OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). })
engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText() msg := ctx.ExtractPlainText()
r := aireply.NewAIReply(getReplyMode(ctx)) r := aireply.NewAIReply(getReplyMode(ctx))
ctx.SendChain(message.Record(mockingbird.NewMockingBirdTTS(1).Speak(ctx.Event.UserID, func() string { tts := newTTS(getSoundMode(ctx))
return r.TalkPlain(msg, zero.BotConfig.NickName[0]) ctx.SendChain(message.Record(tts.Speak(ctx.Event.UserID, func() string {
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
re := regexp.MustCompile(reNumber)
reply = re.ReplaceAllStringFunc(reply, func(s string) string {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Errorln("[tts]:", err)
return s
}
return numcn.EncodeFromFloat64(f)
})
log.Println("[tts]:", reply)
return reply
}))) })))
}) })
engine.OnRegex(`^设置语音模式(.*)$`, ctxext.FirstValueInList(t)).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
param := ctx.State["regex_matched"].([]string)[1]
err := setSoundMode(ctx, param)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功"))
})
}
// newTTS 语音简单工厂
func newTTS(name string) tts.TTS {
return t.m[name]
}
func setSoundMode(ctx *zero.Ctx, name string) error {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
var index int64
for i, s := range t.l {
if s == name {
index = int64(i)
break
}
}
m, ok := control.Lookup(ttsServiceName)
if !ok {
return errors.New("no such plugin")
}
return m.SetData(gid, index)
}
func getSoundMode(ctx *zero.Ctx) (name string) {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
m, ok := control.Lookup(ttsServiceName)
if ok {
index := m.GetData(gid)
if int(index) < len(t.l) {
return t.l[index]
}
}
return "拟声鸟阿梓"
} }