discord js-从sqlite检索信息并存储在数组中

zvokhttg  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(141)

所以,这件事已经让我发疯好长一段时间了,我终于屈服了,在这里提出了这个问题。作为对应该发生的事情的概述:
根据特定文本,在discord频道中接收消息
从消息中提取的信息(正则表达式/查找的组合)
写入sqlite数据库的信息(3列)
查询sqlite db以查找该成员在过去一小时内提出的所有索赔
步骤1-3似乎工作正常,信息被捕获并写入数据库。然而,我已经尝试了无数种方法来收回信息,并且搜索/研究了比我愿意承认的更多的信息,但我似乎无法让它发挥作用。
问题是。。。结果应始终至少有一个结果(即刚刚写入数据库的结果),因此不应存在此值为0的机会。有时,在以前的方法中,结果已返回并存储在数组中,有时数组返回为空。

client.on('message', async message => {
    if (message.channel.id === chan_role.channels.register) {
        // The post is in the register channel
        if (message.content.includes('doing business with you')) {
            // it's a successful claim
            Rclaimedby = message.content.match(/\*\*(.*?)\*\*/)[1];
            let serverMembers = client.guilds.cache.get(chan_role.serverID).members;
            let Ruser = serverMembers.cache.find(m => m.displayName == Rclaimedby);
            Rdatetime = moment(message.createdAt);

            // Update the database with the win information
            await update_customer_db(message.channel.id, Ruser.id, message.createdTimestamp)
                .then(console.log(`Updated DB (Channel: ${message.channel.id} | Winner: ${Ruser.id} | WinTime: ${Rdatetime})`))
                .catch(console.error)

            // Check if the member is a Customer Booster and highlight accordingly
            if (Ruser.roles.cache.has(chan_role.roles.custboost)) {
                custboost = "<:qsoisnotresponsiblefornolife:800654013300736030> Customer Booster";
            } else {
                custboost = "Normal Member";
            }

            // Now we want to try to bring the information back from the database to check if
            // they have had multiple claims within the last 60 minutes.
            //claims = await get_customer_claims(`${Ruser.user.id}`,`${message.createdTimestamp}`);
            await get_customer_claims(`${Ruser.user.id}`, `${message.createdTimestamp}`)
                .then(results => {
                    if (results.length > 0) {
                        console.log(results.length)
                        console.log(results)
                    } else {
                        console.log(`It's empty apparently`)
                        console.log(results)
                    }
                })
                .catch(error => {
                    console.log(error)
                });

            REmbed = generate_customer_embed(message.channel.id, `${Ruser.user.id}`, `${custboost}`);
            client.channels.cache.get(chan_role.testchannels.botroom).send(REmbed);
            console.log(`${message.createdAt} - #Register - ${Rclaimedby} - ${custboost}`);
            //console.log(claims)
        }
    }

});

这是代码的最新版本-以下函数为当前函数(不起作用)

async function update_customer_db(chan, win, timestamp) {
    // This function will update the customer table within the database so that we
    // can query that later for repeat winners.
    let db = new sqlite.Database('./chezbot.db', sqlite.OPEN_READWRITE);
    let insertdata = db.prepare(`INSERT INTO customer VALUES (?,?,?)`);
    insertdata.run(win, chan, timestamp);
    insertdata.finalize();
    db.close();
}

//function get_customer_claims(winner, timestamp, callback)

async function get_customer_claims(winner, timestamp) {
    hourago = timestamp - 3600000
    let db = new sqlite.Database('./chezbot.db', sqlite.OPEN_READONLY);
    sql = "SELECT * FROM customer WHERE userid = \"" + winner + "\" AND wintime BETWEEN " + hourago + " AND " + timestamp + " ORDER BY wintime ASC"
    console.log(sql);
    return new Promise((resolve, reject) => {
        db.all(sql,(err, results) => {
            if (err) return reject(err);
            return resolve(results);
        });
    });

}

我也尝试过类似的方法:

// Update the database with the win information
            await update_customer_db(message.channel.id, Ruser.id, message.createdTimestamp)
                .then(await get_customer_claims(Ruser.id,message.createdTimestamp,function(claims){
                    console.log(claims);
                }))
                .catch(console.error)

其功能如下:

async function get_customer_claims(winner, timestamp, callback) {
    console.log(`Winner: ${winner} | Timestamp: ${timestamp}`);
    let db = new sqlite.Database('./chezbot.db', sqlite.OPEN_READONLY);
    var claims = []; // we'll return the customer claims in here

    db.serialize(function() {
        db.each("SELECT * FROM customer WHERE userid = \"" + winner + "\" AND wintime BETWEEN (" + timestamp + "-3600000) AND " + timestamp + " ORDER BY wintime ASC", function(err,row) {
            claims.push(row);
        }, function() {
            db.close();
            callback(claims);
        });
    });
}

在我尝试过的各种选项中,它似乎要么显示一些成员的数组信息,要么只是一个空白数组。如上所述,不应该有空返回的机会,因为我们已经在其中写入了一条记录。
期望的结果。。。对于第一部分,我实际上只关心发现了多少个结果,这将导致另一个决定(如果他们是正常成员,那么允许他们提出2个索赔。。。如果他们是助推器,他们可以申请4次。。。更多-他们会沉默一段时间)。但是,由于每次都没有返回结果,我不能确信它会在正确的时间静音。
it运行示例:

在前两个矩形中,它返回了结果(每个成员在该小时只有一个结果),但在第三个矩形中,它返回为空。

第二个ss显示该成员的结果实际上已写入数据库-因此应该在查询中返回。
另一个问题是,有时它不会返回下面屏幕截图中显示的所有结果。第一个矩形显示这为这个成员返回了一个[](即使它已经存储了它)——第二个矩形显示他们在大约5分钟后又得到了一个[]。。。但是返回的结果(查看wintime)是原始声明-因此这次没有包括第二个声明。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题