在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();
});

我想到的是存储上次给用户打分的时间戳。然后,每当您想为新消息向用户授予更多积分时,请检查当前时间是否比上次为用户分配积分晚了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();
});

我使用的来源:

  • SQLite日期&时间
  • 日期.toISO字符串

最新更新