From 14e03313ec587da50ea30477ed5b45383d1d3a6e Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 9 Apr 2013 00:10:08 +0000 Subject: [PATCH] Provisional/example ~ignore functionality using databank [#331] --- modules/ignore/config.json | 1 + modules/ignore/ignore.js | 52 ++++++++++++++++++++++++++------------ modules/users/users.js | 2 +- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/modules/ignore/config.json b/modules/ignore/config.json index 14e12c2..94f16ed 100644 --- a/modules/ignore/config.json +++ b/modules/ignore/config.json @@ -2,5 +2,6 @@ "ignorable": false, "dependencies": [ "command" ], "dbKeys": [ "ignores", "bans" ], + "dbType": "memory", "help": "http://github.com/reality/depressionbot/blob/master/modules/ignore/README.md" } diff --git a/modules/ignore/ignore.js b/modules/ignore/ignore.js index 3e3d974..cc09904 100644 --- a/modules/ignore/ignore.js +++ b/modules/ignore/ignore.js @@ -5,7 +5,9 @@ * this information, since that actually performs the ignorance. Also provides * commands for moderators to choose the bot to ignore certain channels. */ -var _ = require('underscore')._; +var _ = require('underscore')._, + databank = require('databank'), + NoSuchThingError = databank.NoSuchThingError; var ignore = function(dbot) { var commands = { @@ -25,21 +27,39 @@ var ignore = function(dbot) { })); } else { if(module == '*' || _.include(ignorableModules, module)) { - if(_.has(dbot.db.ignores, event.user) && _.include(dbot.db.ignores[event.user], module)) { - event.reply(dbot.t('already_ignoring', { 'user': event.user })); - } else { - if(_.has(dbot.db.ignores, module)) { - dbot.db.ignores[event.user].push(module); - } else { - dbot.db.ignores[event.user] = [module]; - } - - dbot.instance.ignoreTag(event.user, module); - event.reply(dbot.t('ignored', { - 'user': event.user, - 'module': module - })); - } + dbot.api.users.resolveUser(event.server, event.user, function(user) { + this.db.read('ignores', user.id, function(err, ignores) { + if(err == NoSuchThingError) { + this.db.create('ignores', user.id, { + 'id': user.id, + 'ignores': [ module ] + }, function(err, result) { + if(!err) { + dbot.instance.ignoreTag(event.user, module); + event.reply(dbot.t('ignored', { + 'user': event.user, + 'module': module + })); + } + }); + } else { + if(!_.include(ignores.ignores, module)) { + ignores.ignores.push(module); + this.db.save('ignores', user.id, function(err) { + if(!err) { + dbot.instance.ignoreTag(event.user, module); + event.reply(dbot.t('ignored', { + 'user': event.user, + 'module': module + })); + } + }); + } else { + event.reply(dbot.t('already_ignoring', { 'user': event.user })); + } + } + }); + }); } else { event.reply(dbot.t('invalid_ignore', { 'user': event.user })); } diff --git a/modules/users/users.js b/modules/users/users.js index 78b2c69..ec0e3a4 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -75,7 +75,7 @@ var users = function(dbot) { } user.currentNick = event.user; - this.db.save(users, user.id, user, function(err) { + this.db.save('users', user.id, user, function(err) { if(err) { // QQ }