🎨 改进代码

This commit is contained in:
Yiwen-Chan 2021-03-22 21:20:52 +08:00
parent 0a1dcde512
commit ef2093cd83
7 changed files with 72 additions and 63 deletions

View File

@ -2,13 +2,14 @@ package manager
import (
"bot/manager/utils"
"strings"
"time"
zero "github.com/wdvxdr1123/ZeroBot"
)
func init() { // 插件主体
// TODO 菜单
// 菜单
zero.OnFullMatch("群管系统").SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.Send(`====群管====
@ -26,7 +27,7 @@ func init() { // 插件主体
- 私聊转发 0000 XXX`)
return
})
// TODO 升为管理
// 升为管理
zero.OnRegex(`^升为管理.*?(\d+)`, zero.OnlyGroup, zero.SuperUserPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupAdmin(
@ -42,7 +43,7 @@ func init() { // 插件主体
ctx.Send(nickname + " 升为了管理~")
return
})
// TODO 取消管理
// 取消管理
zero.OnRegex(`^取消管理.*?(\d+)`, zero.OnlyGroup, zero.SuperUserPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupAdmin(
@ -58,7 +59,7 @@ func init() { // 插件主体
ctx.Send("残念~ " + nickname + " 暂时失去了管理员的资格")
return
})
// TODO 踢出群聊
// 踢出群聊
zero.OnRegex(`^踢出群聊.*?(\d+)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupKick(
@ -74,7 +75,7 @@ func init() { // 插件主体
ctx.Send("残念~ " + nickname + " 被放逐")
return
})
// TODO 退出群聊
// 退出群聊
zero.OnRegex(`^退出群聊.*?(\d+)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupLeave(
@ -83,7 +84,7 @@ func init() { // 插件主体
)
return
})
// TODO 开启全体禁言
// 开启全体禁言
zero.OnRegex(`^开启全员禁言$`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupWholeBan(
@ -93,7 +94,7 @@ func init() { // 插件主体
ctx.Send("全员自闭开始~")
return
})
// TODO 解除全员禁言
// 解除全员禁言
zero.OnRegex(`^解除全员禁言$`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupWholeBan(
@ -103,7 +104,7 @@ func init() { // 插件主体
ctx.Send("全员自闭结束~")
return
})
// TODO 禁言
// 禁言
zero.OnRegex(`^禁言.*?(\d+).*?\s(\d+)(.*)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
duration := utils.Str2Int(ctx.State["regex_matched"].([]string)[2])
@ -128,7 +129,7 @@ func init() { // 插件主体
ctx.Send("小黑屋收留成功~")
return
})
// TODO 解除禁言
// 解除禁言
zero.OnRegex(`^解除禁言.*?(\d+)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupBan(
@ -139,7 +140,7 @@ func init() { // 插件主体
ctx.Send("小黑屋释放成功~")
return
})
// TODO 自闭禁言
// 自闭禁言
zero.OnRegex(`^我要自闭.*?(\d+)(.*)`, zero.OnlyGroup).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
duration := utils.Str2Int(ctx.State["regex_matched"].([]string)[1])
@ -164,7 +165,7 @@ func init() { // 插件主体
ctx.Send("那我就不手下留情了~")
return
})
// TODO 修改名片
// 修改名片
zero.OnRegex(`^修改名片.*?(\d+).*?\s(.*)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupCard(
@ -175,7 +176,7 @@ func init() { // 插件主体
ctx.Send("嗯!已经修改了")
return
})
// TODO 修改头衔
// 修改头衔
zero.OnRegex(`^修改头衔.*?(\d+).*?\s(.*)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupSpecialTitle(
@ -186,7 +187,7 @@ func init() { // 插件主体
ctx.Send("嗯!已经修改了")
return
})
// TODO 申请头衔
// 申请头衔
zero.OnRegex(`^申请头衔(.*)`, zero.OnlyGroup).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupSpecialTitle(
@ -197,27 +198,35 @@ func init() { // 插件主体
ctx.Send("嗯!不错的头衔呢~")
return
})
// TODO 群聊转发
// 群聊转发
zero.OnRegex(`^群聊转发.*?(\d+)\s(.*)`, zero.SuperUserPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
// 对CQ码进行反转义
content := ctx.State["regex_matched"].([]string)[2]
content = strings.ReplaceAll(content, "[", "[")
content = strings.ReplaceAll(content, "]", "]")
ctx.SendGroupMessage(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 需要发送的群
ctx.State["regex_matched"].([]string)[2], // 需要发送的信息
content, // 需要发送的信息
)
ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1])
return
})
// TODO 私聊转发
// 私聊转发
zero.OnRegex(`^私聊转发.*?(\d+)\s(.*)`, zero.SuperUserPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
// 对CQ码进行反转义
content := ctx.State["regex_matched"].([]string)[2]
content = strings.ReplaceAll(content, "[", "[")
content = strings.ReplaceAll(content, "]", "]")
ctx.SendPrivateMessage(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 需要发送的人的qq
ctx.State["regex_matched"].([]string)[2], // 需要发送的信息
content, // 需要发送的信息
)
ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1])
return
})
// TODO 戳一戳
// 戳一戳
zero.OnNotice().SetBlock(false).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
if ctx.Event.NoticeType == "notify" && ctx.Event.SubType == "poke" && ctx.Event.RawEvent.Get("target_id").Int() == utils.Str2Int(zero.BotConfig.SelfID) {
@ -226,7 +235,7 @@ func init() { // 插件主体
}
return
})
// TODO 入群欢迎
// 入群欢迎
zero.OnNotice().SetBlock(false).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
if ctx.Event.NoticeType == "group_increase" {
@ -234,7 +243,7 @@ func init() { // 插件主体
}
return
})
// TODO 退群提醒
// 退群提醒
zero.OnNotice().SetBlock(false).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
if ctx.Event.NoticeType == "group_decrease" {

View File

@ -8,38 +8,38 @@ import (
)
func init() { // 插件主体
// TODO 根据PID搜图
// 根据PID搜图
zero.OnRegex(`^搜图(\d+)$`).SetBlock(true).SetPriority(30).
Handle(func(ctx *zero.Ctx) {
id := utils.Str2Int(ctx.State["regex_matched"].([]string)[1])
ctx.Send("少女祈祷中......")
// TODO 获取P站插图信息
// 获取P站插图信息
illust := &utils.Illust{}
if err := illust.IllustInfo(id); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
}
// TODO 下载P站插图
// 下载P站插图
if _, err := illust.PixivPicDown(CACHEPATH); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
}
// TODO 发送搜索结果
// 发送搜索结果
ctx.Send(illust.DetailPic)
return
})
// TODO 通过回复以图搜图
// 通过回复以图搜图
zero.OnRegex(`\[CQ:reply,id=(.*?)\](.*)搜索图片`).SetBlock(true).SetPriority(32).
Handle(func(ctx *zero.Ctx) {
var pics []string // 图片搜索池子
// TODO 获取回复的上文图片链接
// 获取回复的上文图片链接
id := utils.Str2Int(ctx.State["regex_matched"].([]string)[1])
for _, elem := range ctx.GetMessage(id).Elements {
if elem.Type == "image" {
pics = append(pics, elem.Data["url"])
}
}
// TODO 没有收到图片则向用户索取
// 没有收到图片则向用户索取
if len(pics) == 0 {
ctx.Send("请发送多张图片!")
next := ctx.FutureEvent("message", ctx.CheckSession())
@ -64,7 +64,7 @@ func init() { // 插件主体
ctx.Send("没有收到图片,搜图结束......")
return
}
// TODO 开始搜索图片
// 开始搜索图片
ctx.Send("少女祈祷中......")
for _, pic := range pics {
if text, err := utils.SauceNaoSearch(pic); err == nil {
@ -82,17 +82,17 @@ func init() { // 插件主体
}
return
})
// TODO 通过命令以图搜图
// 通过命令以图搜图
zero.OnKeywordGroup([]string{"以图识图", "以图搜图", "搜索图片"}).SetBlock(true).SetPriority(33).
Handle(func(ctx *zero.Ctx) {
var pics []string // 图片搜索池子
// TODO 获取信息中图片链接
// 获取信息中图片链接
for _, elem := range ctx.Event.Message {
if elem.Type == "image" {
pics = append(pics, elem.Data["url"])
}
}
// TODO 没有收到图片则向用户索取
// 没有收到图片则向用户索取
if len(pics) == 0 {
ctx.Send("请发送多张图片!")
next := ctx.FutureEvent("message", zero.CheckUser(ctx.Event.UserID))
@ -117,7 +117,7 @@ func init() { // 插件主体
ctx.Send("没有收到图片,搜图结束......")
return
}
// TODO 开始搜索图片
// 开始搜索图片
ctx.Send("少女祈祷中......")
for _, pic := range pics {
if text, err := utils.SauceNaoSearch(pic); err == nil {

View File

@ -51,7 +51,7 @@ func init() { // 插件主体
return
}
var type_ = ctx.State["regex_matched"].([]string)[1]
// TODO 补充池子
// 补充池子
go func() {
times := utils.Min(PoolsCache.Max-PoolsCache.Size(type_), 2)
for i := 0; i < times; i++ {
@ -88,7 +88,7 @@ func init() { // 插件主体
return
}
}
// TODO 从缓冲池里抽一张
// 从缓冲池里抽一张
if id := ctx.Send(PoolsCache.GetOnePic(type_, FORM)); id == 0 {
ctx.Send(fmt.Sprintf("ERROR: %v", errors.New("可能被风控了")))
}
@ -103,13 +103,13 @@ func init() { // 插件主体
illust = &utils.Illust{}
)
ctx.Send("少女祈祷中......")
// TODO 查询P站插图信息
// 查询P站插图信息
if err := illust.IllustInfo(id); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
}
// TODO 下载插画
// 下载插画
if _, err := illust.PixivPicDown(PoolsCache.Path); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
@ -119,7 +119,7 @@ func init() { // 插件主体
ctx.Send(fmt.Sprintf("ERROR: %v", "可能被风控,发送失败"))
return
}
// TODO 添加插画到对应的数据库table
// 添加插画到对应的数据库table
if err := DB.Insert(type_, illust); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
@ -134,7 +134,7 @@ func init() { // 插件主体
type_ = ctx.State["regex_matched"].([]string)[1]
id = utils.Str2Int(ctx.State["regex_matched"].([]string)[2])
)
// TODO 查询数据库
// 查询数据库
if err := DB.Delete(type_, fmt.Sprintf("WHERE pid=%d", id)); err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
@ -143,7 +143,7 @@ func init() { // 插件主体
return
})
// TODO 查询数据库涩图数量
// 查询数据库涩图数量
zero.OnFullMatchGroup([]string{"setu -s", "setu --status"}).SetBlock(true).SetPriority(23).
Handle(func(ctx *zero.Ctx) {
state := []string{"[SetuTime]"}
@ -160,14 +160,14 @@ func init() { // 插件主体
ctx.Send(strings.Join(state, ""))
return
})
// TODO 开xml模式
// 开xml模式
zero.OnFullMatchGroup([]string{"setu -x", "setu --xml"}).SetBlock(true).SetPriority(24).
Handle(func(ctx *zero.Ctx) {
FORM = "XML"
ctx.Send("[SetuTime] XML->ON")
return
})
// TODO 关xml模式
// 关xml模式
zero.OnFullMatchGroup([]string{"setu -p", "setu --pic"}).SetBlock(true).SetPriority(24).
Handle(func(ctx *zero.Ctx) {
FORM = "PIC"

View File

@ -24,12 +24,12 @@ func Ascii2dSearch(pic string) (text string, err error) {
Transport: &transport,
}
// TODO 包装请求参数
// 包装请求参数
data := url.Values{}
data.Set("uri", pic) // 图片链接
fromData := strings.NewReader(data.Encode())
// TODO 网络请求
// 网络请求
req, _ := http.NewRequest("POST", api, fromData)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0")
@ -37,7 +37,7 @@ func Ascii2dSearch(pic string) (text string, err error) {
if err != nil {
return "", err
}
// TODO 色合检索改变到特征检索
// 色合检索改变到特征检索
var bovwUrl = strings.ReplaceAll(resp.Request.URL.String(), "color", "bovw")
bovwReq, _ := http.NewRequest("POST", bovwUrl, nil)
bovwReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
@ -47,15 +47,15 @@ func Ascii2dSearch(pic string) (text string, err error) {
return "", err
}
defer bovwResp.Body.Close()
// TODO 解析XPATH
// 解析XPATH
doc, err := xpath.Parse(resp.Body)
if err != nil {
return "", err
}
// TODO 取出每个返回的结果
// 取出每个返回的结果
list := xpath.Find(doc, `//div[@class="row item-box"]`)
var link string
// TODO 遍历取出第一个返回的PIXIV结果
// 遍历取出第一个返回的PIXIV结果
for _, n := range list {
linkPath := xpath.Find(n, `//div[2]/div[3]/h6/a[1]`)
picPath := xpath.Find(n, `//div[1]/img`)
@ -66,7 +66,7 @@ func Ascii2dSearch(pic string) (text string, err error) {
}
}
}
// TODO 链接取出PIXIV id
// 链接取出PIXIV id
var index = strings.LastIndex(link, "/")
if link == "" || index == -1 {
return "", errors.New("Ascii2d not found")
@ -75,7 +75,7 @@ func Ascii2dSearch(pic string) (text string, err error) {
if id == 0 {
return "", errors.New("convert to pid error")
}
// TODO 根据PID查询插图信息
// 根据PID查询插图信息
var illust = &Illust{}
if err := illust.IllustInfo(id); err != nil {
return "", err
@ -83,7 +83,7 @@ func Ascii2dSearch(pic string) (text string, err error) {
if illust.AgeLimit != "all-age" {
return "", errors.New("Ascii2d not found")
}
// TODO 返回插图信息文本
// 返回插图信息文本
return fmt.Sprintf(
`[SetuTime] emmm大概是这个
标题%s

View File

@ -18,13 +18,13 @@ func (this *Illust) PixivPicDown(path string) (savePath string, err error) {
url = strings.ReplaceAll(url, "img-original", "img-master")
url = strings.ReplaceAll(url, "_p0", "_p0_master1200")
url = strings.ReplaceAll(url, ".png", ".jpg")
// TODO 文件名为url的hash值
// 文件名为url的hash值
savePath = path + Int2Str(pid) + ".jpg"
// TODO 文件存在或文件大小大于10kb
// 文件存在或文件大小大于10kb
if PathExists(savePath) && FileSize(savePath) > 10240 {
return savePath, nil
}
// TODO 模拟QQ客户端请求
// 模拟QQ客户端请求
client := &http.Client{}
reqest, _ := http.NewRequest("GET", url, nil)
reqest.Header.Add("User-Agent", "QQ/8.2.0.1296 CFNetwork/1126")
@ -39,7 +39,7 @@ func (this *Illust) PixivPicDown(path string) (savePath string, err error) {
return "", errors.New(fmt.Sprintf("Download failed, code %d", code))
}
defer resp.Body.Close()
// TODO 写入文件
// 写入文件
data, _ := ioutil.ReadAll(resp.Body)
f, _ := os.OpenFile(savePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
defer f.Close()

View File

@ -30,12 +30,12 @@ func (this *Illust) IllustInfo(id int64) (err error) {
api := fmt.Sprintf("https://pixiv.net/ajax/illust/%d", id)
transport := http.Transport{
DisableKeepAlives: true,
// TODO 绕过sni审查
// 绕过sni审查
TLSClientConfig: &tls.Config{
ServerName: "-",
InsecureSkipVerify: true,
},
// TODO 更改dns
// 更改dns
Dial: func(network, addr string) (net.Conn, error) {
return net.Dial("tcp", "210.140.131.223:443")
},
@ -44,7 +44,7 @@ func (this *Illust) IllustInfo(id int64) (err error) {
Transport: &transport,
}
// TODO 网络请求
// 网络请求
req, err := http.NewRequest("GET", api, nil)
if err != nil {
return err
@ -67,7 +67,7 @@ func (this *Illust) IllustInfo(id int64) (err error) {
}
json := gjson.ParseBytes(body).Get("body")
// TODO 如果有"R-18"tag则判断为R-18暂时
// 如果有"R-18"tag则判断为R-18暂时
var ageLimit = "all-age"
for _, tag := range json.Get("tags.tags.#.tag").Array() {
if tag.Str == "R-18" {
@ -75,12 +75,12 @@ func (this *Illust) IllustInfo(id int64) (err error) {
break
}
}
// TODO 解决json返回带html格式
// 解决json返回带html格式
var caption = strings.ReplaceAll(json.Get("illustComment").Str, "<br />", "\n")
if index := strings.Index(caption, "<"); index != -1 {
caption = caption[:index]
}
// TODO 解析返回插画信息
// 解析返回插画信息
this.Pid = json.Get("illustId").Int()
this.Title = json.Get("illustTitle").Str
this.Caption = caption

View File

@ -27,7 +27,7 @@ func SauceNaoSearch(pic string) (text string, err error) {
Transport: &transport,
}
// TODO 包装请求参数
// 包装请求参数
data := url.Values{}
data.Set("url", pic) // 图片链接
data.Set("api_key", apiKey) // api_key
@ -36,7 +36,7 @@ func SauceNaoSearch(pic string) (text string, err error) {
data.Set("output_type", "2") // 返回JSON格式数据
fromData := strings.NewReader(data.Encode())
// TODO 网络请求
// 网络请求
req, err := http.NewRequest("POST", api, fromData)
if err != nil {
return "", err
@ -65,7 +65,7 @@ func SauceNaoSearch(pic string) (text string, err error) {
if content.Get("results.0.header.similarity").Float() < minSimilarity {
return "", errors.New("SauceNAO not found")
}
// TODO 正常发送
// 正常发送
return fmt.Sprintf(
`[SetuTime] 我有把握是这个[CQ:image,file=%s]相似度%s%%
标题%s