1 回答

TA貢獻(xiàn)2016條經(jīng)驗 獲得超9個贊
我想到的是存儲您上次給用戶積分的時間戳。然后,每次你想為新消息獎勵用戶更多積分時,檢查當(dāng)前時間是否比你上次分配用戶積分晚了 60 秒以上。
看看下面的示例代碼并試一試。它可能需要調(diào)整,因為我對 SQLite 沒有真正的經(jīng)驗,但我將鏈接我在下面使用的資源。
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();
});
添加回答
舉報