在 SQLite xp 系统上为奖励积分添加冷却时间

我希望通过只允许每 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();

});


鸿蒙传说
浏览 88回答 1
1回答

慕沐林林

我想到的是存储您上次给用户积分的时间戳。然后,每次你想为新消息奖励用户更多积分时,检查当前时间是否比你上次分配用户积分晚了 60 秒以上。看看下面的示例代码并试一试。它可能需要调整,因为我对 SQLite 没有真正的经验,但我将链接我在下面使用的资源。client.on('ready', () => {&nbsp;// Check if the table "points" exists.&nbsp;const table = sql&nbsp; .prepare(&nbsp; &nbsp;"SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';"&nbsp; )&nbsp; .get();&nbsp;if (!table['count(*)']) {&nbsp; // create and setup the database correctly.&nbsp; // Includes the new column 'lastAwardedDate'.&nbsp; sql&nbsp; &nbsp;.prepare(&nbsp; &nbsp; 'CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER, lastAwardedDate TEXT);'&nbsp; &nbsp;)&nbsp; &nbsp;.run();&nbsp; // "id" row is always unique and indexed.&nbsp; sql.prepare('CREATE UNIQUE INDEX idx_scores_id ON scores (id);').run();&nbsp; sql.pragma('synchronous = 1');&nbsp; sql.pragma('journal_mode = wal');&nbsp;}&nbsp;// get and set the score data.&nbsp;client.getScore = sql.prepare(&nbsp; 'SELECT * FROM scores WHERE user = ? AND guild = ?'&nbsp;);&nbsp;client.setScore = sql.prepare(&nbsp; 'INSERT OR REPLACE INTO scores (id, user, guild, points, level, lastAwardedDate) VALUES (@id, @user, @guild, @points, @level, @lastAwardedDate);'&nbsp;);});// Define a constant value for the delay (in ms).const pointDelay = 60 * 1000;client.on('message', (message) => {&nbsp;if (message.author.bot) return;&nbsp;let score;&nbsp;if (message.guild) {&nbsp; score = client.getScore.get(message.author.id, message.guild.id);&nbsp; if (!score) {&nbsp; &nbsp;score = {&nbsp; &nbsp; id: `${message.guild.id}-${message.author.id}`,&nbsp; &nbsp; user: message.author.id,&nbsp; &nbsp; guild: message.guild.id,&nbsp; &nbsp; points: 0,&nbsp; &nbsp; level: 1,&nbsp; &nbsp;};&nbsp; } else {&nbsp; &nbsp;// Check if the current time minus the last awarded time is less than the delay.&nbsp; &nbsp;if (new Date() - Date.parse(score.lastAwardedDate) < pointDelay) {&nbsp; &nbsp; return;&nbsp; &nbsp;}&nbsp; }&nbsp; score.points++;&nbsp; score.lastAwardedDate = new Date().toString();&nbsp; const curLevel = Math.floor(0.2 * Math.sqrt(score.points));&nbsp; if (score.level < curLevel) {&nbsp; &nbsp;score.level++;&nbsp; &nbsp;client.channels.cache&nbsp; &nbsp; .get('738662532700700719')&nbsp; &nbsp; .send(`${message.author} has leveled up to level **${curLevel}**!`);&nbsp; }&nbsp; client.setScore.run(score);&nbsp;}&nbsp;if (message.content.indexOf(config.prefix) !== 0) return;&nbsp;const args = message.content&nbsp; .slice(config.prefix.length)&nbsp; .trim()&nbsp; .split(/ +/g);&nbsp;const command = args.shift().toLowerCase();});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript