55 "fmt"
66 "strconv"
77 "strings"
8+ "sync"
89 "time"
910
1011 ctrl "github.com/FloatTech/zbpctrl"
@@ -15,7 +16,14 @@ import (
1516 "github.com/wdvxdr1123/ZeroBot/message"
1617)
1718
18- var wmitems , itemNames = newwm ()
19+ var wmdr sync.RWMutex
20+
21+ type wmdata struct {
22+ wmitems map [string ]items
23+ itemNames []string
24+ }
25+
26+ var wd , _ = newwm ()
1927
2028func init () {
2129 eng := control .Register ("warframeapi" , & ctrl.Options [* zero.Ctx ]{
@@ -234,10 +242,12 @@ func init() {
234242 ctx .SendChain (message .Text ("已拉取服务器时间并同步到本地模拟" ))
235243 })
236244 // 根据名称从Warframe市场查询物品售价
237- eng .OnPrefix (".wm " ).SetBlock (true ).
245+ eng .OnPrefix (".wm " , checknwm ).SetBlock (true ).
238246 Handle (func (ctx * zero.Ctx ) {
239247 // 根据输入的名称, 从游戏物品名称列表中进行模糊搜索
240- sol := fuzzy .FindNormalizedFold (ctx .State ["args" ].(string ), itemNames )
248+ wmdr .RLock ()
249+ sol := fuzzy .FindNormalizedFold (ctx .State ["args" ].(string ), wd .itemNames )
250+ wmdr .RUnlock ()
241251 // 物品名称
242252 var name string
243253
@@ -282,17 +292,20 @@ func init() {
282292 if onlymaxrank {
283293 msgs = msgs [:0 ]
284294 }
285- sells , iteminfo , txt , err := getitemsorder (wmitems [name ].URLName , onlymaxrank )
295+
296+ sells , iteminfo , txt , err := getitemsorder (wd .wmitems [name ].URLName , onlymaxrank )
286297 if ! onlymaxrank {
298+ wmdr .RLock ()
287299 if iteminfo .ZhHans .WikiLink == "" {
288300 msgs = append (msgs , ctxext .FakeSenderForwardNode (ctx ,
289- message .Image ("https://warframe.market/static/assets/" + wmitems [name ].Thumb ),
290- message .Text ("\n " , wmitems [name ].ItemName )))
301+ message .Image ("https://warframe.market/static/assets/" + wd . wmitems [name ].Thumb ),
302+ message .Text ("\n " , wd . wmitems [name ].ItemName )))
291303 } else {
292304 msgs = append (msgs , ctxext .FakeSenderForwardNode (ctx ,
293- message .Image ("https://warframe.market/static/assets/" + wmitems [name ].Thumb ),
294- message .Text ("\n " , wmitems [name ].ItemName , "\n wiki: " , iteminfo .ZhHans .WikiLink )))
305+ message .Image ("https://warframe.market/static/assets/" + wd . wmitems [name ].Thumb ),
306+ message .Text ("\n " , wd . wmitems [name ].ItemName , "\n wiki: " , iteminfo .ZhHans .WikiLink )))
295307 }
308+ wmdr .RUnlock ()
296309 }
297310
298311 if err != nil {
0 commit comments