-
Notifications
You must be signed in to change notification settings - Fork 3
/
todo.jsm
69 lines (57 loc) · 1.76 KB
/
todo.jsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// -*- Mode: JavaScript; tab-width: 4 -*- vim:tabstop=4 syntax=javascript:
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*global module: false */
module.version = 0.5;
module.users = {};
module.DB_FILENAME = "todo.json";
module.loadUsers = function loadUsers() {
this.users = JSON.parse(aucgbot.readFile(this.DB_FILENAME));
};
module.saveUsers = function saveUsers() {
var file = new Stream(this.DB_FILENAME, "w");
file.write(JSON.stringify(this.users));
file.close();
};
module.getList = function getList(nick) {
var name = nick.replace(/(?!^)(?:\|.*|\d+|_+)$/, "").toLowerCase();
var users = this.users;
if (!Object.prototype.hasOwnProperty.call(users, name)) {
users[name] = [];
}
return users[name];
};
module.cmd_todo = function cmd_todo(e) {
var list = this.getList(e.nick);
if (e.args) {
list.push(e.args);
this.saveUsers();
e.notice("Ok.");
} else if (!list.length) {
e.reply("Nothing on your todo list.");
} else {
e.reply(list.join("; "));
}
return true;
};
module.cmd_todo.help = "Add stuff to your todo list and get the list. Usage: todo [item]";
module.cmd_tododel = function cmd_tododel(e) {
if (!e.args) {
e.reply(this.cmd_tododel.help);
return true;
}
var index = e.args >>> 0;
var list = this.getList(e.nick);
if (index >= list.length) {
e.reply("You only have {0} things in your todo list.".format(list.length));
} else {
list.splice(index, 1);
e.notice("Ok.");
}
return true;
};
module.cmd_tododel.help = "Delete an item from your todo list. Usage: tododel <index>";
try { module.loadUsers(); } catch (ex) {
println("Error while loading todo lists from disk: ", ex);
}