Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
fangliuyu committed Sep 21, 2022
2 parents 9f02c19 + 76cd2af commit 6f4949b
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 195 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.19

require (
github.com/Coloured-glaze/gg v1.3.4
github.com/FloatTech/AnimeAPI v1.5.1-0.20220917124909-ae9e70781efe
github.com/FloatTech/AnimeAPI v1.5.1-0.20220921020517-f4076c11486c
github.com/FloatTech/floatbox v0.0.0-20220917062226-31fb3ec4b135
github.com/FloatTech/sqlite v0.3.3
github.com/FloatTech/sqlite v0.4.0
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b
github.com/FloatTech/zbpctrl v1.5.2-0.20220919124954-6ab7f2656545
github.com/FloatTech/zbputils v1.5.1-0.20220919151529-ea4fc5c54947
github.com/FloatTech/zbpctrl v1.5.2-0.20220921013146-40d64bc7799c
github.com/FloatTech/zbputils v1.5.1-0.20220921055011-4675abce7d2b
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c
github.com/antchfx/htmlquery v1.2.5
github.com/corona10/goimagehash v1.1.0
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/Coloured-glaze/gg v1.3.4 h1:l31zIF/HaVwkzjrj+A56RGQoSKyKuR1IWtIrqXGFStI=
github.com/Coloured-glaze/gg v1.3.4/go.mod h1:Ih5NLNNDHOy3RJbB0EPqGTreIzq/H02TGThIagh8HJg=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/FloatTech/AnimeAPI v1.5.1-0.20220917124909-ae9e70781efe h1:3sO7PnVEHJXti4UMmUDOmAqlG6wnHvPUtvVVoPGAYGM=
github.com/FloatTech/AnimeAPI v1.5.1-0.20220917124909-ae9e70781efe/go.mod h1:NR/WdOv4i6cHMW4OCId3kFrZumDWUOkDCywiHCNAaYM=
github.com/FloatTech/AnimeAPI v1.5.1-0.20220921020517-f4076c11486c h1:L4mAdwNIqFi+tas13k1yJV9CyNXncTR8qvZ42sBPMxQ=
github.com/FloatTech/AnimeAPI v1.5.1-0.20220921020517-f4076c11486c/go.mod h1:8tw2qmRQW2bmfAU34bef84EPYZOkSrtYtgPkNdWuxVE=
github.com/FloatTech/floatbox v0.0.0-20220917062226-31fb3ec4b135 h1:HJZRF10i/vszUsZXJ+13oz+y1DTgCW453SFTeJvjjyo=
github.com/FloatTech/floatbox v0.0.0-20220917062226-31fb3ec4b135/go.mod h1:VNqP+XqXDOEArudsDVYQJfZd5bO6Sj4l4oRa8DM8cAw=
github.com/FloatTech/sqlite v0.3.3 h1:cSlpv+DcIehqaUG2YyjW0Twh396FoGsybpEpFZnX3Jg=
github.com/FloatTech/sqlite v0.3.3/go.mod h1:i33d92OtR8jcp5fBUvQtospf27+MkfUxnGwnZ95E/dA=
github.com/FloatTech/sqlite v0.4.0 h1:fvQ1vc7fw99jYXccs5KItMluy7QL1t6NxbkH7aN1F4g=
github.com/FloatTech/sqlite v0.4.0/go.mod h1:i33d92OtR8jcp5fBUvQtospf27+MkfUxnGwnZ95E/dA=
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b h1:tvciXWq2nuvTbFeJGLDNIdRX3BI546D3O7k7vrVueZw=
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/FloatTech/zbpctrl v1.5.2-0.20220919124954-6ab7f2656545 h1:MhJ+ihagJsOKNBBWn5502+WQAL8vHNdppFMM9L0vifI=
github.com/FloatTech/zbpctrl v1.5.2-0.20220919124954-6ab7f2656545/go.mod h1:Je/7SFwqfMeTvjav4qmwWE7w7aT8cYzCYVgrie9tuu4=
github.com/FloatTech/zbputils v1.5.1-0.20220919151529-ea4fc5c54947 h1:8s2JLVn9cJ5ZNbR3CIaML/gHhSl4ExJXjQbIEAdPQi8=
github.com/FloatTech/zbputils v1.5.1-0.20220919151529-ea4fc5c54947/go.mod h1:FT+WkqHuETSMdZzp9zJEP1NvjbRS+pUmUoIUc4ZBaIY=
github.com/FloatTech/zbpctrl v1.5.2-0.20220921013146-40d64bc7799c h1:zYcKvMbwQeifSQg0LnxhmCdyP3mb7ZqS6rjt68jbukw=
github.com/FloatTech/zbpctrl v1.5.2-0.20220921013146-40d64bc7799c/go.mod h1:+2mGs9vUWJsvfcbodcmFegqOKEqHnLwopF1jTLVs/gU=
github.com/FloatTech/zbputils v1.5.1-0.20220921055011-4675abce7d2b h1:fjbeCejROd6lAnP8TbMXU5H7sfLlodRIX0Icq81t+Us=
github.com/FloatTech/zbputils v1.5.1-0.20220921055011-4675abce7d2b/go.mod h1:C+PfyuEOxooTDb1S/SoptP3K+pTCXbUvC/AxXb3Q2yk=
github.com/PuerkitoBio/goquery v1.5.1 h1:PSPBGne8NIUWw+/7vFBV+kG2J/5MOjbzc7154OaKCSE=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q=
Expand Down
218 changes: 37 additions & 181 deletions plugin/ai_reply/ai_tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@ package aireply

import (
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"sync"
"time"

"github.com/pkumza/numcn"
log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"

"github.com/FloatTech/AnimeAPI/aireply"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
)

const (
ttsServiceName = "tts"
cnapi = "http://233366.proxy.nscc-gz.cn:8888?speaker=%s&text=%s"
// testString = "这是测试语言......"
cnapi = "http://233366.proxy.nscc-gz.cn:8888?speaker=%s&text=%s"
// testString = "这是测试语音......"
)

// 每个角色的测试文案
Expand Down Expand Up @@ -96,10 +86,9 @@ var (
}
)

type ttsInstances struct {
type ttsmode struct {
sync.RWMutex
defaultSoundMode string
soundMode []string
mode map[int64]int64
}

func list(list []string, num int) string {
Expand All @@ -115,200 +104,67 @@ func list(list []string, num int) string {
return s
}

func init() {
tts := &ttsInstances{
defaultSoundMode: "派蒙",
soundMode: soundList[:],
}
engine := control.Register(ttsServiceName, &ctrl.Options[*zero.Ctx]{
DisableOnDefault: true,
Help: "语音回复(大家一起来炼丹)\n" +
"- @Bot 任意文本(任意一句话回复)\n" +
"- 设置语音模式[原神人物]\n" +
"- 设置默认语音模式[原神人物]\n" +
"- 恢复成默认语音模式\n" +
"当前适用的原神人物含有以下:\n" + list(soundList[:], 5),
})
tts.RLock()
defer tts.RUnlock()
func newttsmode() *ttsmode {
tts := &ttsmode{}
m, ok := control.Lookup(ttsServiceName)
tts.mode = make(map[int64]int64)
if ok {
tts.defaultSoundMode = soundList[m.GetData(-2905)]
tts.mode[-2905] = m.GetData(-2905)
}
engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText()
// 获取回复模式
r := aireply.NewAIReply(getReplyMode(ctx))
// 获取回复的文本
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
// 获取角色
name := tts.getSoundMode(ctx)
if _, ok := testRecord[name]; !ok {
ctx.SendChain(message.Text("配置的语言人物数据丢失!请重新设置语言人物。"))
return
}
// 获取语言
record := message.Record(fmt.Sprintf(cnapi, url.QueryEscape(name), url.QueryEscape(
// 将数字转文字
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)
}),
))).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(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool {
param := ctx.State["regex_matched"].([]string)[1]
if _, ok := testRecord[param]; !ok {
return false
}
return true
}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
param := ctx.State["regex_matched"].([]string)[1]
// 保存设置
err := tts.setSoundMode(ctx, param)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
return
}
// 设置验证
name := tts.getSoundMode(ctx)
if _, ok := testRecord[name]; !ok {
ctx.SendChain(message.Text("配置的语言人物数据丢失!请重新设置语言人物。"))
return
}
record := message.Record(fmt.Sprintf(cnapi, url.QueryEscape(name), url.QueryEscape(testRecord[name]))).Add("cache", 0)
if ID := ctx.SendChain(record); ID.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语言,请重试。"))
return
}
time.Sleep(time.Second * 2)
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,默认模式为", name))
})
engine.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool {
param := ctx.State["regex_matched"].([]string)[1]
if _, ok := testRecord[param]; !ok {
return false
}
return true
}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
param := ctx.State["regex_matched"].([]string)[1]
// 保存设置
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)
if ID := ctx.SendChain(record); ID.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语言,请重试。"))
return
}
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))
})
return tts
}

func (tts *ttsInstances) setSoundMode(ctx *zero.Ctx, name string) error {
func (tts *ttsmode) setSoundMode(ctx *zero.Ctx, name string) error {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
var index int64
tts.RLock()
for i, s := range tts.soundMode {
var i int
var s string
for i, s = range soundList {
if s == name {
index = int64(i)
break
}
}
tts.RUnlock()
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
return m.SetData(gid, index)
tts.Lock()
defer tts.Unlock()
tts.mode[gid] = int64(i)
return m.SetData(gid, int64(i))
}

func (tts *ttsInstances) getSoundMode(ctx *zero.Ctx) (name string) {
func (tts *ttsmode) getSoundMode(ctx *zero.Ctx) (name string) {
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 tts.soundMode[index]
}
tts.RLock()
defer tts.RUnlock()
return soundList[tts.mode[gid]]
}

func (tts *ttsmode) resetSoundMode(ctx *zero.Ctx) {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
return tts.defaultSoundMode
tts.Lock()
defer tts.Unlock()
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
tts.mode[gid] = m.GetData(-2905)
}

func (tts *ttsInstances) setDefaultSoundMode(name string) error {
var index int64
tts.RLock()
for i, s := range tts.soundMode {
func setDefaultSoundMode(name string) error {
var i int
var s string
for i, s = range soundList {
if s == name {
index = int64(i)
break
}
}
tts.RUnlock()
m, ok := control.Lookup(ttsServiceName)
if !ok {
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("[tts]检验数据失败,当前写入的数据为" + soundMode)
}
tts.defaultSoundMode = soundMode
return errors.New("[tts] service not found")
}
return err
return m.SetData(-2905, int64(i))
}
Loading

0 comments on commit 6f4949b

Please sign in to comment.