2017-09-21 23:47:26 +04:30
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
// Utils
|
2017-10-31 22:20:49 +01:00
|
|
|
|
const { link, scheduleDeletion } = require('../../utils/tg');
|
2017-09-23 21:49:23 +03:30
|
|
|
|
const { logError } = require('../../utils/log');
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
|
|
|
|
// Bot
|
2017-09-23 21:40:58 +03:30
|
|
|
|
const { replyOptions } = require('../../bot/options');
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
|
|
|
|
// DB
|
2017-11-17 18:53:51 +01:00
|
|
|
|
const { getUser, nowarns } = require('../../stores/user');
|
|
|
|
|
const { listGroups } = require('../../stores/group');
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
2017-11-19 13:08:11 +01:00
|
|
|
|
const noop = Function.prototype;
|
|
|
|
|
|
|
|
|
|
// This handler is very similiar to commands/unban.
|
|
|
|
|
// When adding a feature here, please consider adding it there too.
|
|
|
|
|
|
2017-11-17 18:53:51 +01:00
|
|
|
|
const nowarnsHandler = async ({ message, reply, state, telegram }) => {
|
2017-10-06 16:34:23 +03:30
|
|
|
|
const { isAdmin, user } = state;
|
2017-10-06 16:40:49 +03:30
|
|
|
|
if (!isAdmin) return null;
|
2017-09-25 12:40:04 +03:30
|
|
|
|
|
|
|
|
|
const userToUnwarn = message.reply_to_message
|
|
|
|
|
? message.reply_to_message.from
|
|
|
|
|
: message.commandMention
|
|
|
|
|
? message.commandMention
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (!userToUnwarn) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
'ℹ️ <b>Reply to a message or mention a user.</b>',
|
|
|
|
|
replyOptions
|
2018-11-20 11:47:30 +05:30
|
|
|
|
).then(scheduleDeletion());
|
2017-09-21 23:47:26 +04:30
|
|
|
|
}
|
|
|
|
|
|
2017-11-17 18:53:51 +01:00
|
|
|
|
const dbUser = await getUser({ id: userToUnwarn.id });
|
2017-09-21 23:47:26 +04:30
|
|
|
|
|
2017-11-17 18:53:51 +01:00
|
|
|
|
const { warns } = dbUser;
|
|
|
|
|
|
|
|
|
|
if (warns.length === 0) {
|
2017-10-31 23:08:22 +01:00
|
|
|
|
return reply(
|
|
|
|
|
`ℹ️ ${link(userToUnwarn)} <b>already has no warnings.</b>`,
|
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-24 22:17:27 +03:30
|
|
|
|
}
|
2017-11-17 18:53:51 +01:00
|
|
|
|
|
|
|
|
|
if (dbUser.status === 'banned') {
|
|
|
|
|
const groups = await listGroups();
|
|
|
|
|
|
|
|
|
|
groups.forEach(group =>
|
|
|
|
|
telegram.unbanChatMember(group.id, userToUnwarn.id));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-23 21:49:23 +03:30
|
|
|
|
try {
|
|
|
|
|
await nowarns(userToUnwarn);
|
|
|
|
|
} catch (err) {
|
2017-09-27 23:12:12 +03:30
|
|
|
|
logError(err);
|
2017-09-23 21:49:23 +03:30
|
|
|
|
}
|
2017-09-22 15:52:27 +03:30
|
|
|
|
|
2017-11-19 13:08:11 +01:00
|
|
|
|
if (dbUser.status === 'banned') {
|
|
|
|
|
telegram.sendMessage(
|
|
|
|
|
userToUnwarn.id,
|
|
|
|
|
'♻️ You were unbanned from all of the /groups!'
|
|
|
|
|
).catch(noop);
|
|
|
|
|
// it's likely that the banned person haven't PMed the bot,
|
|
|
|
|
// which will cause the sendMessage to fail,
|
|
|
|
|
// hance .catch(noop)
|
|
|
|
|
// (it's an expected, non-critical failure)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 23:47:26 +04:30
|
|
|
|
return reply(
|
2017-10-06 16:34:23 +03:30
|
|
|
|
`♻️ ${link(user)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
2017-09-24 23:24:34 +03:30
|
|
|
|
'<b>for all of their warnings.</b>',
|
2017-10-31 23:08:22 +01:00
|
|
|
|
replyOptions
|
|
|
|
|
);
|
2017-09-21 23:47:26 +04:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = nowarnsHandler;
|