diff --git a/data b/data index 61349e6e..913db5e5 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 61349e6e05d2256182950469321653fb535ccbbc +Subproject commit 913db5e597a217c205584488a66de52277a4a327 diff --git a/plugin_cpstory/cpstory.go b/plugin_cpstory/cpstory.go index ddca9eee..dd4866df 100644 --- a/plugin_cpstory/cpstory.go +++ b/plugin_cpstory/cpstory.go @@ -2,13 +2,12 @@ package cpstory import ( "strings" - "time" zero "github.com/wdvxdr1123/ZeroBot" - "github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/ctxext" "github.com/FloatTech/ZeroBot-Plugin/utils/math" ) @@ -16,19 +15,15 @@ const ( prio = 20 ) -var ( - engine = control.Register("cpstory", &control.Options{ +func init() { + 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) { + engine.OnRegex("^组cp.*?(\\d+).*?(\\d+)", zero.OnlyGroup).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])) + gong := ctxext.CardOrNickName(ctx, math.Str2Int64(ctx.State["regex_matched"].([]string)[1])) + shou := ctxext.CardOrNickName(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) @@ -51,11 +46,3 @@ func init() { } }) } - -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 index ba5a8d7a..1a433507 100644 --- a/plugin_cpstory/data.go +++ b/plugin_cpstory/data.go @@ -24,7 +24,7 @@ func init() { // os.RemoveAll(dbpath) _ = os.MkdirAll(dbpath, 0755) _, _ = file.GetLazyData(dbfile, false, true) - err := db.Create("cp_story", &CpStory{}) + err := db.Create("cp_story", &cpstory{}) if err != nil { panic(err) } diff --git a/plugin_cpstory/model.go b/plugin_cpstory/model.go index c1d8ab1c..e3f86ad8 100644 --- a/plugin_cpstory/model.go +++ b/plugin_cpstory/model.go @@ -1,13 +1,13 @@ package cpstory -type CpStory struct { - Id int64 `db:"id"` +type cpstory struct { + ID int64 `db:"id"` Gong string `db:"gong"` Shou string `db:"shou"` Story string `db:"story"` } -func getRandomCpStory() (cs CpStory) { +func getRandomCpStory() (cs cpstory) { _ = db.Pick("cp_story", &cs) return } diff --git a/plugin_funny/laugh.go b/plugin_funny/laugh.go index 51eb8d7b..4ba38c80 100644 --- a/plugin_funny/laugh.go +++ b/plugin_funny/laugh.go @@ -1,7 +1,6 @@ package funny import ( - "strconv" "strings" "time" @@ -10,6 +9,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/ctxext" "github.com/FloatTech/ZeroBot-Plugin/utils/sql" ) @@ -29,19 +29,7 @@ func init() { return } // 获取名字 - name := ctx.State["args"].(string) - if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" { - qq, _ := strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64) - name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("card").String() - if name == "" { - name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("nickname").String() - } - } else if name == "" { - name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, ctx.Event.UserID, false).Get("card").String() - if name == "" { - name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, ctx.Event.UserID, false).Get("nickname").String() - } - } + name := ctxext.NickName(ctx) var j joke err := db.Pick("jokes", &j) if err != nil { diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index 6dd16221..1a53a34f 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -17,6 +17,7 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/control" "github.com/FloatTech/ZeroBot-Plugin/plugin_manager/timer" + "github.com/FloatTech/ZeroBot-Plugin/utils/ctxext" "github.com/FloatTech/ZeroBot-Plugin/utils/math" "github.com/FloatTech/ZeroBot-Plugin/utils/process" "github.com/FloatTech/ZeroBot-Plugin/utils/sql" @@ -423,11 +424,7 @@ func init() { // 插件主体 Handle(func(ctx *zero.Ctx) { if ctx.Event.NoticeType == "group_decrease" { userid := ctx.Event.UserID - nickname := ctx.GetGroupMemberInfo(ctx.Event.GroupID, userid, false).Get("card").String() - if nickname == "" { - nickname = ctx.GetGroupMemberInfo(ctx.Event.GroupID, userid, false).Get("nickname").String() - } - ctx.SendChain(message.Text(nickname, "(", userid, ")", "离开了我们...")) + ctx.SendChain(message.Text(ctxext.CardOrNickName(ctx, userid), "(", userid, ")", "离开了我们...")) } }) // 设置欢迎语 diff --git a/plugin_nativewife/main.go b/plugin_nativewife/main.go index e8bed251..145b5963 100644 --- a/plugin_nativewife/main.go +++ b/plugin_nativewife/main.go @@ -17,6 +17,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/utils/helper" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/ctxext" "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) @@ -49,13 +50,7 @@ func init() { ctx.SendChain(message.Text("大家的wife都是", wn, "\n"), message.Image(baseuri+"/"+grpf+"/"+wn), message.Text("\n哦~")) default: // 获取名字 - name := ctx.State["args"].(string) - if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" { - qq, _ := strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64) - name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("nickname").Str - } else if name == "" { - name = ctx.Event.Sender.NickName - } + name := ctxext.NickName(ctx) now := time.Now() s := md5.Sum(helper.StringToBytes(fmt.Sprintf("%s%d%d%d", name, now.Year(), now.Month(), now.Day()))) r := rand.New(rand.NewSource(int64(binary.LittleEndian.Uint64(s[:])))) diff --git a/plugin_shindan/shindan.go b/plugin_shindan/shindan.go index 62caf5ea..4ba5b5d5 100644 --- a/plugin_shindan/shindan.go +++ b/plugin_shindan/shindan.go @@ -2,7 +2,6 @@ package shindan import ( - "strconv" "time" "github.com/FloatTech/AnimeAPI/shindanmaker" @@ -11,6 +10,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/ctxext" ) var ( @@ -40,13 +40,7 @@ func handle(ctx *zero.Ctx) { return } // 获取名字 - name := ctx.State["args"].(string) - if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" { - qq, _ := strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64) - name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("nickname").Str - } else if name == "" { - name = ctx.Event.Sender.NickName - } + name := ctxext.NickName(ctx) // 调用接口 text, err := shindanmaker.Shindanmaker(ctx.State["id"].(int64), name) if err != nil { diff --git a/plugin_wtf/main.go b/plugin_wtf/main.go index 604d87ac..2730f08c 100644 --- a/plugin_wtf/main.go +++ b/plugin_wtf/main.go @@ -31,7 +31,7 @@ func init() { } ctx.SendChain(message.Text(s)) }) - en.OnRegex(`^查询鬼东西(\d*)`).SetBlock(true).SetPriority(30). + en.OnRegex(`^查询鬼东西(\d*)`, zero.OnlyGroup).SetBlock(true).SetPriority(30). Handle(func(ctx *zero.Ctx) { if !limit.Load(ctx.Event.UserID).Acquire() { ctx.SendChain(message.Text("请稍后重试0x0...")) diff --git a/utils/ctxext/name.go b/utils/ctxext/name.go new file mode 100644 index 00000000..4e9d37aa --- /dev/null +++ b/utils/ctxext/name.go @@ -0,0 +1,29 @@ +// Package ctxext zero ctx 扩展 +package ctxext + +import ( + "strconv" + + zero "github.com/wdvxdr1123/ZeroBot" +) + +// NickName 从 args 获取名字 +func NickName(ctx *zero.Ctx) (name string) { + name = ctx.State["args"].(string) + if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" { + qq, _ := strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64) + name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("nickname").Str + } else if name == "" { + name = ctx.Event.Sender.NickName + } + return +} + +// CardOrNickName 从 uid 获取名字 +func CardOrNickName(ctx *zero.Ctx, uid int64) (name string) { + name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, uid, false).Get("card").String() + if name == "" { + name = ctx.GetStrangerInfo(uid, false).Get("nickname").String() + } + return +}