在SQLite XP系统上为奖励积分增加冷却时间

时间:2020-09-14 08:35:26

标签: javascript node.js sqlite discord.js

我希望通过仅每60秒获取一次xp来改进积分系统。我已经尝试了一些方法,但没有一个真正接近。当前的积分奖励代码是

client.on('ready', () => {
 // Check if the table "points" exists.
 const table = sql
  .prepare(
   "SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';"
  )
  .get();
 if (!table['count(*)']) {
  // create and setup the database correctly.
  sql
   .prepare(
    'CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);'
   )
   .run();
  // "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');
 }

 // 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);'
 );
});
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,
   };
  }
  score.points++;
  const curLevel = Math.floor(0.2 * Math.sqrt(score.points));
  if (score.level < curLevel) {
   score.level++;
   client.channels.cache
    .get('738662532700700719')
    .send(`${message.author} has leveled up to level **${curLevel}**!`);
  }
  client.setScore.run(score);
 }

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

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

1 个答案:

答案 0 :(得分:3)

对我而言,最重要的是存储您上一次给用户评分的时间戳。然后,对于每一次您想给用户更多的新消息积分,请检查当前时间是否比上次分配用户积分晚60秒以上。

看看下面的示例代码,然后尝试一下。可能需要进行调整,因为我对SQLite并不是很了解,但是我将链接下面使用的资源。

client.on('ready', () => {
 // Check if the table "points" exists.
 const table = sql
  .prepare(
   "SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';"
  )
  .get();
 if (!table['count(*)']) {
  // create and setup the database correctly.
  // Includes the new column 'lastAwardedDate'.
  sql
   .prepare(
    'CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER, lastAwardedDate TEXT);'
   )
   .run();
  // "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');
 }

 // 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, lastAwardedDate) VALUES (@id, @user, @guild, @points, @level, @lastAwardedDate);'
 );
});

// Define a constant value for the delay (in ms).
const pointDelay = 60 * 1000;

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,
   };
  } else {
   // Check if the current time minus the last awarded time is less than the delay.
   if (new Date() - Date.parse(score.lastAwardedDate) < pointDelay) {
    return;
   }
  }
  score.points++;
  score.lastAwardedDate = new Date().toString();
  const curLevel = Math.floor(0.2 * Math.sqrt(score.points));
  if (score.level < curLevel) {
   score.level++;
   client.channels.cache
    .get('738662532700700719')
    .send(`${message.author} has leveled up to level **${curLevel}**!`);
  }
  client.setScore.run(score);
 }

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

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

我使用的来源: