forked from yqchilde/wxbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (83 loc) · 2.96 KB
/
Copy pathmain.go
File metadata and controls
94 lines (83 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package plmm
import (
"fmt"
"github.com/imroc/req/v3"
"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/engine/pkg/sqlite"
"github.com/yqchilde/wxbot/engine/robot"
)
var (
db sqlite.DB
plmm Plmm
)
func init() {
engine := control.Register("plmm", &control.Options{
Alias: "漂亮妹妹",
Help: "指令:\n" +
"* 漂亮妹妹 -> 获取漂亮妹妹",
DataFolder: "plmm",
})
if err := sqlite.Open(engine.GetDataFolder()+"/plmm.db", &db); err != nil {
log.Fatalf("open sqlite db failed: %v", err)
}
if err := db.CreateAndFirstOrCreate("plmm", &plmm); err != nil {
log.Fatalf("create plmm table failed: %v", err)
}
engine.OnFullMatch("漂亮妹妹").SetBlock(true).Handle(func(ctx *robot.Ctx) {
if plmm.AppId == "" || plmm.AppSecret == "" {
ctx.ReplyTextAndAt("请先私聊机器人配置appId和appSecret\n指令:set plmm appId __\n指令:set plmm appSecret __\n相关秘钥申请地址:https://www.mxnzp.com/doc/detail?id=15")
return
}
if len(plmmUrlStorage) > 0 {
if err := ctx.ReplyImage(plmmUrlStorage[0]); err != nil {
log.Errorf("[plmm] 发送图片失败: %v", err)
}
plmmUrlStorage = plmmUrlStorage[1:]
} else {
var resp PlmmApiResponse
api := fmt.Sprintf("https://www.mxnzp.com/api/image/girl/list/random?app_id=%s&app_secret=%s", plmm.AppId, plmm.AppSecret)
if err := req.C().SetBaseURL(api).Get().Do().Into(&resp); err != nil {
return
}
if resp.Code != 1 {
return
}
for _, val := range resp.Data {
plmmUrlStorage = append(plmmUrlStorage, val.ImageUrl)
}
if err := ctx.ReplyImage(plmmUrlStorage[0]); err != nil {
log.Errorf("[plmm] 发送图片失败: %v", err)
}
plmmUrlStorage = plmmUrlStorage[1:]
}
})
// 设置appId
engine.OnRegex("set plmm appId ([0-9a-z]{16})", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
appId := ctx.State["regex_matched"].([]string)[1]
if err := db.Orm.Table("plmm").Where("1 = 1").Update("app_id", appId).Error; err != nil {
ctx.ReplyText("appId设置失败")
return
}
plmm.AppId = appId
ctx.ReplyText("appId设置成功")
})
// 设置appSecret
engine.OnRegex("set plmm appSecret ([0-9a-zA-Z]{32})", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
appSecret := ctx.State["regex_matched"].([]string)[1]
if err := db.Orm.Table("plmm").Where("1 = 1").Update("app_secret", appSecret).Error; err != nil {
ctx.ReplyText("appSecret设置失败")
return
}
plmm.AppSecret = appSecret
ctx.ReplyText("appSecret设置成功")
})
// 获取插件配置信息
engine.OnFullMatch("get plmm info", robot.OnlyPrivate, robot.AdminPermission).SetBlock(true).Handle(func(ctx *robot.Ctx) {
var plmm Plmm
if err := db.Orm.Table("plmm").Limit(1).Find(&plmm).Error; err != nil {
return
}
ctx.ReplyTextAndAt(fmt.Sprintf("插件 - 漂亮妹妹\nappId: %s\nappSecret: %s", plmm.AppId, plmm.AppSecret))
})
}