diff --git a/rikkabot/plugins/defaultPlugin.go b/rikkabot/plugins/defaultPlugin.go index d36bd2d..962184a 100644 --- a/rikkabot/plugins/defaultPlugin.go +++ b/rikkabot/plugins/defaultPlugin.go @@ -113,6 +113,9 @@ func (af *autoAddNewFriend) recoverCache() { af.firstRecoverFlag = true c := af.onceDialogM.Cache.GetPluginCacheByName(autoAddnewfriendName) bCache, err := json.Marshal(c) + if err != nil { + logging.ErrorWithErr(err, "recover auto-add-friend-cache fail") + } err = json.Unmarshal(bCache, &af.Cache) if err != nil { logging.ErrorWithErr(err, "recover auto-add-friend-cache fail") diff --git a/rikkabot/processor/cache/cache.go b/rikkabot/processor/cache/cache.go index 81dcd5b..5ae6c7f 100644 --- a/rikkabot/processor/cache/cache.go +++ b/rikkabot/processor/cache/cache.go @@ -45,14 +45,32 @@ type cacheExported struct { // EnablePluginMap 获取插件状态列表 插件名-状态 func (c *Cache) EnablePluginMap() map[string]bool { c.mu.RLock() - defer c.mu.RUnlock() cpEnablePlugins := make(map[string]bool, len(c.EnablePlugins)) + needDelPlugins := make([]string, 0, len(c.EnablePlugins)) + for k, _ := range c.EnablePlugins { // 需要删除的插件信息 + if !register.GetPluginPool().IsExistPlugin(k) { // 已经不在注册列表中 + needDelPlugins = append(needDelPlugins, k) + } + } for k, v := range c.EnablePlugins { - cpEnablePlugins[k] = v + if register.GetPluginPool().IsExistPlugin(k) { + cpEnablePlugins[k] = v + } + } + c.mu.RUnlock() + for _, plugin := range needDelPlugins { // 删除插件信息 + c.delPlugin(plugin) } return cpEnablePlugins } +// 删除插件信息 +func (c *Cache) delPlugin(pluginName string) { + c.mu.Lock() + defer c.mu.Unlock() + delete(c.EnablePlugins, pluginName) +} + // 插件是否存在 func (c *Cache) isExistPlugin(pluginName string) bool { _, ok := c.EnablePlugins[pluginName] diff --git a/rikkabot/processor/register/register.go b/rikkabot/processor/register/register.go index a9baa2a..0201be7 100644 --- a/rikkabot/processor/register/register.go +++ b/rikkabot/processor/register/register.go @@ -41,6 +41,13 @@ func (p *PluginRegister) GetPlugin(name string) IPlugin { return p.Plugins[name] } +func (p *PluginRegister) IsExistPlugin(name string) bool { + if p.GetPlugin(name) == nil { + return false + } + return true +} + func (p *PluginRegister) GetPluginMap() map[string]IPlugin { p.mu.RLock() defer p.mu.RUnlock()