discord.js better-sqlite3 命令 运行 没有执行命令

discord.js better-sqlite3 command running without the command being executed

好的,下面是我创建一个余额为 0 美元的数据库的代码,下面是一个测试命令,用于测试两个参数,以便我可以获得 return 消息:

机器人:“${args} ${args2}”

但是,每当我用两个参数定义测试命令时,我都会收到一条错误消息,提示 SqliteError: no such table: a(我输入命令 eco test a b,"a" 是 $ {args} 和 "b" 是 ${args2}。

client.on("message", message => {
  if(message.author.bot) return;
  const args = message.content.slice(config.prefix.length).trim().split(' ');
  const command = args.shift().toLowerCase();

  if (command === "testbal") {

  const table = sql.prepare(`SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '${args}';`).get();
  if (!table['count(*)']) {
    // If the table isn't there, create it and setup the database correctly.
    sql.prepare(`CREATE TABLE IF NOT EXISTS ${args} (bal TEXT);`).run();
    // Ensure that the "id" row is always unique and indexed.
    sql.prepare(`CREATE UNIQUE INDEX idx_${args}_id ON ${args} (bal);`).run();
    sql.pragma("synchronous = 1");
    sql.pragma("journal_mode = wal");
  }

  // And then we have two prepared statements to get and set the score data.
  client.getScore = sql.prepare(`SELECT * FROM ${args} WHERE bal = ?`);
  client.setScore = sql.prepare(`INSERT OR REPLACE INTO ${args} (bal) VALUES (@bal);`);
  sql.prepare(`INSERT OR REPLACE INTO ${args} (bal) VALUES (0);`).run();
}

});

//Actual command now

client.on("message", message => {
  const args = message.content.slice(config.prefix.length).trim().split(' ');
  const command = args.shift().toLowerCase();
    if (command === "bal"); {
        if (message.author.bot) return;
        const data = sql.prepare(`SELECT bal FROM ${args}`).get();
      message.channel.send(`You have ${data.bal}`)
  }
});

//TEST ARGS

client.on("message", message => {
    const args = message.content.slice(config.prefix.length).trim().split(' ');
    const args2 = message.content.slice(config.prefix.length).trim(args).split(' ');
    const command = args.shift().toLowerCase();
    if (command === `test`); {
        message.channel.send(`${args} ${args2}`);
    }
});

根据控制台,错误来自const data = sql.prepare(`SELECT bal FROM ${args}`).get();,这很奇怪,因为我根本没有执行bal命令,只执行了test命令,但不是const data = sql.prepare(`SELECT bal FROM ${args}`).get(); 所在的代码的一部分。

您的 if 语句和语句的实际块之间有一个分号,这使得它始终运行。

举个例子,我们知道 if (false) {} 不应该做任何事情,但是因为有一个分号所以它会做:

if (false); {
  console.log('This shouldn\'t be logged, but it is.');
}

if (false) {
  console.log('This doesn\'t get logged.');
}