Flying Kart
Flying Kart

Reputation: 85

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

Okay, so below is my code to create a database with the balance $0, and underneath that is a test command to test out two argument so that I can get a return message of:

BOT: "${args} ${args2}"

However, whenever I got to test out the command with the two argument definitions, I get an error saying SqliteError: no such table: a (I typed in the command eco test a b, "a" being ${args} and "b" being ${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}`);
    }
});

The error comes from const data = sql.prepare(`SELECT bal FROM ${args}`).get(); according to the console, which is strange since I did not execute the bal command at all, and only did the test command which is not part of the code where const data = sql.prepare(`SELECT bal FROM ${args}`).get(); is at.

Upvotes: 0

Views: 832

Answers (1)

user12251171
user12251171

Reputation:

You have a semicolon between your if statements and the actual block of the statement, which is making it so it always runs.

Take this example, we know if (false) {} should do nothing, but because there's a semicolon it does:

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

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

Upvotes: 1

Related Questions