From 08922c8e67024281ec70f485932b9568b559ee4d Mon Sep 17 00:00:00 2001 From: Scritches Date: Fri, 2 Nov 2018 16:21:51 -0400 Subject: [PATCH] Initial commit of new todo list module --- modules/todo/config.json | 5 +++ modules/todo/strings.json | 14 +++++++ modules/todo/todo.js | 87 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 modules/todo/config.json create mode 100644 modules/todo/strings.json create mode 100644 modules/todo/todo.js diff --git a/modules/todo/config.json b/modules/todo/config.json new file mode 100644 index 0000000..1eb9f36 --- /dev/null +++ b/modules/todo/config.json @@ -0,0 +1,5 @@ +{ + "dependencies": [ ], + "ignorable": true, + "outputPrefix": "todo" +} \ No newline at end of file diff --git a/modules/todo/strings.json b/modules/todo/strings.json new file mode 100644 index 0000000..21d6559 --- /dev/null +++ b/modules/todo/strings.json @@ -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'." + } +} \ No newline at end of file diff --git a/modules/todo/todo.js b/modules/todo/todo.js new file mode 100644 index 0000000..b1526cf --- /dev/null +++ b/modules/todo/todo.js @@ -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); +} \ No newline at end of file