Initial commit of new todo list module

This commit is contained in:
Scritches 2018-11-02 16:21:51 -04:00
parent dfdb31b31d
commit 08922c8e67
3 changed files with 106 additions and 0 deletions

5
modules/todo/config.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": [ ],
"ignorable": true,
"outputPrefix": "todo"
}

14
modules/todo/strings.json Normal file
View File

@ -0,0 +1,14 @@
{
"added": {
"en": "[{user}] - {entry-number}: '{entry}' added to your todo list."
},
"marked-complete": {
"en": "[{user}] - {entry-number}: '{entry}' marked as complete."
},
"no-entry": {
"en": "[{user}] - There is no todo entry {entry-number}."
},
"empty-list": {
"en": "[{user}] - Your todo list is empty. Add entries via '~todoadd'."
}
}

87
modules/todo/todo.js Normal file
View File

@ -0,0 +1,87 @@
var _ = require('underscore')._;
var todo = function(dbot) {
if(!_.has(dbot.db, 'todos')) {
dbot.db.todos = {};
}
var todos = dbot.db.todos;
this.api = {
'getTodos': user =>
_.has(todos,user) ? todos[user] : [],
'addTodo': (user, entry) => {
if (!_.has(todos,user)) {
todos[user] = [];
}
todos[user].push(entry);
return todos[user].length;
},
// entryNum is 1-based
// if entryNum is falsy then this will return a random todo.
'getTodo': (user, entryNum) => {
if (!_.has(todos,user)) return false;
if (!entryNum) {
entryNum = Math.floor(Math.random() * todos[user].length) + 1;
}
if (entryNum > todos[user].length) return false;
if(entryNum < 1) return false;
return {
todoNum: entryNum,
todo: todos[user][entryNum - 1]
};
},
// entryNum is 1-based
'delTodo': (user, entryNum) => {
if (!_.has(todos,user)) return false;
if (entryNum > todos[user].length) return false;
if (entryNum < 1) return false;
todos[user].splice(entryNum-1, 1);
return true;
}
}
this.commands = {
'todo': evt => {
var user = evt.rUser.id;
var myTodos = this.api.getTodos(user);
if(myTodos.length == 0) {
evt.reply(dbot.t('empty-list', {'user': evt.user}));
} else {
evt.reply('[' + evt.user + ']:');
for (let i = 0; i < myTodos.length; i++) {
var todo = myTodos[i];
evt.reply((i+1) + ': ' + todo);
}
}
},
'todoadd': evt => {
var user = evt.rUser.id;
var todoNum = this.api.addTodo(user, evt.input[1]);
evt.reply(dbot.t('added', { 'user': evt.user, 'entry-number': todoNum, 'entry': evt.input[1]}));
},
'tododone': evt => {
var user = evt.rUser.id;
var todoNum = parseInt(evt.input[1]);
var todo = this.api.getTodo(user, todoNum);
if(!todo) {
evt.reply(dbot.t('no-entry', { 'user': evt.user, 'entry-number': todoNum}));
} else {
this.api.delTodo(user, todoNum);
evt.reply(dbot.t('marked-complete', { 'user': evt.user, 'entry-number': todo.todoNum, 'entry': todo.todo}));
}
}
}
this.commands.todoadd.regex = [/todoadd (.+)/, 2];
this.commands.tododone.regex = [/tododone (\d+)/, 2];
}
exports.fetch = function(dbot) {
return new todo(dbot);
}