genshin vits add api key

This commit is contained in:
源文雨 2022-12-03 16:35:14 +08:00
parent a93f4f68b4
commit 82608d01bb
3 changed files with 56 additions and 14 deletions

View File

@ -12,7 +12,7 @@ import (
) )
const ( const (
cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&id=%d&text=%s" cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&id=%d&text=%s&code=%s"
) )
// 每个角色的测试文案 // 每个角色的测试文案
@ -132,6 +132,7 @@ func getReplyMode(ctx *zero.Ctx) (name string) {
*************************************************************/ *************************************************************/
type ttsmode struct { type ttsmode struct {
sync.RWMutex sync.RWMutex
apikey string
mode map[int64]int64 mode map[int64]int64
} }
@ -164,6 +165,24 @@ func newttsmode() *ttsmode {
return tts return tts
} }
func (tts *ttsmode) getAPIKey() string {
return tts.apikey
}
func (tts *ttsmode) setAPIKey(ctx *zero.Ctx, key string) error {
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
err := m.Manager.SetExtra(gid, &key)
if err != nil {
return errors.New("内部错误")
}
tts.apikey = key
return nil
}
func (tts *ttsmode) setSoundMode(ctx *zero.Ctx, name string) error { func (tts *ttsmode) setSoundMode(ctx *zero.Ctx, name string) error {
gid := ctx.Event.GroupID gid := ctx.Event.GroupID
if gid == 0 { if gid == 0 {

View File

@ -20,7 +20,7 @@ import (
var replyModes = [...]string{"青云客", "小爱"} var replyModes = [...]string{"青云客", "小爱"}
func init() { // 插件主体 func init() { // 插件主体
enOftts := control.Register("tts", &ctrl.Options[*zero.Ctx]{ ent := control.Register("tts", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: true, DisableOnDefault: true,
Brief: "人工智能语音回复", Brief: "人工智能语音回复",
Help: "- @Bot 任意文本(任意一句话回复)\n" + Help: "- @Bot 任意文本(任意一句话回复)\n" +
@ -30,7 +30,7 @@ func init() { // 插件主体
"当前适用的原神人物含有以下:\n" + list(soundList[:], 5), "当前适用的原神人物含有以下:\n" + list(soundList[:], 5),
}) })
tts := newttsmode() tts := newttsmode()
enOfreply := control.Register("aireply", &ctrl.Options[*zero.Ctx]{ enr := control.Register("aireply", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false, DisableOnDefault: false,
Brief: "人工智能回复", Brief: "人工智能回复",
Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]", Help: "- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]",
@ -38,7 +38,7 @@ func init() { // 插件主体
/************************************************************* /*************************************************************
*******************************AIreply************************ *******************************AIreply************************
*************************************************************/ *************************************************************/
enOfreply.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). enr.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
aireply := aireply.NewAIReply(getReplyMode(ctx)) aireply := aireply.NewAIReply(getReplyMode(ctx))
reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0])) reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
@ -51,7 +51,7 @@ func init() { // 插件主体
} }
ctx.Send(reply) ctx.Send(reply)
}) })
enOfreply.OnPrefix("设置回复模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { enr.OnPrefix("设置回复模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
param := ctx.State["args"].(string) param := ctx.State["args"].(string)
err := setReplyMode(ctx, param) err := setReplyMode(ctx, param)
if err != nil { if err != nil {
@ -63,7 +63,7 @@ func init() { // 插件主体
/************************************************************* /*************************************************************
***********************tts************************************ ***********************tts************************************
*************************************************************/ *************************************************************/
enOftts.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser). ent.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
msg := ctx.ExtractPlainText() msg := ctx.ExtractPlainText()
// 获取回复模式 // 获取回复模式
@ -82,13 +82,13 @@ func init() { // 插件主体
} }
return numcn.EncodeFromFloat64(f) return numcn.EncodeFromFloat64(f)
}), }),
))).Add("cache", 0) ), tts.getAPIKey())).Add("cache", 0)
// 发送语音 // 发送语音
if ID := ctx.SendChain(record); ID.ID() == 0 { if ID := ctx.SendChain(record); ID.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply)) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply))
} }
}) })
enOftts.OnRegex(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool { ent.OnRegex(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool {
param := ctx.State["regex_matched"].([]string)[1] param := ctx.State["regex_matched"].([]string)[1]
if _, ok := testRecord[param]; !ok { if _, ok := testRecord[param]; !ok {
return false return false
@ -108,7 +108,7 @@ func init() { // 插件主体
ctx.SendChain(message.Text("配置的语音人物数据丢失!请重新设置语音人物。")) ctx.SendChain(message.Text("配置的语音人物数据丢失!请重新设置语音人物。"))
return return
} }
record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]))).Add("cache", 0) record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]), tts.getAPIKey())).Add("cache", 0)
if ID := ctx.SendChain(record); ID.ID() == 0 { if ID := ctx.SendChain(record); ID.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语音,请重试。")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语音,请重试。"))
return return
@ -116,7 +116,7 @@ func init() { // 插件主体
time.Sleep(time.Second * 2) time.Sleep(time.Second * 2)
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功"))
}) })
enOftts.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool { ent.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool {
param := ctx.State["regex_matched"].([]string)[1] param := ctx.State["regex_matched"].([]string)[1]
if _, ok := testRecord[param]; !ok { if _, ok := testRecord[param]; !ok {
return false return false
@ -132,7 +132,7 @@ func init() { // 插件主体
} }
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功"))
}) })
enOftts.OnFullMatch("恢复成默认语音模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) { ent.OnFullMatch("恢复成默认语音模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
err := tts.resetSoundMode(ctx) err := tts.resetSoundMode(ctx)
if err != nil { if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err)) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
@ -142,4 +142,12 @@ func init() { // 插件主体
index := tts.getSoundMode(ctx) index := tts.getSoundMode(ctx)
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,当前为", soundList[index])) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,当前为", soundList[index]))
}) })
ent.OnRegex(`^设置原神语音\s*api\s*key\s*([0-9a-zA-Z-_]{54}==)$`, zero.OnlyPrivate, zero.SuperUserPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
err := tts.setAPIKey(ctx, ctx.State["regex_matched"].([]string)[1])
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("设置成功"))
})
} }

View File

@ -16,7 +16,7 @@ import (
const ( const (
jpapi = "https://moegoe.azurewebsites.net/api/speak?text=%s&id=%d" jpapi = "https://moegoe.azurewebsites.net/api/speak?text=%s&id=%d"
krapi = "https://moegoe.azurewebsites.net/api/speakkr?text=%s&id=%d" krapi = "https://moegoe.azurewebsites.net/api/speakkr?text=%s&id=%d"
cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&text=%s&id=%d" cnapi = "https://genshin.azurewebsites.net/api/speak?format=mp3&text=%s&id=%d&code=%s"
) )
var speakers = map[string]uint{ var speakers = map[string]uint{
@ -49,6 +49,21 @@ func init() {
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
text := ctx.State["regex_matched"].([]string)[2] text := ctx.State["regex_matched"].([]string)[2]
id := speakers[ctx.State["regex_matched"].([]string)[1]] id := speakers[ctx.State["regex_matched"].([]string)[1]]
ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id))) c, ok := control.Lookup("tts")
if !ok {
ctx.SendChain(message.Text("ERROR: plugin tts not found"))
return
}
var key string
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
err := c.Manager.GetExtra(gid, &key)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id, key)))
}) })
} }