I create a Discord bot with node.js and discord.js, and there is a feature that allows users to vote for the team, but I would like them to vote only once a week.
The problem is that on this Difference, the weeks start on Friday and end on Thursday , so I can't just write:
var weekNow = moment().week(); var weekLastVote = moment(dateLastVote).week(); if (weekNow == weekLastVote){
So I wrote code that seems to work, but I would like your opinion about it to look very sloppy, and I'm not sure that Iβve taken all the possibilities into account (I donβt know if I need to use my month variables, eg):
module.exports = { isSameWeek: function (dateLastVote) { // moments for today 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 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 like a trick, but I would like someone else to think about it to make sure that there is no easier way or, if not, if I have not forgotten anything.
Thank you for reading:)
source share