3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-27 13:12:44 +01:00
dbot/modules/warning/warning.js

185 lines
6.6 KiB
JavaScript
Raw Normal View History

var _ = require('underscore')._;
uuid = require('node-uuid');
var warning = function(dbot) {
2014-06-26 23:50:33 +02:00
this.api = {
'warn': function(server, warner, user, reason, channel, callback) {
var adminChannel = dbot.config.servers[server].admin_channel || channel.name;
2014-09-04 15:43:53 +02:00
dbot.api.users.resolveUser(server, user, function(err, warnee) {
if(warnee) {
var id = uuid.v4();
this.db.save('warnings', id, {
'id': id,
2014-06-26 23:50:33 +02:00
'server': server,
'warnee': warnee.id,
'warner': warner.id,
'reason': reason,
'time': new Date().getTime()
}, function(err) {
2018-02-10 13:59:02 +01:00
var sType = 'warn_notify';
if(reason.match('#note')) {
sType = 'note_notify';
}
var notifyString = dbot.t(sType, {
'warner': warner.primaryNick,
'warnee': warnee.primaryNick,
'reason': reason,
'url': dbot.api.web.getUrl('warning/' + server + '/'
+ warnee.primaryNick)
});
2013-08-28 19:35:11 +02:00
dbot.api.report.notify('warn', server, warner, adminChannel, notifyString, false, warnee.primaryNick);
dbot.say(server, adminChannel, notifyString);
2014-06-26 23:50:33 +02:00
2014-11-09 15:04:04 +01:00
var uString = dbot.t('warn_user', {
'warner': warner.primaryNick,
'reason': reason,
'admin_channel': dbot.config.servers[server].admin_channel
});
2015-02-08 22:10:23 +01:00
if(reason.indexOf('#quiet') === -1 && reason.indexOf('#note') === -1) {
dbot.say(server, warnee.currentNick, uString);
}
2014-11-09 15:04:04 +01:00
dbot.api.event.emit('new_warning', [ warner, warnee, reason ]);
2014-06-26 23:50:33 +02:00
callback(null);
});
} else {
2014-06-26 23:50:33 +02:00
callback(true);
}
}.bind(this));
}
};
this.commands = {
'~warn': function(event) {
var warner = event.rUser,
server = event.server,
reason = event.input[2];
this.api.warn(server, warner, event.input[1], reason, event.channel, function(err) {
if(err) {
event.reply(dbot.t('warnee_not_found', { 'user': event.input[1] }));
}
2014-06-26 23:50:33 +02:00
});
},
2018-02-10 13:35:31 +01:00
'~addnote': function(event) {
var warner = event.rUser,
server = event.server,
reason = event.input[2];
if(!reason.match('#note')) {
reason += ' #note';
}
this.api.warn(server, warner, event.input[1], reason, event.channel, function(err) {
if(err) {
event.reply(dbot.t('warnee_not_found', { 'user': event.input[1] }));
} else {
event.reply('Note added!');
}
});
},
2014-06-26 23:50:33 +02:00
'~rmwarning': function(event) {
2016-02-19 20:12:22 +01:00
var user = event.params[1],
index = parseInt(event.params[2]);
dbot.api.users.resolveUser(event.server, user, function(err, warnee) {
if(warnee) {
var warns = {};
2016-02-19 20:14:10 +01:00
dbot.api.users.getUserAliases(warnee.id, function(err, aliases) {
2016-02-19 20:24:01 +01:00
aliases.push(warnee.primaryNick);
dbot.modules.report.db.search('notifies', {
2016-02-19 20:18:43 +01:00
'server': event.server,
2016-02-19 20:12:22 +01:00
'type': 'warn'
}, function(result) {
2016-02-19 20:24:01 +01:00
if(_.include(aliases, result.target)) {
2016-02-19 20:12:22 +01:00
warns[result.time] = result;
}
}, function(err) {
var sTimes = _.keys(warns).sort(function(a, b) {
return parseInt(a) - parseInt(b);
});
2016-02-19 20:17:05 +01:00
if(index <= sTimes.length && index >= 0) {
2016-02-19 20:28:01 +01:00
this.db.del('notifies', warns[sTimes[index]].id, function(err) {
2016-02-19 20:12:22 +01:00
event.reply(dbot.t('warning_removed'));
});
} else {
event.reply(dbot.t('warning_not_found'));
}
}.bind(this));
2016-02-19 20:15:06 +01:00
}.bind(this));
2016-02-19 20:12:22 +01:00
}
2016-02-19 20:15:06 +01:00
}.bind(this));
},
2015-07-08 22:47:15 +02:00
'~rmlastwarning': function(event) {
var lastWarning = null;
dbot.api.users.resolveUser(event.server, event.params[1], function(err, warnee) {
this.db.search('warnings', { 'server': event.server, 'warnee': warnee.id }, function(warning) {
if(!lastWarning || (lastWarning && lastWarning.time < warning.time)) {
lastWarning = warning;
}
}, function(err) {
if(!lastWarning) {
event.reply('Looks like ' + event.params[1] + ' has no warnings.');
} else {
this.db.del('warnings', lastWarning.id, function(err) {
event.reply(dbot.t('warning_removed') + ' - ' + lastWarning.reason);
});
}
}.bind(this));
}.bind(this));
},
2018-02-10 13:35:31 +01:00
'~warnings': function(event) {
var warnee = event.params[1],
server = event.server;
2014-09-04 15:43:53 +02:00
dbot.api.users.resolveUser(server, warnee, function(err, warnee) {
2014-11-09 14:59:00 +01:00
if(warnee) {
var warnings = 0;
this.db.search('warnings', {
'server': server,
'warnee': warnee.id
}, function(warning) {
warnings++;
}, function(err) {
if(warnings > 0) {
event.reply(dbot.t('warning_info', {
'user': warnee.primaryNick,
'num': warnings,
'url': dbot.api.web.getUrl('warning/' + server + '/'
+ warnee.primaryNick)
}));
} else {
event.reply(dbot.t('no_warnings', { 'user':
warnee.primaryNick }));
}
});
} else {
event.reply(event.params[1] + ' not found.');
}
}.bind(this));
}
};
2013-12-29 19:38:24 +01:00
this.commands['~warn'].regex = [/warn ([^ ]+) (.+)/, 3];
this.commands['~warn'].access = 'power_user';
2014-06-26 23:50:33 +02:00
this.commands['~rmwarning'].access = 'power_user';
2015-07-08 22:47:15 +02:00
this.commands['~rmlastwarning'].access = 'power_user';
2018-02-10 13:47:54 +01:00
this.commands['~addnote'].regex = [/addnote ([^ ]+) (.+)/, 3];
2018-02-10 13:35:31 +01:00
this.commands['~addnote'].access = 'power_user';
};
exports.fetch = function(dbot) {
return new warning(dbot);
};