dbot/modules/report/commands.js

204 lines
8.2 KiB
JavaScript
Raw Normal View History

2014-05-25 23:16:28 +02:00
var _ = require('underscore')._,
moment = require('moment');
2013-10-21 17:40:45 +02:00
var commands = function(dbot) {
var commands = {
2014-05-25 22:47:30 +02:00
'~ncount': function(event) {
var chanCounts = {},
2014-05-25 23:16:28 +02:00
total = 0,
offString = event.params[1] || null;
offset = moment().subtract(offString, 1).valueOf() || null;
console.log(offset);
/*if(!offset || !offset.isValid()) {
event.reply('Invalid timescale. Try \'week\'');
return;
}*/
2014-05-25 22:47:30 +02:00
this.db.scan('notifies', function(notify) {
if(notify.user == event.rUser.id) {
2014-05-25 23:16:28 +02:00
if(!offString) {
if(!_.has(chanCounts, notify.channel)) chanCounts[notify.channel] = 0;
chanCounts[notify.channel]++;
total++;
} else {
console.log(offset);
console.log(notify.time);
console.log();
if(notify.time > offset) {
if(!_.has(chanCounts, notify.channel)) chanCounts[notify.channel] = 0;
chanCounts[notify.channel]++;
total++;
}
}
2014-05-25 22:47:30 +02:00
}
}, function() {
var cCounts = _.chain(chanCounts)
.pairs()
.sortBy(function(p) { return p[1]; })
.reverse()
.first(10)
.value();
var cString = '';
for(var i=0;i<cCounts.length;i++) {
cString += cCounts[i][0] + " (" + cCounts[i][1] + "), ";
}
cString = cString.slice(0, -2);
2014-05-25 23:16:28 +02:00
if(offString) {
event.reply(dbot.t('timed_notifies', {
'user': event.user,
'count': total,
'offString': offString,
'cString': cString
}));
} else {
event.reply(dbot.t('total_notifies', {
'user': event.user,
'count': total,
'cString': cString
}));
}
2014-05-25 22:47:30 +02:00
});
},
2013-10-21 17:40:45 +02:00
'~clearmissing': function(event) {
if(_.has(this.pending, event.rUser.id)) {
var count = this.pending[event.rUser.id].length;
delete this.pending[event.rUser.id];
event.reply(dbot.t('cleared_notifies', { 'count': count }));
} else {
event.reply(dbot.t('no_missed_notifies'));
}
},
'~report': function(event) {
2014-04-18 20:18:23 +02:00
var channelName = (event.input[1].trim() || event.channel),
2013-10-21 17:40:45 +02:00
nick = event.input[2],
reason = event.input[3].trim();
2014-02-04 01:58:09 +01:00
if(channelName == event.user) {
channelName = dbot.config.servers[event.server].admin_channel;
}
2013-10-21 17:40:45 +02:00
if(reason.charAt(reason.length - 1) != '.') reason += '.';
dbot.api.users.resolveUser(event.server, nick, function(reportee) {
if(_.has(event.allChannels, channelName)) {
if(reportee) {
this.api.notify('report', event.server, event.rUser,
channelName, dbot.t('report', {
'reporter': event.rUser.primaryNick,
'reportee': nick,
'reason': reason
}));
event.reply(dbot.t('reported', { 'reported': nick }));
} else {
event.reply(dbot.t('user_not_found', {
'reported': nick,
'channel': channelName
}));
}
} else {
event.reply(dbot.t('not_in_channel', { 'channel': channelName }));
}
}.bind(this));
},
'~notify': function(event) {
var channelName = event.input[1],
message = event.input[2];
if(_.has(event.allChannels, channelName)) {
if(this.config.firstHost) {
var first = message.split(' ')[0];
dbot.api.users.resolveUser(event.server, first, function(user) {
if(user && _.include(this.config.host_lookup, channelName)) {
2013-10-21 17:40:45 +02:00
dbot.api.nickserv.getUserHost(event.server, first, function(host) {
message = message.replace(first, first + ' [' + host + ']');
this.api.notify('notify', event.server, event.rUser, channelName, message);
}.bind(this));
} else {
this.api.notify('notify', event.server, event.rUser, channelName, message);
}
}.bind(this));
} else {
this.api.notify('notify', event.server, event.rUser, channelName, message);
}
event.reply(dbot.t('notified', {
'user': event.user,
'channel': channelName
}));
} else {
event.reply(dbot.t('not_in_channel', { 'channel': channelName }));
}
},
'~nunsub': function(event) {
var cName = event.input[1];
dbot.api.users.resolveChannel(event.server, cName, function(channel) {
if(channel) {
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
if(!nunsubs) {
var nunsubs = {
'id': channel.id,
'users': []
}
}
if(!_.include(nunsubs, event.rUser.id)) {
nunsubs.users.push(event.rUser.id);
this.db.save('nunsubs', channel.id, nunsubs, function() {
var reply = dbot.t('nunsubbed', { 'cName': cName })
if(_.has(this.config.chan_redirs, cName)) {
reply += dbot.t('n_also_found', { 'afaName' : this.config.chan_redirs[cName] });
}
event.reply(reply);
}.bind(this));
} else {
event.reply(dbot.t('already_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
}.bind(this));
},
'~ununsub': function(event) {
var cName = event.input[1];
dbot.api.users.resolveChannel(event.server, cName, function(channel) {
if(channel) {
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
if(!_.isUndefined(nunsubs) && _.include(nunsubs.users, event.rUser.id)) {
nunsubs.users = _.without(nunsubs.users, event.rUser.id);
this.db.save('nunsubs', channel.id, nunsubs, function() {
event.reply(dbot.t('ununsubbed', { 'cName': cName }));
});
} else {
event.reply(dbot.t('not_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
}.bind(this));
}
};
2014-02-04 01:58:09 +01:00
commands['~report'].regex = /^report (#[^ ]+ )?([^ ]+) (.*)$/;
2013-12-29 19:38:24 +01:00
commands['~notify'].regex = [/^notify ([^ ]+) (.+)$/, 3];
commands['~nunsub'].regex = [/^nunsub ([^ ]+)$/, 2];
commands['~ununsub'].regex = [/^ununsub ([^ ]+)$/, 2];
2013-10-21 17:40:45 +02:00
return commands;
};
exports.fetch = function(dbot) {
return commands(dbot);
};