【问题标题】:discord.js better-sqlite3 command running without the command being executeddiscord.js better-sqlite3 命令在没有执行命令的情况下运行
【发布时间】:2020-01-14 23:28:29
【问题描述】:

好的,下面是我创建余额为 $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(); 所在代码的一部分是在。

【问题讨论】:

    标签: node.js discord.js better-sqlite3


    【解决方案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.');
    }

    【讨论】:

    • 在所有要忘记的事情中,它必须是分号....好的,谢谢!
    猜你喜欢
    • 2014-08-22
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2011-02-25
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多