Enmap (better-sqlite map) 在应用程序重新启动之前不会更新

Posted

技术标签:

【中文标题】Enmap (better-sqlite map) 在应用程序重新启动之前不会更新【英文标题】:Enmap (better-sqlite map) doesnt update until application is restarted 【发布时间】:2020-03-19 22:22:09 【问题描述】:

这是我正在研究的不和谐机器人的代码片段

它需要从 Enmap 加载数据,将信息添加到该 Array,然后将其推回。

它成功地提取数据,进行所有的转换和添加等 并成功推送该数据

但是,在程序重新启动之前,它无法提取新数据。它被保存了,但是 let locationDataArray = await data.get(locationDataKey); 除非重新启动程序,否则似乎不会拉出这个新版本的地图

我很困惑

const Commando = require('discord.js-commando');
const levenshtein = require('fast-levenshtein');


const Enmap = require("enmap");
const data = new Enmap(
    name:"locationdata"
);

const settingsMap = new Enmap(
    name:"locationSettings"
)

module.exports = class addConnection extends Commando.Command
    constructor(client)
        super(client, 
            name:"addconnection",
            group:"management",
            aliases:["connect"],
            memberName:"addconnection",
            userPermissions:['ADMINISTRATOR'],
            description:"adds a connection between two zones",
            examples: ['$addconnection <zone1>,<zone2>'],
            throttling: 
                usages: 1,
                duration: 5
            ,
            args:[
                key:'destinations',
                prompt:'what are the two areas you want to connect (seperate them with a comma , )',
                type:'string'
            ]
        );
    

    async run(msg,args)

        //get our guilds Information
        let guildID = msg.guild.id;
        let locationDataKey = guildID+"_mapData";
        let locationDataArray = await data.get(locationDataKey);

        //Our Guild Settings
        let settingMapKey = guildID+"_setting";
        let settings = await settingsMap.get(settingMapKey);


        //chiefly out npcRoleID
        let npcRole = await msg.guild.roles.get(settings.npcRoleID);

        let connectionArray = await args.destinations.toLowerCase().split(",");

        for(var i = 0; i < connectionArray.length; i++)
            //make sure the item is valid
            var distance = levenshtein.get(connectionArray[i], locationDataArray[0].name);
            var closestDistance = distance;
            var closestWord = locationDataArray[0].name;

            for(var j = 0; j < locationDataArray.length; j++)
                distance = levenshtein.get(connectionArray[i], locationDataArray[j].name);
                if (distance < closestDistance)
                    closestDistance = distance;
                    closestWord = locationDataArray[j].name;
                
            

            //make sure all the areas are valid and good
            if(closestDistance < (closestWord.length/2))
                connectionArray[i] = closestWord;
            
            else
                msg.channel.send("those channels don't seem to exist").then( msg => 
                    msg.delete(10000);
                );
                return;
              

        
        //our array of connections now only contains valid options
        //loop over our location data array
        for(var i = 0; i< connectionArray.length; i++)
            for(var j = 0; j < locationDataArray.length; j++)
                //when we hit one of out LDA that has the same name as something in the connectionArray
                //stop add the rest of the connection array to its connections
                if(locationDataArray[j].name === connectionArray[i])
                    for(var k = 0; k < connectionArray.length; k++)
                        if(locationDataArray[j].name == connectionArray[k])

                        
                        else
                            if(!locationDataArray[j].connections.includes(connectionArray[k]))
                                await locationDataArray[j].connections.push(connectionArray[k]);
                            

                            //get the role for the connection and the current channel
                            let role = await msg.guild.roles.find(role => role.name === connectionArray[k]);
                            let currentChannel = await msg.guild.channels.find(channel => channel.name === locationDataArray[j].channelName);


                            //the connection can read but not type in its connection
                            currentChannel.overwritePermissions(
                            role,
                            
                                'SEND_MESSAGES':false,
                                'VIEW_CHANNEL':true,
                                'READ_MESSAGE_HISTORY':true
                            
                            )


                            data.set(locationDataKey, locationDataArray);
                            msg.channel.send("Connected "+locationDataArray[j].name+" and "+connectionArray[k]);
                        
                    
                

            
           
    

【问题讨论】:

【参考方案1】:

我知道这个问题很老了,我希望你很久以前就解决了这个问题,但为了后代,因为人们以后可能会发现这个问题,我还是会回答它。

发生这种情况的原因是您在此文件中创建了一个新的 Enmap 实例,并且可能您也在其他文件中声明了它。这会导致为 Enmap 创建一个新的“客户端”,具有自己的缓存,并且 Enmap 无法更新这些实例。

解决方案是将您的 Enmap 附加到您随身携带的变量(例如客户端),或者创建一个单独的节点模块,您可以将它导入到您需要的任何地方。 Enmap 文档中更详细地描述了这两个选项:https://enmap.evie.dev/usage/using-from-multiple-files

免责声明:我是 Enmap 的作者

【讨论】:

以上是关于Enmap (better-sqlite map) 在应用程序重新启动之前不会更新的主要内容,如果未能解决你的问题,请参考以下文章

邀请/查看成员加入部落 - Discord.js 和 Enmap

无法安装 better-sqlite3 discord.js

使用 better-sqlite3 创建新表

错误:找不到绑定文件 better-sqlite3.node

无法在我的 Raspberry PI 上安装 Better-Sqlite3

在 Angular + Electron 应用程序中使用 better-sqlite3:模块解析失败:意外字符