当现有数据已经​​存在时,为什么 better-sqlite3 不写入我的数据?

Why is better-sqlite3 not writing my data when existent data already exists?

每当我为已经存在的数据设置sqlite数据时,它不会设置它。因此,如果用户 '9' 不存在,它会毫无问题地在 sqlite table 中创建行。如果数据确实存在,它什么也不做。

例如试图写信给用户“9”

之前

{user: '9', timestamp: 10}

结束结果(尝试运行):

this._client.setCooldown[this.name].run({user: '9', timestamp: 20})
//this.name = command name
//this.client = discord.js client | setCooldown is an array. the [this.name] represents the command name that i want to cooldown for a certain user
this._client.getCooldown[this.name].get('9')
//doesn't return {user: '9', timestamp: 20} which is expected
//instead returns {user: '9', timestamp: 10} which is the actual result

控制台没有显示错误。以下是重要文件的片段,顺序为 运行

index.js(用于创建客户端并调用另一个文件。)

var djs = require('discord.js');
var client = new djs.client();

// code

require('./sqlitecreator.js')(client);

// code

sqlitecreator.js(这个文件基本上创建了一些与这个问题无关的其他sqlite文件,并创建了一个客户端集合,其中包含命令名称及其各自的目录供另一个文件读取,并创建冷却时间文件)

var sqlite3 = require('better-sqlite3');
var CooldownsSqlite = sqlite(`./src/util/essentials/util-cache/Cooldowns.sqlite`)
//verifiednames is an array of names where every string is unique.
//bot is the client from index.js
    bot.getCooldown = [];
    bot.setCooldown = [];

    for(var verifiedname of verifiednames) {
      var cooldownsqlitetable = CooldownsSqlite.prepare(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '${verifiedname}';`).get();
      if (!cooldownsqlitetable['count(*)']) {
          console.log('Command Cooldowns Not Prepared!')
          CooldownsSqlite.prepare(`CREATE TABLE ${verifiedname} (user TEXT, timestamp INTEGER);`).run();
          CooldownsSqlite.prepare(`CREATE UNIQUE INDEX idx_${verifiedname}_user ON ${verifiedname} (user, timestamp);`).run();
          CooldownsSqlite.pragma("synchronous = 1");
          CooldownsSqlite.pragma("journal_mode = wal");
      }
      bot.getCooldown[verifiedname] = CooldownsSqlite.prepare(`SELECT * FROM ${verifiedname} WHERE user = ?`);
      bot.setCooldown[verifiedname] = CooldownsSqlite.prepare(`INSERT OR REPLACE INTO ${verifiedname} (user, timestamp) VALUES (@user, @timestamp);`);
    }


  } catch (e) {
    console.error(e);
    process.exit(1);
  }

message.js(当客户端收到消息事件时启动,从上面的集合中获取目录,还从 Command.js(class 文件)调用函数)

//file basically checks if message begins with prefix
//...
    command.executable(message); // checks if the command can run (checks permissions, uses bot.getCooldown function from the previous file.
    if(command.executable(message) !== 'finished') return;
    command.throttle(message.author); // is supposed to run the bot.setCooldown function

command.js(这个文件使用了set/getCooldown函数,这也是一个class文件)


    //occurances of get/setCooldown
    executable(...) {
//...
        if(this._client.getCooldown[this.name.toLowerCase()].get(msg.author.id)) {
        //.. executes another function
        }
//..
    }
   //...

    /**
     * Cooldown a user.
     * @param {Discord.User} user 
     * @param {Date} now 
     * @returns {void}
     */
    async throttle(user, now = Date.now()) {
        if(this._client.getCooldown[this.name.toLowerCase()]) return;
        var cd = {user: user.id,timestamp: now} ;
        //i thought it was a problem with the setCooldown function running before the object was created, but this didn't work.
        await this._client.setCooldown[this.name.toLowerCase()].run(cd);
        return;
    } 

在 message.js 文件中,我 console.log() 我得到的冷却时间,它总是 returns 相同的值。

D.JS Documentation

当我准备 Cooldowns.sqlite 文件时,我看到了这段代码:

CooldownsSqlite.prepare(`CREATE UNIQUE INDEX idx_${verifiedname}_user ON ${verifiedname} (user, timestamp);`).run();

所以我开始修改我的代码,这样当我想要获得用户时它看起来像这样:

{
id: '9-command',
user: '9',
command: 'command',
timestamp: 20
}

所以所有的冷却时间都在一个文件中,在一个 table 中。我将唯一索引部分更改为:

CooldownsSqlite.prepare(`CREATE UNIQUE INDEX idx_cooldowns_id ON cooldowns (id);`).run();