如何在SQLite node.js中为经济系统创建另一个表和列?

时间:2019-12-17 04:19:21

标签: javascript node.js sqlite

好的,这就是我想要做的。目前,我有一个机器人可以记录问题并在不和谐的服务器中记录人们的得分。我正在为此创建一个更好的经济系统,该系统使用 fa bal1 作为命令并显示得分。但是,每当我为经济系统键入相同的代码时,要么重置当前积分系统中的积分,要么在积分系统中使用该命令。

这是当前的积分系统+经济系统代码

client.on("ready", () => {
  sql.prepare("CREATE TABLE IF NOT EXISTS bal (id TEXT PRIMARY KEY, user TEXT, guild TEXT, bal1 INTEGER, bal2 INTEGER, bal3 INTEGER,  bal4 INTEGER, bal5 INTEGER, bal6 INTEGER, bal7 INTEGER, bal8 INTEGER, bal9 INTEGER, bal10 INTEGER, bal11 INTEGER, bal12 INTEGER, bal13 INTEGER, bal14 INTEGER, bal15 INTEGER, bal16 INTEGER, bal17 INTEGER, bal18 INTEGER, bal19 INTEGER, bal20 INTEGER);");
  const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
  const table2 = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'bal';").get();
  if (!table['count(*)']) {
    // If the table isn't there, create it and setup the database correctly.
    sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
    // Ensure that the "id" row is always unique and indexed.
    sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").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 scores WHERE user = ? AND guild = ?");
  client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");

  if (!table2['count(*)']) {
    // If the table isn't there, create it and setup the database correctly.
    sql.prepare("CREATE TABLE bal (id TEXT PRIMARY KEY, user TEXT, guild TEXT, bal1 INTEGER, bal2 INTEGER, bal3 INTEGER,  bal4 INTEGER, bal5 INTEGER, bal6 INTEGER, bal7 INTEGER, bal8 INTEGER, bal9 INTEGER, bal10 INTEGER, bal11 INTEGER, bal12 INTEGER, bal13 INTEGER, bal14 INTEGER, bal15 INTEGER, bal16 INTEGER, bal17 INTEGER, bal18 INTEGER, bal19 INTEGER, bal20 INTEGER);").run();
    // Ensure that the "id" row is always unique and indexed.
    sql.prepare("CREATE UNIQUE INDEX idx_bal_id ON bal (id);").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 bal WHERE user = ? AND guild = ?");
  client.setScore = sql.prepare("INSERT OR REPLACE INTO bal (id, user, guild, bal1, bal2, bal3, bal4, bal5, bal6, bal7, bal8, bal9, bal10, bal11, bal12, bal13, bal14, bal15, bal16, bal17, bal18, bal19, bal20) VALUES (@id, @user, @guild, @bal1, @bal2, @bal3, @bal4, @bal5, @bal6, @bal7, @bal8, @bal9, @bal10, @bal11, @bal12, @bal13, @bal14, @bal15, @bal16, @bal17, @bal18, @bal19, @bal20);");
  client.channels.get('588901365200322570').send("Bot Ready!")
  client.channels.get('588901365200322570').send("https://tenor.com/TYHP.gif")
  client.user.setActivity(`BV-4.0.0-001`);

});

积分系统代码

client.on("message", message => {
  if (message.author.bot) return;
  let score;
    if (message.guild) {
      score = client.getScore.get(message.author.id, message.guild.id);
      if (!score) {
        score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1}

      }

  }
  if (message.content.indexOf(config.prefix) !== 0) return;

  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  // Command-specific code here!

  if(command === "xp") {
    return message.channel.send(`You currently have ${score.points} xp`)
  }
  if(command === "level") {
    return message.channel.send(`You are currently in level ${score.level}`)
  }
  if (command == "give") {

    // Only server owner (me) can do this commands
    if(!message.author.id === message.guild.owner) return message.reply("Sorry, but you don't have enough permissions to run this command");

    const user = message.mentions.users.first() || client.users.get(args[0]);
    if(!user) return message.reply("Please @ the person or give me their ID");

    const pointsToAdd = parseInt(args[1], 10);
    if(!pointsToAdd) return message.reply("How much xp? You didn't tell me that!");

    //Get their current XP
    let userscore = client.getScore.get(user.id, message.guild.id);
    if(!userscore) {
      userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, points: 0, level: 1}

    }

    //XP level update
    userscore.points += pointsToAdd;
    let userLevel = Math.floor(0.1 * Math.sqrt(score.points));
    userscore.level = userLevel;

    client.setScore.run(userscore);
    return message.channel.send(`${user.tag} has received ${pointsToAdd}`);
  }

  if (command == "take") {
    if(!message.author.id === message.guild.owner) return message.reply("Sorry, but you don't have enough permissions to run this command");

    const user = message.mentions.users.first() || client.users.get(args[0]);
    if(!user) return message.reply("Please @ the person or give me their ID");

    const pointsToRemove = parseInt(args[1], 10);
    if(!pointsToRemove) return message.reply("How much xp? You didn't tell me that!");

    //Get their current XP

    let userscore = client.getScore.get(user.id, message.guild.id);
    if(!userscore) {
      userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, points: 0, level: 1}
    }


    //XP level update
    userscore.points -= pointsToRemove;
    let userLevel = Math.floor(-0.1 * Math.sqrt(score.points));
    userscore.level = userLevel;

    client.setScore.run(userscore);
    return message.channel.send(`${user.tag} has lost ${pointsToRemove}`);
  }
});

余额代码(由于这是测试代码,因此仅bal1)

client.on("message", message => {
  if (message.author.bot) return;
  let bal;
    if (message.guild) {
      bal = client.getScore.get(message.author.id, message.guild.id);
      if (!bal) {
        bal = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, bal1: 0}

      }

  }
  if (message.content.indexOf(config.prefix) !== 0) return;

  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();

  // Command-specific code here!

  if(command === "bal1") {
    return message.channel.send(`You currently have ${bal.bal1} balance`)
  }
  if (command == "addbal1") {

    // Only server owner (me) can do this commands
    if(!message.author.id === message.guild.owner) return message.reply("Sorry, but you don't have enough permissions to run this command");

    const user = message.mentions.users.first() || client.users.get(args[0]);
    if(!user) return message.reply("Please @ the person or give me their ID");

    const pointsToAdd = parseInt(args[1], 10);
    if(!pointsToAdd) return message.reply("How much? You didn't tell me that!");

    //Get their current XP
    let userscore = client.getScore.get(user.id, message.guild.id);
    if(!userscore) {
      userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, bal1: 0}

    }

    client.setScore.run(userscore);
    return message.channel.send(`${user.tag} has received ${pointsToAdd}`);
  }

  if (command == "removebal1") {
    if(!message.author.id === message.guild.owner) return message.reply("Sorry, but you don't have enough permissions to run this command");

    const user = message.mentions.users.first() || client.users.get(args[0]);
    if(!user) return message.reply("Please @ the person or give me their ID");

    const pointsToRemove = parseInt(args[1], 10);
    if(!pointsToRemove) return message.reply("How much? You didn't tell me that!");

    //Get their current XP

    let userscore = client.getScore.get(user.id, message.guild.id);
    if(!userscore) {
      userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, bal1: 0}
    }

    client.setScore.run(userscore);
    return message.channel.send(`${user.tag} has lost ${pointsToRemove}`);
  }
});

0 个答案:

没有答案
相关问题