ZeroBot-Plugin/plugin_ai_reply/main.go
himawari 45f0f5cd6c
将mockingbird统一到tts (#131)
* fix:处理回复文本

* feat:将以mockingbird统一到tts里

* fix:修lint

* fix:换一个正则函数

* fix:去掉ok

Co-authored-by: Guohuiyuan <haibaraguo@yeahka.com>
2022-02-23 11:15:42 +08:00

93 lines
2.2 KiB
Go

// Package aireply AI 回复
package aireply
import (
"errors"
"time"
"github.com/FloatTech/AnimeAPI/aireply"
control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/zbputils/control/order"
)
const (
replyServiceName = "aireply"
)
var replyModes = [...]string{"青云客", "小爱"}
func init() { // 插件主体
engine := control.Register(replyServiceName, order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "人工智能回复\n" +
"- @Bot 任意文本(任意一句话回复)\n- 设置回复模式[青云客 | 小爱]\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 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功"))
})
}
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 := control.Lookup(replyServiceName)
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 := control.Lookup(replyServiceName)
if ok {
index := m.GetData(gid)
if int(index) < len(replyModes) {
return replyModes[index]
}
}
return "青云客"
}