From d0612c40f9d8b1ad933ff848b27680e9caae46ce Mon Sep 17 00:00:00 2001 From: Griffin Strayer Date: Sun, 28 Jan 2018 22:58:40 -0500 Subject: [PATCH] Fixed a bug where if you were to record attendance twice in one day then you would record the same date multiple times. This however means that if you decide to record twice in one day it does not check if you have entered already that day. --- commands/recordStart.js | 24 ++++++++++++++++++++++++ commands/recordStop.js | 2 -- commands/userRemove.js | 1 - 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/commands/recordStart.js b/commands/recordStart.js index 336bab1..b3511ca 100644 --- a/commands/recordStart.js +++ b/commands/recordStart.js @@ -1,3 +1,5 @@ +const fs = require('fs'); + exports.run = (client, message, config) => { if (config.isAutomated) { return message.reply( @@ -10,6 +12,28 @@ exports.run = (client, message, config) => { } else { config.attendanceArray = []; config.isRecording = true; + + var today = new Date(); + var month = today.getMonth() + 1; //january is 0 + var day = today.getDate(); //getday returns the day of the week. + var year = today.getFullYear(); //gets the 4 digit version + thisMonth = year + '-' + month; + thisDay = year + '/' + month + '/' + day; + + fs.readFile('./logs/' + thisMonth + '.txt', 'utf-8', function(err, data) { + if (err) { + if (err.code === 'ENOENT') { + console.log('doesnt exist'); + config.attendanceArray.unshift(thisDay); + } + } else { + console.log('exists'); + var theFile = data.toString(); + if (theFile.indexOf(thisDay) == -1) { + config.attendanceArray.unshift(thisDay); + } + } + }); //start the recording return message.channel.send( 'Attendance recording has started. \n' + diff --git a/commands/recordStop.js b/commands/recordStop.js index fbfe763..31b6723 100644 --- a/commands/recordStop.js +++ b/commands/recordStop.js @@ -25,8 +25,6 @@ exports.run = (client, message, config) => { var year = today.getFullYear(); //gets the 4 digit version thisMonth = year + '-' + month; thisDay = year + '/' + month + '/' + day; - //unshift brings the date to the front of the array - arrayAttendance.unshift(thisDay); //w -open file for writing. the file is created (if it does not exist) or truncated (if it exists). //wx - like w but fails if path exists. diff --git a/commands/userRemove.js b/commands/userRemove.js index e4d7b01..47a5849 100644 --- a/commands/userRemove.js +++ b/commands/userRemove.js @@ -46,7 +46,6 @@ exports.run = (client, messsage, args) => { //remove the user, remove the empty line, combine them together with the stuff before. var removeUser = lastPartFile.replace(args[1], ''); var combine = firstPartFile + removeUser.replace(/(\r\n|\n|\r)/, ''); - console.log(combine); fs.writeFile('./logs/' + thisMonth + '.txt', combine, 'utf8', err => { if (err) throw err; return message.reply(