diff --git a/api/dice_public.go b/api/dice_public.go index db7974f5..6ac1ee1c 100644 --- a/api/dice_public.go +++ b/api/dice_public.go @@ -2,6 +2,7 @@ package api import ( "net/http" + "time" "sealdice-core/dice" @@ -41,5 +42,7 @@ func dicePublicSet(c echo.Context) error { myDice.PublicDiceInfoRegister() myDice.PublicDiceEndpointRefresh() myDice.PublicDiceSetupTick() + myDice.LastUpdatedTime = time.Now().Unix() + myDice.Save(false) return Success(&c, Response{}) } diff --git a/api/helpdoc.go b/api/helpdoc.go index df4bd9eb..4838b83e 100644 --- a/api/helpdoc.go +++ b/api/helpdoc.go @@ -47,7 +47,6 @@ func helpDocReload(c echo.Context) error { dm.Help.Close() dm.InitHelp() - dm.AddHelpWithDice(dm.Dice[0]) return Success(&c, Response{}) } return Error(&c, "帮助文档正在重新装载", Response{}) diff --git a/dice/builtin_commands.go b/dice/builtin_commands.go index e1199ef7..06379c7f 100644 --- a/dice/builtin_commands.go +++ b/dice/builtin_commands.go @@ -442,7 +442,6 @@ func (d *Dice) registerCoreCommands() { dm.Help.Close() dm.InitHelp() - dm.AddHelpWithDice(dm.Dice[0]) ReplyToSender(ctx, msg, "帮助文档已经重新装载") } else { ReplyToSender(ctx, msg, "帮助文档正在重新装载,请稍后...") @@ -1122,7 +1121,6 @@ func (d *Dice) registerCoreCommands() { dm.IsHelpReloading = true dm.Help.Close() dm.InitHelp() - dm.AddHelpWithDice(dice) ReplyToSender(ctx, msg, "帮助文档已重载") } else { ReplyToSender(ctx, msg, "帮助文档正在重新装载") diff --git a/dice/dice_help.go b/dice/dice_help.go index a26378b0..a80c0ee8 100644 --- a/dice/dice_help.go +++ b/dice/dice_help.go @@ -57,7 +57,6 @@ func (e HelpTextItems) Len() int { type HelpManager struct { CurID uint64 - Parent *DiceManager EngineType EngineType LoadingFn string HelpDocTree []*HelpDoc @@ -122,7 +121,7 @@ func (m *HelpManager) Close() { _ = os.RemoveAll("./_help_cache") } -func (m *HelpManager) Load() { +func (m *HelpManager) Load(internalCmdMap CmdMapCls, extList []*ExtInfo) { m.loadSearchEngine() _ = m.AddItem(docengine.HelpTextItem{ @@ -184,7 +183,7 @@ func (m *HelpManager) Load() { totalEntries := len(entries) for i, entry := range entries { progress := float64(i+1) / float64(totalEntries) * 100 - log.Infof("处理帮助文档组[文件夹]: 当前帮助文档加载总进度: %s %.2f%% (%d/%d)", entry.Name(), progress, i+1, totalEntries) + log.Infof("[帮助文档] 处理用户定义帮助文档组[文件夹]: 当前帮助文档加载进度: %s %.2f%% (%d/%d)", entry.Name(), progress, i+1, totalEntries) if strings.HasPrefix(entry.Name(), ".") { continue } @@ -219,7 +218,29 @@ func (m *HelpManager) Load() { }) m.HelpDocTree = append(m.HelpDocTree, &child) } - _ = m.AddItemApply(true) + err = m.AddItemApply(false) + if err != nil { + log.Errorf("加载用户自定义帮助文档出现异常!: %v", err) + } + log.Infof("[帮助文档] 用户定义的帮助文档组已加载完成!") + log.Infof("[帮助文档] 正在处理指令相关(含插件)帮助文档组") + err = m.addInternalCmdHelp(internalCmdMap) + if err != nil { + log.Errorf("加载内置指令帮助文档出现异常: %v", err) + } + err = m.AddItemApply(false) + if err != nil { + log.Errorf("加载内置指令帮助文档出现异常: %v", err) + } + err = m.addExternalCmdHelp(extList) + if err != nil { + log.Errorf("加载插件指令帮助文档出现异常: %v", err) + } + err = m.AddItemApply(true) + if err != nil { + log.Errorf("加载插件指令帮助文档出现异常: %v", err) + } + log.Infof("[帮助文档] 指令相关(含插件)帮助文档组已加载完成!") m.CurID = m.searchEngine.GetTotalID() elapsed := time.Since(start) // 计算执行时间 log.Infof("帮助文档加载完毕,共耗费时间: %s 共计加载条目:%d\n", elapsed, m.CurID) @@ -409,35 +430,51 @@ out: return synonymCount, nil } -func (dm *DiceManager) AddHelpWithDice(dice *Dice) { - m := dm.Help - - addCmdMap := func(packageName string, cmdMap CmdMapCls) { - for k, v := range cmdMap { - content := v.Help - if content == "" { - content = v.ShortHelp - } - _ = m.AddItem(docengine.HelpTextItem{ - Group: HelpBuiltinGroup, - Title: k, - Content: content, - PackageName: packageName, - }) +func (m *HelpManager) addCmdMap(packageName string, cmdMap CmdMapCls) error { + for k, v := range cmdMap { + content := v.Help + if content == "" { + content = v.ShortHelp } + err := m.AddItem(docengine.HelpTextItem{ + Group: HelpBuiltinGroup, + Title: k, + Content: content, + PackageName: packageName, + }) + if err != nil { + log.Errorf("AddCmdMapItem err:%v", err) + return err + } + } + return nil +} + +func (m *HelpManager) addInternalCmdHelp(cmdMap CmdMapCls) error { + err := m.addCmdMap("核心指令", cmdMap) + if err != nil { + return err } + return nil +} - addCmdMap("核心指令", dice.CmdMap) - for _, i := range dice.ExtList { - _ = m.AddItem(docengine.HelpTextItem{ +func (m *HelpManager) addExternalCmdHelp(ext []*ExtInfo) error { + for _, i := range ext { + err := m.AddItem(docengine.HelpTextItem{ Group: HelpBuiltinGroup, Title: i.Name, Content: i.GetDescText(i), PackageName: "扩展模块", }) - addCmdMap(i.Name, i.CmdMap) + if err != nil { + return err + } + err = m.addCmdMap(i.Name, i.CmdMap) + if err != nil { + return err + } } - _ = m.AddItemApply(false) + return nil } func (m *HelpManager) AddItem(item docengine.HelpTextItem) error { diff --git a/dice/dice_manager.go b/dice/dice_manager.go index 3d0704de..b5dc0b85 100644 --- a/dice/dice_manager.go +++ b/dice/dice_manager.go @@ -112,9 +112,12 @@ func (dm *DiceManager) InitHelp() { dm.IsHelpReloading = true _ = os.MkdirAll("./data/helpdoc", 0755) dm.Help = new(HelpManager) - dm.Help.Parent = dm dm.Help.EngineType = EngineType(dm.HelpDocEngineType) - dm.Help.Load() + if len(dm.Dice) == 0 { + log.Fatalf("Dice实例不存在!") + return + } + dm.Help.Load(dm.Dice[0].CmdMap, dm.Dice[0].ExtList) dm.IsHelpReloading = false } @@ -255,12 +258,8 @@ func (dm *DiceManager) InitDice() { } } }() - // 加载帮助 dm.InitHelp() - if len(dm.Dice) >= 1 { - dm.AddHelpWithDice(dm.Dice[0]) - } }() dm.ResetAutoBackup() diff --git a/dice/docengine/bleve.go b/dice/docengine/bleve.go index 4692989d..492f9f0c 100644 --- a/dice/docengine/bleve.go +++ b/dice/docengine/bleve.go @@ -120,7 +120,6 @@ func (d *BleveSearchEngine) AddItem(item HelpTextItem) (string, error) { // AddItemApply 这里认为是真正执行插入文档的逻辑 // 由于现在已经将执行函数改为了可按文件执行,所以可以按文件进行Apply,这应当不会有太大的量级。 // end代表是否是最后一次执行,一般用在所有的数据都处理完之后,关闭逻辑的时候使用,如bleve batch重复利用后最后销毁 -// TODO: 似乎很奇怪,这家伙貌似不会回收内存的吗? func (d *BleveSearchEngine) AddItemApply(end bool) error { if d.batch != nil { // 执行batch @@ -130,11 +129,10 @@ func (d *BleveSearchEngine) AddItemApply(end bool) error { } // 如果是最后一批 if end { - // 销毁batch d.batch.Reset() d.batch = nil } else { - // 否则重置batch + // 否则仅重置batch d.batch.Reset() } return err diff --git a/dice/model/group_info.go b/dice/model/group_info.go index 42e0b9fa..3f22619a 100644 --- a/dice/model/group_info.go +++ b/dice/model/group_info.go @@ -135,7 +135,8 @@ func GroupPlayerInfoGet(db *gorm.DB, groupID string, playerID string) *GroupPlay result := db.Model(&GroupPlayerInfoBase{}). Where("group_id = ? AND user_id = ?", groupID, playerID). Select("name, last_command_time, auto_set_name_template, dice_side_num"). - First(&ret) + Limit(1). + Find(&ret) err := result.Error // 如果查询发生错误,打印错误并返回 nil if err != nil { diff --git a/dice/platform_adapter_lagrange_helper.go b/dice/platform_adapter_lagrange_helper.go index d93c64f0..62c98856 100644 --- a/dice/platform_adapter_lagrange_helper.go +++ b/dice/platform_adapter_lagrange_helper.go @@ -429,21 +429,21 @@ func LagrangeGetSignInfo(dice *Dice) ([]SignInfo, error) { cachePath := filepath.Join(dice.BaseConfig.DataDir, "extra/SignInfo.cache") signInfo, err := lagrangeGetSignInfoFromCloud(cachePath) if err == nil && len(signInfo) > 0 { - copy(signInfoGlobal, signInfo) + signInfoGlobal = append([]SignInfo(nil), signInfo...) return signInfo, nil } dice.Logger.Infof("无法从云端获取SignInfo,即将读取本地缓存数据, 原因: %s", err.Error()) signInfo, err = lagrangeGetSignInfoFromCache(cachePath) if err == nil && len(signInfo) > 0 { - copy(signInfoGlobal, signInfo) + signInfoGlobal = append([]SignInfo(nil), signInfo...) return signInfo, nil } dice.Logger.Infof("无法从本地缓存获取SignInfo,即将读取内置数据, 原因: %s", err.Error()) if err = json.Unmarshal([]byte(signInfoJson), &signInfo); err == nil { lagrangeGetSignServerLatency(signInfo) - copy(signInfoGlobal, signInfo) + signInfoGlobal = append([]SignInfo(nil), signInfo...) return signInfo, nil } dice.Logger.Infof("无法从内置数据获取SignInfo,请联系开发者上报问题, 原因: %s", err.Error()) diff --git a/dice/version.go b/dice/version.go index fe728484..d32a46a3 100644 --- a/dice/version.go +++ b/dice/version.go @@ -21,7 +21,7 @@ var ( // APP_CHANNEL 更新频道,stable/dev,在 action 构建时自动注入 APP_CHANNEL = "dev" //nolint:revive - VERSION_CODE = int64(1004100) //nolint:revive + VERSION_CODE = int64(1004101) //nolint:revive VERSION_JSAPI_COMPATIBLE = []*semver.Version{ VERSION,