mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
parent
2922854f96
commit
dc34a33346
@ -13,7 +13,6 @@ 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"
|
||||||
// testString = "这是测试语音......"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 每个角色的测试文案
|
// 每个角色的测试文案
|
||||||
@ -86,6 +85,51 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*************************************************************
|
||||||
|
*******************************AIreply************************
|
||||||
|
*************************************************************/
|
||||||
|
func setReplyMode(ctx *zero.Ctx, name string) error {
|
||||||
|
gid := ctx.Event.GroupID
|
||||||
|
if gid == 0 {
|
||||||
|
gid = -ctx.Event.UserID
|
||||||
|
}
|
||||||
|
var ok bool
|
||||||
|
var index int64
|
||||||
|
for i, s := range replyModes {
|
||||||
|
if s == name {
|
||||||
|
ok = true
|
||||||
|
index = int64(i)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return errors.New("no such mode")
|
||||||
|
}
|
||||||
|
m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||||
|
if !ok {
|
||||||
|
return errors.New("no such plugin")
|
||||||
|
}
|
||||||
|
return m.SetData(gid, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getReplyMode(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 {
|
||||||
|
index := m.GetData(gid)
|
||||||
|
if int(index) < len(replyModes) {
|
||||||
|
return replyModes[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "青云客"
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************
|
||||||
|
***********************tts************************************
|
||||||
|
*************************************************************/
|
||||||
type ttsmode struct {
|
type ttsmode struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
mode map[int64]int64
|
mode map[int64]int64
|
||||||
@ -106,10 +150,16 @@ func list(list []string, num int) string {
|
|||||||
|
|
||||||
func newttsmode() *ttsmode {
|
func newttsmode() *ttsmode {
|
||||||
tts := &ttsmode{}
|
tts := &ttsmode{}
|
||||||
|
tts.Lock()
|
||||||
|
defer tts.Unlock()
|
||||||
m, ok := control.Lookup(ttsServiceName)
|
m, ok := control.Lookup(ttsServiceName)
|
||||||
tts.mode = make(map[int64]int64)
|
tts.mode = make(map[int64]int64, 2*len(soundList))
|
||||||
|
tts.mode[-2905] = 1
|
||||||
if ok {
|
if ok {
|
||||||
tts.mode[-2905] = m.GetData(-2905)
|
index := m.GetData(-2905)
|
||||||
|
if index > 0 && index < int64(len(soundList)) {
|
||||||
|
tts.mode[-2905] = index
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return tts
|
return tts
|
||||||
}
|
}
|
||||||
@ -119,31 +169,42 @@ func (tts *ttsmode) setSoundMode(ctx *zero.Ctx, name string) error {
|
|||||||
if gid == 0 {
|
if gid == 0 {
|
||||||
gid = -ctx.Event.UserID
|
gid = -ctx.Event.UserID
|
||||||
}
|
}
|
||||||
var i int
|
var index int64
|
||||||
var s string
|
for i, s := range soundList {
|
||||||
for i, s = range soundList {
|
|
||||||
if s == name {
|
if s == name {
|
||||||
|
index = int64(i + 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if index == 0 {
|
||||||
|
return errors.New("不支持设置语音人物" + name)
|
||||||
|
}
|
||||||
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||||
tts.Lock()
|
tts.Lock()
|
||||||
defer tts.Unlock()
|
defer tts.Unlock()
|
||||||
tts.mode[gid] = int64(i)
|
tts.mode[gid] = index
|
||||||
return m.SetData(gid, int64(i))
|
return m.SetData(gid, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tts *ttsmode) getSoundMode(ctx *zero.Ctx) (i int64) {
|
func (tts *ttsmode) getSoundMode(ctx *zero.Ctx) int64 {
|
||||||
gid := ctx.Event.GroupID
|
gid := ctx.Event.GroupID
|
||||||
if gid == 0 {
|
if gid == 0 {
|
||||||
gid = -ctx.Event.UserID
|
gid = -ctx.Event.UserID
|
||||||
}
|
}
|
||||||
tts.RLock()
|
tts.Lock()
|
||||||
defer tts.RUnlock()
|
defer tts.Unlock()
|
||||||
return tts.mode[gid]
|
i, ok := tts.mode[gid]
|
||||||
|
if !ok {
|
||||||
|
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||||
|
i = m.GetData(gid)
|
||||||
|
}
|
||||||
|
if i <= 0 || i >= int64(len(soundList)) {
|
||||||
|
i = tts.mode[-2905]
|
||||||
|
}
|
||||||
|
return i - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tts *ttsmode) resetSoundMode(ctx *zero.Ctx) {
|
func (tts *ttsmode) resetSoundMode(ctx *zero.Ctx) error {
|
||||||
gid := ctx.Event.GroupID
|
gid := ctx.Event.GroupID
|
||||||
if gid == 0 {
|
if gid == 0 {
|
||||||
gid = -ctx.Event.UserID
|
gid = -ctx.Event.UserID
|
||||||
@ -151,20 +212,27 @@ func (tts *ttsmode) resetSoundMode(ctx *zero.Ctx) {
|
|||||||
tts.Lock()
|
tts.Lock()
|
||||||
defer tts.Unlock()
|
defer tts.Unlock()
|
||||||
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
m := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||||
tts.mode[gid] = m.GetData(-2905)
|
tts.mode[gid] = 0
|
||||||
|
return m.SetData(gid, 0) // 重置数据
|
||||||
}
|
}
|
||||||
|
|
||||||
func setDefaultSoundMode(name string) error {
|
func (tts *ttsmode) setDefaultSoundMode(name string) error {
|
||||||
var i int
|
var index int64
|
||||||
var s string
|
for i, s := range soundList {
|
||||||
for i, s = range soundList {
|
|
||||||
if s == name {
|
if s == name {
|
||||||
|
index = int64(i + 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if index == 0 {
|
||||||
|
return errors.New("不支持设置语音人物" + name)
|
||||||
|
}
|
||||||
|
tts.Lock()
|
||||||
|
defer tts.Unlock()
|
||||||
m, ok := control.Lookup(ttsServiceName)
|
m, ok := control.Lookup(ttsServiceName)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("[tts] service not found")
|
return errors.New("[tts] service not found")
|
||||||
}
|
}
|
||||||
return m.SetData(-2905, int64(i))
|
tts.mode[-2905] = index
|
||||||
|
return m.SetData(-2905, index)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
package aireply
|
package aireply
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -26,7 +25,7 @@ const (
|
|||||||
var replyModes = [...]string{"青云客", "小爱"}
|
var replyModes = [...]string{"青云客", "小爱"}
|
||||||
|
|
||||||
func init() { // 插件主体
|
func init() { // 插件主体
|
||||||
engine := control.Register(ttsServiceName, &ctrl.Options[*zero.Ctx]{
|
enOftts := control.Register(ttsServiceName, &ctrl.Options[*zero.Ctx]{
|
||||||
DisableOnDefault: true,
|
DisableOnDefault: true,
|
||||||
Help: "语音回复(大家一起来炼丹)\n" +
|
Help: "语音回复(大家一起来炼丹)\n" +
|
||||||
"- @Bot 任意文本(任意一句话回复)\n" +
|
"- @Bot 任意文本(任意一句话回复)\n" +
|
||||||
@ -36,7 +35,40 @@ func init() { // 插件主体
|
|||||||
"当前适用的原神人物含有以下:\n" + list(soundList[:], 5),
|
"当前适用的原神人物含有以下:\n" + list(soundList[:], 5),
|
||||||
})
|
})
|
||||||
tts := newttsmode()
|
tts := newttsmode()
|
||||||
engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
|
enOfreply := control.Register(replyServiceName, &ctrl.Options[*zero.Ctx]{
|
||||||
|
DisableOnDefault: false,
|
||||||
|
Help: "人工智能回复\n" +
|
||||||
|
"- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]",
|
||||||
|
})
|
||||||
|
/*************************************************************
|
||||||
|
*******************************AIreply************************
|
||||||
|
*************************************************************/
|
||||||
|
enOfreply.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
|
||||||
|
Handle(func(ctx *zero.Ctx) {
|
||||||
|
aireply := aireply.NewAIReply(getReplyMode(ctx))
|
||||||
|
reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
|
||||||
|
// 回复
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
if zero.OnlyPublic(ctx) {
|
||||||
|
reply = append(reply, message.Reply(ctx.Event.MessageID))
|
||||||
|
ctx.Send(reply)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx.Send(reply)
|
||||||
|
})
|
||||||
|
enOfreply.OnPrefix("设置回复模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
|
param := ctx.State["args"].(string)
|
||||||
|
err := setReplyMode(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("成功"))
|
||||||
|
})
|
||||||
|
/*************************************************************
|
||||||
|
***********************tts************************************
|
||||||
|
*************************************************************/
|
||||||
|
enOftts.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
msg := ctx.ExtractPlainText()
|
msg := ctx.ExtractPlainText()
|
||||||
// 获取回复模式
|
// 获取回复模式
|
||||||
@ -44,7 +76,8 @@ func init() { // 插件主体
|
|||||||
// 获取回复的文本
|
// 获取回复的文本
|
||||||
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
|
reply := r.TalkPlain(msg, zero.BotConfig.NickName[0])
|
||||||
// 获取语音
|
// 获取语音
|
||||||
record := message.Record(fmt.Sprintf(cnapi, tts.getSoundMode(ctx), url.QueryEscape(
|
index := tts.getSoundMode(ctx)
|
||||||
|
record := message.Record(fmt.Sprintf(cnapi, index, url.QueryEscape(
|
||||||
// 将数字转文字
|
// 将数字转文字
|
||||||
re.ReplaceAllStringFunc(reply, func(s string) string {
|
re.ReplaceAllStringFunc(reply, func(s string) string {
|
||||||
f, err := strconv.ParseFloat(s, 64)
|
f, err := strconv.ParseFloat(s, 64)
|
||||||
@ -55,16 +88,12 @@ func init() { // 插件主体
|
|||||||
return numcn.EncodeFromFloat64(f)
|
return numcn.EncodeFromFloat64(f)
|
||||||
}),
|
}),
|
||||||
))).Add("cache", 0)
|
))).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 {
|
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))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
engine.OnRegex(`^设置语音模式(.*)$`, zero.AdminPermission, func(ctx *zero.Ctx) bool {
|
enOftts.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
|
||||||
@ -92,7 +121,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("设置成功"))
|
||||||
})
|
})
|
||||||
engine.OnRegex(`^设置默认语音模式(.*)$`, zero.SuperUserPermission, func(ctx *zero.Ctx) bool {
|
enOftts.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
|
||||||
@ -101,85 +130,21 @@ func init() { // 插件主体
|
|||||||
}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
param := ctx.State["regex_matched"].([]string)[1]
|
param := ctx.State["regex_matched"].([]string)[1]
|
||||||
// 保存设置
|
// 保存设置
|
||||||
err := setDefaultSoundMode(param)
|
err := tts.setDefaultSoundMode(param)
|
||||||
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))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功"))
|
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功"))
|
||||||
})
|
})
|
||||||
engine.OnFullMatch("恢复成默认语音模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
enOftts.OnFullMatch("恢复成默认语音模式", zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
||||||
tts.resetSoundMode(ctx)
|
err := tts.resetSoundMode(ctx)
|
||||||
// 设置验证
|
|
||||||
name := tts.getSoundMode(ctx)
|
|
||||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,当前为", name))
|
|
||||||
})
|
|
||||||
engine = control.Register(replyServiceName, &ctrl.Options[*zero.Ctx]{
|
|
||||||
DisableOnDefault: false,
|
|
||||||
Help: "人工智能回复\n" +
|
|
||||||
"- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客|小爱]",
|
|
||||||
})
|
|
||||||
// 回复 @和包括名字
|
|
||||||
engine.OnMessage(zero.OnlyToMe).SetBlock(true).Limit(ctxext.LimitByUser).
|
|
||||||
Handle(func(ctx *zero.Ctx) {
|
|
||||||
aireply := aireply.NewAIReply(getReplyMode(ctx))
|
|
||||||
reply := message.ParseMessageFromString(aireply.Talk(ctx.ExtractPlainText(), zero.BotConfig.NickName[0]))
|
|
||||||
// 回复
|
|
||||||
time.Sleep(time.Second * 1)
|
|
||||||
if zero.OnlyPublic(ctx) {
|
|
||||||
reply = append(reply, message.Reply(ctx.Event.MessageID))
|
|
||||||
ctx.Send(reply)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ctx.Send(reply)
|
|
||||||
})
|
|
||||||
engine.OnPrefix("设置回复模式").SetBlock(true).
|
|
||||||
Handle(func(ctx *zero.Ctx) {
|
|
||||||
param := ctx.State["args"].(string)
|
|
||||||
err := setReplyMode(ctx, param)
|
|
||||||
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))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功"))
|
// 设置验证
|
||||||
|
index := tts.getSoundMode(ctx)
|
||||||
|
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("设置成功,当前为", soundList[index]))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func setReplyMode(ctx *zero.Ctx, name string) error {
|
|
||||||
gid := ctx.Event.GroupID
|
|
||||||
if gid == 0 {
|
|
||||||
gid = -ctx.Event.UserID
|
|
||||||
}
|
|
||||||
var ok bool
|
|
||||||
var index int64
|
|
||||||
for i, s := range replyModes {
|
|
||||||
if s == name {
|
|
||||||
ok = true
|
|
||||||
index = int64(i)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
return errors.New("no such mode")
|
|
||||||
}
|
|
||||||
m, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
|
||||||
if !ok {
|
|
||||||
return errors.New("no such plugin")
|
|
||||||
}
|
|
||||||
return m.SetData(gid, index)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getReplyMode(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 {
|
|
||||||
index := m.GetData(gid)
|
|
||||||
if int(index) < len(replyModes) {
|
|
||||||
return replyModes[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "青云客"
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user