好的,下面是我用余额$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();所在的代码的一部分。
发布于 2020-01-14 23:28:30
在if语句和语句的实际块之间有一个分号,这使得它总是运行。
以这个例子为例,我们知道if (false) {}应该什么都不做,但是因为有一个分号,所以它什么都不做:
if (false); {
console.log('This shouldn\'t be logged, but it is.');
}
if (false) {
console.log('This doesn\'t get logged.');
}
https://stackoverflow.com/questions/59736611
复制相似问题