Skip to content

Commit 3a3c9e5

Browse files
committed
✏️ 线程安全
1 parent bf6714f commit 3a3c9e5

3 files changed

Lines changed: 47 additions & 30 deletions

File tree

plugin_diana/data/text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func LoadText() error {
6565
log.Printf("[Diana]从镜像下载小作文%d字节...", resp.ContentLength)
6666
data, err := io.ReadAll(resp.Body)
6767
if err == nil && len(data) > 0 {
68-
f.Write(data)
68+
_, _ = f.Write(data)
6969
return compo.Unmarshal(data)
7070
}
7171
return err

plugin_hs/run.go

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1+
// Package hs 炉石
12
package hs
23

34
import (
45
"fmt"
56
"os"
67
"strconv"
78
"strings"
9+
"time"
810

911
"github.com/imroc/req"
1012
"github.com/tidwall/gjson"
1113
zero "github.com/wdvxdr1123/ZeroBot"
1214
"github.com/wdvxdr1123/ZeroBot/message"
1315
)
1416

17+
const cachedir = "data/hs/"
18+
1519
var header = req.Header{
1620
"user-agent": `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36`,
1721
"referer": `https://hs.fbigame.com`,
1822
}
1923

2024
func init() {
25+
os.RemoveAll(cachedir)
26+
err := os.MkdirAll(cachedir, 0755)
27+
if err != nil {
28+
panic(err)
29+
}
2130
zero.OnRegex(`^搜卡(.+)$`).
2231
SetBlock(true).SetPriority(20).Handle(func(ctx *zero.Ctx) {
2332
List := ctx.State["regex_matched"].([]string)[1]
@@ -26,32 +35,38 @@ func init() {
2635
gjson.Get(g, `list.0.CardID`).String()+
2736
`.png?auth_key=`+
2837
gjson.Get(g, `list.0.auth_key`).String(), header)
29-
im.ToFile("data/image/1.png")
30-
file, _ := os.Open("data/image/1.png")
31-
sg, _ := req.Post("https://pic.sogou.com/pic/upload_pic.jsp", req.FileUpload{
32-
File: file,
33-
FieldName: "image", // FieldName 是表单字段名
34-
FileName: "avatar.png", // Filename 是要上传的文件的名称,我们使用它来猜测mimetype,并将其上传到服务器上
35-
})
36-
var tx string
37-
t := int(gjson.Get(g, `list.#`).Int())
38-
if t == 0 {
39-
ctx.SendChain(message.Text("查询为空!"))
40-
return
41-
}
42-
for i := 0; i < t && i < 10; i++ {
43-
tx += strconv.Itoa(i+1) + ". 法力:" +
44-
gjson.Get(g, `list.`+strconv.Itoa(i)+`.COST`).String() +
45-
" " +
46-
gjson.Get(g, `list.`+strconv.Itoa(i)+`.CARDNAME`).String() +
47-
"\n"
38+
cachefile := cachedir + strconv.Itoa(int(time.Now().Unix()))
39+
err := im.ToFile(cachefile)
40+
if err == nil {
41+
file, err := os.Open(cachefile)
42+
if err == nil {
43+
defer file.Close()
44+
sg, _ := req.Post("https://pic.sogou.com/pic/upload_pic.jsp", req.FileUpload{
45+
File: file,
46+
FieldName: "image", // FieldName 是表单字段名
47+
FileName: "avatar.png", // Filename 是要上传的文件的名称,我们使用它来猜测mimetype,并将其上传到服务器上
48+
})
49+
var tx string
50+
t := int(gjson.Get(g, `list.#`).Int())
51+
if t == 0 {
52+
ctx.Send("查询为空!")
53+
return
54+
}
55+
for i := 0; i < t && i < 10; i++ {
56+
tx += strconv.Itoa(i+1) + ". 法力:" +
57+
gjson.Get(g, `list.`+strconv.Itoa(i)+`.COST`).String() +
58+
" " +
59+
gjson.Get(g, `list.`+strconv.Itoa(i)+`.CARDNAME`).String() +
60+
"\n"
61+
}
62+
ctx.SendChain(
63+
message.Image(sg.String()),
64+
message.Text(tx),
65+
)
66+
}
4867
}
49-
ctx.SendChain(
50-
message.Image(sg.String()),
51-
message.Text(tx),
52-
)
5368
})
54-
//卡组
69+
// 卡组
5570
zero.OnRegex(`^[\s\S]*?(AAE[a-zA-Z0-9/\+=]{70,})[\s\S]*$`).
5671
SetBlock(true).SetPriority(20).Handle(func(ctx *zero.Ctx) {
5772
fmt.Print("成功")

plugin_setutime/setu_geter.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ package setutime
44
import (
55
"fmt"
66
"io"
7-
"log"
87
"net/http"
98
"os"
109
"strconv"
1110
"strings"
1211
"sync"
1312
"time"
1413

14+
"github.com/FloatTech/AnimeAPI/pixiv"
15+
"github.com/sirupsen/logrus"
1516
zero "github.com/wdvxdr1123/ZeroBot"
1617
"github.com/wdvxdr1123/ZeroBot/extension/rate"
1718
"github.com/wdvxdr1123/ZeroBot/message"
18-
19-
"github.com/FloatTech/AnimeAPI/pixiv"
2019
)
2120

2221
// Pools 图片缓冲池
@@ -58,10 +57,13 @@ func newPools() *imgpool {
5857
if err == nil {
5958
defer resp.Body.Close()
6059
if resp.ContentLength > 0 {
61-
log.Printf("[Setu]从镜像下载数据库%d字节...", resp.ContentLength)
60+
logrus.Printf("[Setu]从镜像下载数据库%d字节...", resp.ContentLength)
6261
data, err := io.ReadAll(resp.Body)
6362
if err == nil && len(data) > 0 {
64-
f.Write(data)
63+
_, err = f.Write(data)
64+
if err != nil {
65+
logrus.Errorf("[Setu]写入数据库失败: %v", err)
66+
}
6567
}
6668
}
6769
}

0 commit comments

Comments
 (0)