As the title says, I have been working on this issue for a few days and still can't fix it. Basically what I'm trying to do is really simple. I'm trying to build a discord bot that gets the Date at which something started and the Date at which that event has ended. Specifically when the user types "start" I should register when it was written and after that register the time of "end" command.
Here is the code for start.
if (args == 'start') {
if (!message.member.voice.channel) return message.channel.send(`<@${message.member.id}> you have to join a **voice channel** so we can catch your suspicious attempts lol`);
if (user.studystarted) return message.channel.send(`<@${message.member.id}> Your study has started! whisper: *stop procrastinating*`);
await profileModel.updateOne({
userID: message.author.id,
}, {
$set: {
studystarted: true,
},
$currentDate: {
studystart: true,
}
}, {
upsert: true,
});
Here is the code for end;
if (args == 'end') {
if (!user.studystarted) return message.channel.send(`<@${message.member.id}> how are you going to end it if you haven't started?`);
await profileModel.updateOne({
userID: message.author.id,
}, {
$currentDate: {
studyend: true,
}
}, { upsert: true });
let starttime = user.studystart;
let endtime = user.studyend;
console.log("start time: " + starttime);
console.log("end time: " + endtime);
let diffTime = (endtime.getTime() - starttime.getTime()) / 1000;
await profileModel.updateOne({
userID: message.author.id,
}, {
$inc: {
studytotal: diffTime,
weeklytime: diffTime
},
$set: {
studystarted: false,
}
});
I've tried many ways to set the date but I don't see any of it working. It almost always ends up end time being written before and start time written later which shouldn't be how it works.
Any suggestion is appreciated. I'm a beginner