0

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

机器人:“${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();

4

1 回答 1

1

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

以这个例子为例,我们知道if (false) {}什么都不应该做,但是因为有一个分号,所以它做了:

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

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

于 2020-01-14T15:28:29.507 回答