为什么在已有数据已经​​存在的情况下,更好的sqlite3不写我的数据?

每当我为已经存在的数据设置sqlite数据时,就不会设置它。因此,如果用户'9'不存在,它将毫无问题地在sqlite表中创建行。如果数据确实存在,则不执行任何操作。

例如试图写用户'9'

之前

{user: '9',timestamp: 10}

END RESULT (在尝试运行时):

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(类文件)中调用函数)

//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函数,这也是一个类文件)


    //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()获得的冷却时间,并且它始终返回相同的值。

D.JS Documentation

erfaya 回答:为什么在已有数据已经​​存在的情况下,更好的sqlite3不写我的数据?

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

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

所以我开始更改代码,以便在想要获得用户时看起来像这样:

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

因此,所有冷却时间都将在一个文件中且在一个表中。我将唯一索引部分更改为此:

CooldownsSqlite.prepare(`CREATE UNIQUE INDEX idx_cooldowns_id ON cooldowns (id);`).run();
本文链接:https://www.f2er.com/2305304.html

大家都在问