MCPcopy Index your code
hub / github.com/FloatTech/ZeroBot-Plugin / init

Function init

plugin/pig/pig.go:46–155  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

44)
45
46func init() {
47 _ = checkAndUpdateData()
48 // 1. 随机猪猪
49 engine.OnRegex(`^(随机猪猪|来份猪猪|抽个猪猪)$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
50 if err := checkAndUpdateData(); err != nil {
51 ctx.SendChain(message.Text("[Pig] ERROR: ", err, "\nEXP: 随机猪猪失败,获取数据错误"))
52 return
53 }
54
55 pigMutex.RLock()
56 defer pigMutex.RUnlock()
57
58 if len(pigCache) == 0 {
59 ctx.SendChain(message.Text("[Pig] ERROR: 暂无猪猪数据,请联系管理员"))
60 return
61 }
62
63 target := pigCache[rand.Intn(len(pigCache))]
64 imgData, err := target.fetch()
65 if err != nil {
66 ctx.SendChain(message.Text("[Pig] ERROR: ", err, "\nEXP: 图片加载失败"))
67 return
68 }
69
70 ctx.SendChain(
71 message.Text(fmt.Sprintf("🐷 ID: %s | %s", target.ID, target.Title)),
72 message.ImageBytes(imgData),
73 )
74 })
75
76 // 2. 搜索猪猪
77 engine.OnRegex(`^搜索猪猪\s+(.+)$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
78 keyword := strings.TrimSpace(ctx.State["regex_matched"].([]string)[1])
79
80 if err := checkAndUpdateData(); err != nil {
81 ctx.SendChain(message.Text("[Pig] ERROR: ", err, "\nEXP: 搜索猪猪失败,获取数据错误"))
82 return
83 }
84
85 pigMutex.RLock()
86 defer pigMutex.RUnlock()
87
88 var results []pigImage
89 for _, p := range pigCache {
90 if strings.Contains(p.Title, keyword) {
91 results = append(results, p)
92 }
93 }
94
95 if len(results) == 0 {
96 ctx.SendChain(message.Text("[Pig] ERROR: 未找到包含“", keyword, "”的猪猪"))
97 return
98 }
99
100 var sb strings.Builder
101 sb.WriteString(fmt.Sprintf("🔎 根据关键词“%s”找到 %d 只猪猪:\n", keyword, len(results)))
102
103 maxShow := 10

Callers

nothing calls this directly

Calls 3

checkAndUpdateDataFunction · 0.85
fetchMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected