From 486160fc5761b49913ba01e5163b7688c5fa366f Mon Sep 17 00:00:00 2001 From: himawari <54976075+guohuiyuan@users.noreply.github.com> Date: Sun, 26 Dec 2021 22:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0cp=E7=9F=AD=E6=89=93=20(#94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat:添加cp短打插件 * feat:匹配中英文标点符号和中文字符 --- README.md | 3 + main.go | 1 + plugin_chouxianghua/chouxianghua.go | 2 +- plugin_cpstory/cpstory.go | 59 ++++++++++++++++++++ plugin_cpstory/data.go | 35 ++++++++++++ plugin_cpstory/model.go | 13 +++++ plugin_juejuezi/{jiejiezi.go => juejuezi.go} | 0 7 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 plugin_cpstory/cpstory.go create mode 100644 plugin_cpstory/data.go create mode 100644 plugin_cpstory/model.go rename plugin_juejuezi/{jiejiezi.go => juejuezi.go} (100%) diff --git a/README.md b/README.md index c5243821..7f1c2adc 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,9 @@ zerobot -h -t token -u url [-d|w] [-g 监听地址:端口] qq1 qq2 qq3 ... - **藏头诗** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_cangtoushi"` - [x] 藏头诗[xxx] - [x] 藏尾诗[xxx] +- **cp短打** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_cpstory"` + - [x] 组cp[@xxx][@xxx] + - [x] 组cp大老师 雪乃 - **TODO...** ## 使用方法 diff --git a/main.go b/main.go index 74671181..082dac3f 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ import ( _ "github.com/FloatTech/ZeroBot-Plugin/plugin_choose" // 选择困难症帮手 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chouxianghua" // 说抽象话 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_coser" // 三次元小姐姐 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_cpstory" // cp短打 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_fortune" // 运势 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_funny" // 笑话 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_hs" // 炉石 diff --git a/plugin_chouxianghua/chouxianghua.go b/plugin_chouxianghua/chouxianghua.go index 4da594b0..6fa98b51 100644 --- a/plugin_chouxianghua/chouxianghua.go +++ b/plugin_chouxianghua/chouxianghua.go @@ -13,7 +13,7 @@ func init() { control.Register("chouxianghua", &control.Options{ DisableOnDefault: false, Help: "抽象话\n- 抽象翻译xxx\n", - }).OnRegex("^抽象翻译((\\s|[\\r\\n]|[\u4E00-\u9FA5A-Za-z0-9“”,;、。《》=?\\x22])+)$").SetBlock(true).SetPriority(prio). + }).OnRegex("^抽象翻译((\\s|[\\r\\n]|[\\p{Han}\\p{P}A-Za-z0-9])+)$").SetBlock(true).SetPriority(prio). Handle(func(ctx *zero.Ctx) { r := cx(ctx.State["regex_matched"].([]string)[1]) ctx.SendChain(message.Text(r)) diff --git a/plugin_cpstory/cpstory.go b/plugin_cpstory/cpstory.go new file mode 100644 index 00000000..6172770d --- /dev/null +++ b/plugin_cpstory/cpstory.go @@ -0,0 +1,59 @@ +package cpstory + +import ( + "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/math" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/extension/rate" + "github.com/wdvxdr1123/ZeroBot/message" + "strings" + "time" +) + +const ( + prio = 20 +) + +var ( + engine = control.Register("cpstory", &control.Options{ + DisableOnDefault: false, + Help: "cp短打\n- 组cp[@xxx][@xxx]\n- 组cp大老师 雪乃", + }) + limit = rate.NewManager(time.Minute, 20) +) + +func init() { + engine.OnRegex("^组cp.*?(\\d+).*?(\\d+)").SetBlock(true).SetPriority(prio).Handle(func(ctx *zero.Ctx) { + cs := getRandomCpStory() + gong := getCardOrNickName(ctx, math.Str2Int64(ctx.State["regex_matched"].([]string)[1])) + shou := getCardOrNickName(ctx, math.Str2Int64(ctx.State["regex_matched"].([]string)[2])) + text := strings.ReplaceAll(cs.Story, "<攻>", gong) + text = strings.ReplaceAll(text, "<受>", shou) + text = strings.ReplaceAll(text, cs.Gong, gong) + text = strings.ReplaceAll(text, cs.Shou, gong) + ctx.SendChain(message.Text(text)) + }) + engine.OnPrefix("组cp").SetBlock(true).SetPriority(prio + 1).Handle(func(ctx *zero.Ctx) { + cs := getRandomCpStory() + params := strings.Split(ctx.State["args"].(string), " ") + if len(params) < 2 { + ctx.SendChain(message.Text(ctx.Event.MessageID), message.Text("请用空格分开两个人名")) + } else { + gong := params[0] + shou := params[1] + text := strings.ReplaceAll(cs.Story, "<攻>", gong) + text = strings.ReplaceAll(text, "<受>", shou) + text = strings.ReplaceAll(text, cs.Gong, gong) + text = strings.ReplaceAll(text, cs.Shou, gong) + ctx.SendChain(message.Text(text)) + } + }) +} + +func getCardOrNickName(ctx *zero.Ctx, userId int64) (name string) { + name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, userId, false).Get("card").String() + if name == "" { + name = ctx.GetStrangerInfo(userId, false).Get("nickname").String() + } + return +} diff --git a/plugin_cpstory/data.go b/plugin_cpstory/data.go new file mode 100644 index 00000000..45ae4d04 --- /dev/null +++ b/plugin_cpstory/data.go @@ -0,0 +1,35 @@ +package cpstory + +import ( + "github.com/FloatTech/ZeroBot-Plugin/utils/file" + "github.com/FloatTech/ZeroBot-Plugin/utils/process" + "github.com/FloatTech/ZeroBot-Plugin/utils/sql" + log "github.com/sirupsen/logrus" + "os" +) + +const ( + dbpath = "data/CpStory/" + dbfile = dbpath + "cp.db" +) + +var db = &sql.Sqlite{DBPath: dbfile} + +// 加载数据库 +func init() { + go func() { + process.SleepAbout1sTo2s() + // os.RemoveAll(dbpath) + _ = os.MkdirAll(dbpath, 0755) + _, _ = file.GetLazyData(dbfile, false, true) + err := db.Create("cp_story", &CpStory{}) + if err != nil { + panic(err) + } + n, err := db.Count("cp_story") + if err != nil { + panic(err) + } + log.Printf("[cpstory]读取%d条故事", n) + }() +} diff --git a/plugin_cpstory/model.go b/plugin_cpstory/model.go new file mode 100644 index 00000000..c1d8ab1c --- /dev/null +++ b/plugin_cpstory/model.go @@ -0,0 +1,13 @@ +package cpstory + +type CpStory struct { + Id int64 `db:"id"` + Gong string `db:"gong"` + Shou string `db:"shou"` + Story string `db:"story"` +} + +func getRandomCpStory() (cs CpStory) { + _ = db.Pick("cp_story", &cs) + return +} diff --git a/plugin_juejuezi/jiejiezi.go b/plugin_juejuezi/juejuezi.go similarity index 100% rename from plugin_juejuezi/jiejiezi.go rename to plugin_juejuezi/juejuezi.go