Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Moved the commands into the command folder. Updated them to work from…
Browse files Browse the repository at this point in the history
… there. Added a utility file that helps with certain functions that are called a lot. Added a draw file that draws a winner out of the entries. Update the help file to include that enter command. Updated the gitignore with the right file.
  • Loading branch information
Gobluebro committed Jan 28, 2018
1 parent b2a7997 commit 05dcd8f
Show file tree
Hide file tree
Showing 16 changed files with 559 additions and 409 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

#Config files contain personal information#
config.json
config2.json
copy-config.json

#log files#
logs/*.txt
logs/*.txt

24 changes: 24 additions & 0 deletions commands/autoMan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');

// this command is to determine if it's on auto or manual and switch it and save it.
exports.run = (client, message, config) => {
// / root directory, ./ current directory, ../parent of current directory
// the config, null, 2 makes it so the json file is not minified/readable
if (config.isAutomated) {
config.isAutomated = false;
fs.writeFile(
'../config.json',
JSON.stringify(config, null, 2),
err => console.error
);
return message.reply('Attendance Bot is now in manual mode.');
} else {
config.isAutomated = true;
fs.writeFile(
'../config.json',
JSON.stringify(config, null, 2),
err => console.error
);
return message.reply('Attendance Bot is now in automated mode.');
}
};
26 changes: 26 additions & 0 deletions commands/deleteMonth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const util = require('../util.js');
const fs = require('fs');

exports.run = (client, message, args) => {
var errorLog = util.checkArguments(args, 1);
if (errorLog != '') {
return message.reply(errorLog);
}
errorLog = util.checkDate(args, false);
if (errorLog != '') {
return message.reply(errorLog);
}
var thisMonth = args[0].split('/')[0] + '-' + args[0].split('/')[1];
fs.unlink('./logs/' + thisMonth + '.txt', function(err) {
if (err) {
if (err.code === 'ENOENT') {
return message.reply(
'File not found\n' +
'Make sure format is in YYYY/MM\n' +
'e.g. !attenddeletemonth YYYY/MM'
);
}
}
return message.reply(`The attendance for ${args[0]} has been removed.`);
});
};
47 changes: 47 additions & 0 deletions commands/draw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const util = require('../util.js');
const fs = require('fs');

exports.run = (client, message, args) => {
var errorLog = util.checkArguments(args, 1);
if (errorLog != '') {
return message.reply(errorLog);
}
errorLog = util.checkDate(args, false);
if (errorLog != '') {
return message.reply(errorLog);
}
var theYear = args[0].split('/')[0];
var theMonth = args[0].split('/')[1];
var thisMonth = theYear + '-' + theMonth;
fs.open('./logs/' + thisMonth + '.txt', 'r', (err, fd) => {
if (err) {
if (err.code === 'ENOENT') {
return message.reply(
'That file does not exist.\n' +
'Make sure you have the correct format e.g, !commandname YYYY/MM/DD username'
);
}
throw err;
}
fs.readFile('./logs/' + thisMonth + '.txt', 'utf8', function(err, data) {
if (err) throw err;
var theUsers = [];
var wholeFile = data.toString().split('\r\n');
for (var i = 0; i < wholeFile.length; i++) {
if (wholeFile[i].includes('/')) {
continue;
} else {
theUsers.push(wholeFile[i]);
}
}
//this needs to be inside the readfile or else it will be a blank array
return message.channel.send(
`The winner for the month of ${args[0]} is ${util.drawWinner(theUsers)}`
);
});
//must close any opens or else an error can throw "too many files open"
fs.close(fd, err => {
if (err) throw err;
});
});
};
23 changes: 23 additions & 0 deletions commands/enter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
exports.run = (client, message, config) => {
if (config.isRecording) {
//only enter their username if they haven't entered today already
if (
config.attendanceArray.includes(message.author.username) ||
config.attendanceArray.includes(message.member.nickname)
) {
return message.reply('You have already entered today.');
} else {
//prefer getting their nickname otherwise get their username
if (message.member.nickname == null) {
config.attendanceArray.push(message.author.username);
} else {
config.attendanceArray.push(message.member.nickname);
}
}
} else {
return message.reply(
'Attendance bot is not currently recording.\n' +
'Please contact a GM for help'
);
}
};
37 changes: 37 additions & 0 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
exports.run = (client, message, args) => {
var printHelp =
'__General Commands__\n' +
'!attendadd - Adds a user to attendance based on moderator discretion.\n' +
'!attendautoman - Sets the bot in auto or manual mode and follows those rules.\n' +
'!attendcurrent - Shows the currently recorded users during attendance.\n' +
'!attenddeletemonth - Delete a specific month’s recording of attendance.\n' +
'!attenddraw - Draws a random entry winner from a specific month.\n' +
'!attendremove - Removes a user from attendance based on moderator discretion.\n' +
'!enter - User command to enter in the giveaway during attendance recording.\n\n' +
'__Manual Commands__\n' +
'!attendstart - Starts the recording of attendance.\n' +
'!attendstop - Stops the recording of attendance.\n\n' +
'__Automation Commands__\n' +
'!attendlength - Set the length of attendance recording time.\n' +
'!attendtimestart - Set the start time of the attendance.\n' +
'!attendtimeend - Set the ending time of the attendance.\n';
if (args[0] === 'text') {
return message.channel.send(printHelp + '\nMade by Gobluebro');
} else {
return message.channel.send({
embed: {
color: 3447003,
description: printHelp,
title: '__Command List__',
author: {
name: 'Attendance-bot',
url: 'https://github.com/Gobluebro/Attendance-Bot',
icon_url: client.user.avatarURL
},
footer: {
text: 'Made by Gobluebro'
}
}
});
}
};
15 changes: 15 additions & 0 deletions commands/recordCurrent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.run = (client, message, config) => {
if (config.isRecording) {
var formattedString =
'These are the users that have entered during this recording:\n';
for (let i = 0; i < config.attendanceArray.length; i++) {
formattedString += config.attendanceArray[i] + '\n';
}
return message.channel.send(formattedString);
} else {
return message.reply(
'Attendance Bot is not currently recording.\n' +
'If you wish to start the recording please use the command "!attendstart" to start.'
);
}
};
20 changes: 20 additions & 0 deletions commands/recordStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exports.run = (client, message, config) => {
if (config.isAutomated) {
return message.reply(
'Attendance Bot is currently in automation mode. \n' +
'If you wish to start the attendance bot manually please use the command !attendautoman to switch.'
);
} else {
if (config.isRecording) {
return message.reply('Attendance Bot is already recording.');
} else {
config.attendanceArray = [];
config.isRecording = true;
//start the recording
return message.channel.send(
'Attendance recording has started. \n' +
'Please enter the command !enter to have your name recorded for attendance.'
);
}
}
};
72 changes: 72 additions & 0 deletions commands/recordStop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const fs = require('fs');

exports.run = (client, message, config) => {
if (config.isAutomated) {
return message.reply(
`Attendance Bot is currently in automation mode. If you wish to start the attendance bot manually please use the command "!attendautoman" to switch.`
);
} else {
if (!config.isRecording) {
return message.reply(
'Attendance Bot is not currently recording.\n' +
'If you wish to start the recording please use the command "!attendstart" to start.'
);
} else {
//stop the recording but not saving it to config
//if the bot were to die we don't want to save any recordings
config.isRecording = false;
//make sure there is something in the array
//and make sure it's not null or undefined
if (config.attendanceArray[0] != null) {
var arrayAttendance = config.attendanceArray;
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;
//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.
fs.open('./logs/' + thisMonth + '.txt', 'wx', (err, fd) => {
if (err) {
//if the file exists already append the array to it
if (err.code === 'EEXIST') {
//flags a means append
var appendExistingStream = fs.createWriteStream(
'./logs/' + thisMonth + '.txt',
{ flags: 'a' }
);
for (var i = 0; i < arrayAttendance.length; i++) {
appendExistingStream.write('\r\n' + arrayAttendance[i]);
}
appendExistingStream.end();
return;
}
throw err;
return;
}
//defaults to just writing not appending.
var newFileStream = fs.createWriteStream(
'./logs/' + thisMonth + '.txt'
);
newFileStream.write(arrayAttendance[0]);
if (arrayAttendance[1] != null) {
for (var i = 1; i < arrayAttendance.length; i++) {
newFileStream.write('\r\n' + arrayAttendance[i]);
}
}
newFileStream.end();
return;
//must close any opens or else an error can throw "too many files open"
fs.close(fd, err => {
if (err) throw err;
});
});
}
return message.channel.send('Attendance has stopped recording.');
}
}
};
13 changes: 13 additions & 0 deletions commands/timeEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.run = (client, message, fs) => {
if (config.isAutomated) {
if (config.isRecording) {
return message.reply('Attendance Bot is already recording.');
} else {
//get the end time
}
} else {
return message.reply(
`Attendance Bot is currently in manual mode. If you want the attendance bot to start manually please use the command "!attendautoman" to switch.`
);
}
};
7 changes: 7 additions & 0 deletions commands/timeLength.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.run = (client, message, fs) => {
const minuteLength = parseInt(args[0], 10);
if (!minuteLength || minuteLength < 1 || minuteLength > 60)
return message.reply(
'Please provide a number between 1 and 60 for the length of attendance in minutes.'
);
};
13 changes: 13 additions & 0 deletions commands/timeStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.run = (client, message, fs) => {
if (config.isAutomated) {
if (config.isRecording) {
return message.reply('Attendance Bot is already recording.');
} else {
//get the start time
}
} else {
return message.reply(
`Attendance Bot is currently in manual mode. If you want the attendance bot to start manually please use the command "!attendautoman" to switch.`
);
}
};
Loading

0 comments on commit 05dcd8f

Please sign in to comment.