我正在尝试用我的机器人发出声望命令,但我希望它能在24小时内运行,因此他们每天只能给出一次声望。他们只查看自己的代表的第一个块,如果他们提及用户,则为所提及的用户提供代表点。不过我不知道该怎么做...
const Discord = require("discord.js");
const fs = require("fs");
const ms = require("ms");
let reputation = JSON.parse(fs.readFileSync("./saves/reputation.json", "utf8"));
module.exports.run = async(bot, message, args) => {
console.log(`${message.member} ran the "reputation" command`);
//-- Timer on last use
let repTimer = 8640000
//-- Getting username
let repUser = message.guild.member(message.mentions.users.first() ||
message.guild.members.get(args[0]));
//-- Only displaying your points --\\
if (!repUser) {
if (!reputation[message.author.id]) {
reputation[message.author.id] = {
reputation: 0
};
}
let repPoints = reputation[message.author.id].reputation;
let repEmbed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#00FF00")
.setDescription(`Reputation Points: ${repPoints}`);
message.channel.send(repEmbed);
} //-- End !repUser
else {
if (!reputation[repUser.id]) {
reputation[repUser.id] = {
reputation: 0
};
}
//-- If user was mentioned --\\
let numRep = reputation[repUser.id].reputation;
reputation[repUser.id] = {
reputation: numRep + 1
};
message.channel.send(`${message.author.username} has given ${repUser} a
reputation point`);
}
//-- Needed (Also Wth Error Message) --\\
fs.writeFile("./saves/reputation.json", JSON.stringify(reputation), (err) => {
if (err) console.log(err)
});
}
module.exports.help = {
name: "rep"
}
答案 0 :(得分:0)
一种实现方法是拥有一个单独的文件(例如/saves/players.json
),您可以在其中存储播放器对象。
首先,您将读取文件,并查看该文件是否已有该用户的对象,如果没有,则创建一个新文件。
此后,您将查看用户是否可以提供代表,如果可以,则触发代码以提供信誉点。之后,您重置属性,以便可以在24小时内再次使用它。
看看下面的代码。这未经测试,但可以让您大致了解我要解释的内容。
let players = JSON.parse(fs.readFileSync("./saves/players.json",
"utf8"));
// Get the user who gave the rep
let repGiver;
if(!players[message.author.id]){
repGiver = {
nextRep: new Date()
};
players[message.author.id] = repGiver;
} else {
repGiver = players[message.author.id];
}
// Check whether the user can give rep
if (repGiver.nextRep <= new Date()) {
/*
* Code to give a player rep
*/
players[message.author.id] = {
// Reset the nextRep property to tomorrow
nextRep: new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
};
} else {
// Code for when a player can't give rep yet
}
// Save the new values for players
fs.writeFile("./saves/players.json", JSON.stringify(players), (err)
=> {
if (err) console.log(err)
});