Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👍 #5

Merged
merged 2 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions plugin/ai_reply/ai_tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aireply

import (
"errors"
"net/url"
"regexp"
"sync"

Expand Down Expand Up @@ -131,9 +132,9 @@ func getReplyMode(ctx *zero.Ctx) (name string) {
***********************tts************************************
*************************************************************/
type ttsmode struct {
sync.RWMutex
apikey string
mode map[int64]int64
sync.RWMutex `json:"-"`
APIKey string
mode map[int64]int64
}

func list(list []string, num int) string {
Expand Down Expand Up @@ -165,8 +166,16 @@ func newttsmode() *ttsmode {
return tts
}

func (tts *ttsmode) getAPIKey() string {
return tts.apikey
func (tts *ttsmode) getAPIKey(ctx *zero.Ctx) string {
if tts.APIKey == "" {
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
}
_ = m.Manager.GetExtra(gid, &tts)
}
return url.QueryEscape(tts.APIKey)
}

func (tts *ttsmode) setAPIKey(ctx *zero.Ctx, key string) error {
Expand All @@ -179,7 +188,7 @@ func (tts *ttsmode) setAPIKey(ctx *zero.Ctx, key string) error {
if err != nil {
return errors.New("内部错误")
}
tts.apikey = key
tts.APIKey = key
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions plugin/ai_reply/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() { // 插件主体
"- 设置语音模式[原神人物]\n" +
"- 设置默认语音模式[原神人物]\n" +
"- 恢复成默认语音模式\n" +
"- 设置原神语音 api key xxxxxx (key请加开发群获得)\n" +
"当前适用的原神人物含有以下:\n" + list(soundList[:], 5),
})
tts := newttsmode()
Expand Down Expand Up @@ -82,7 +83,7 @@ func init() { // 插件主体
}
return numcn.EncodeFromFloat64(f)
}),
), tts.getAPIKey())).Add("cache", 0)
), tts.getAPIKey(ctx)))
// 发送语音
if ID := ctx.SendChain(record); ID.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(reply))
Expand All @@ -108,7 +109,7 @@ func init() { // 插件主体
ctx.SendChain(message.Text("配置的语音人物数据丢失!请重新设置语音人物。"))
return
}
record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]), tts.getAPIKey())).Add("cache", 0)
record := message.Record(fmt.Sprintf(cnapi, i, url.QueryEscape(testRecord[soundList[i]]), tts.getAPIKey(ctx))).Add("cache", 0)
if ID := ctx.SendChain(record); ID.ID() == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置失败!无法发送测试语音,请重试。"))
return
Expand Down
6 changes: 4 additions & 2 deletions plugin/moegoe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func init() {
ctx.SendChain(message.Text("ERROR: plugin tts not found"))
return
}
var key string
var key struct {
APIKey string
}
gid := ctx.Event.GroupID
if gid == 0 {
gid = -ctx.Event.UserID
Expand All @@ -64,6 +66,6 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id, key)))
ctx.SendChain(message.Record(fmt.Sprintf(cnapi, url.QueryEscape(text), id, url.QueryEscape(key.APIKey))))
})
}