I am creating a Discord bot with node.js and discord.js, and there's a feature that allows users to vote thanks to a command, but I'd like them to vote only once a week.
The issue is that, on this Discord, weeks start on Friday and end on Thursday, therefore I can't simply write :
var weekNow = moment().week();
var weekLastVote = moment(dateLastVote).week();
if (weekNow == weekLastVote){
//Prevent from voting again
} else {
//Let the user vote
}
Therefore, I have written some code that seems to work, but I'd like your opinion on it as it seems very sloppy and I'm not sure if I have taken into account all of the possibilities (I don't know if I need to use my month variables for example):
module.exports = {
isSameWeek: function (dateLastVote) {
// moments for today's date
var dayNow = moment().weekday();
var weekNow = moment().week();
var monthNow = moment().month();
var yearNow = moment().year();
var dateNow = moment().format('MMDDYYYY'); // moment without hours/minutes/seconds
// moments for last vote's date
var dayLastVote = moment(dateLastVote).weekday();
var weekLastVote = moment(dateLastVote).week();
var monthLastVote = moment(dateLastVote).month();
var yearLastVote = moment(dateLastVote).year();
var dateLastVote = moment(dateLastVote).format('MMDDYYYY'); // moment without hours/minutes/seconds
if ((yearNow === yearLastVote && weekNow === weekLastVote && dayLastVote < 5) || // 5 = Friday, starting day of the week (a week = Friday to thursday)
(yearNow === yearLastVote && weekNow - 1 === weekLastVote && dayLastVote >= 5 && dayNow < 5) ||
(dateNow === dateLastVote)
){
return true;
} else {
return false;
}
}
};
As I said, this seems do to the trick but I would like someone else's opinion on it to be sure there isn't a simpler way or, if there isn't, if I haven't forgotten anything.
Thank you for reading :)
via Tokipudi
No comments:
Post a Comment