2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 19:37:14 +00:00

65 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
// Utils
2017-09-23 21:52:58 +03:30
const { link } = require('../../utils/tg');
2017-09-23 21:49:23 +03:30
const { logError } = require('../../utils/log');
// Bot
2017-09-23 21:52:58 +03:30
const bot = require('../../bot');
const { replyOptions } = require('../../bot/options');
// DB
2017-09-23 21:52:58 +03:30
const { listGroups } = require('../../stores/groups');
const { isBanned, ban } = require('../../stores/ban');
const { isAdmin } = require('../../stores/admin');
const banHandler = async ({ chat, message, reply, telegram }) => {
if (!await isAdmin(message.from)) {
return null;
}
const userToBan = message.reply_to_message.from;
const reason = message.text.split(' ').slice(1).join(' ').trim();
if (reason.length === 0) {
2017-09-24 14:28:18 +03:30
return reply(' <b>Need a reason to ban.</b>', replyOptions);
}
if (!message.reply_to_message) {
2017-09-24 14:28:18 +03:30
return reply(' <b>Reply to a message.</b>', replyOptions);
}
bot.telegram.deleteMessage(chat.id, message.reply_to_message.message_id);
if (await isAdmin(userToBan)) {
2017-09-24 14:28:18 +03:30
return reply(' <b>Can\'t ban other admins.</b>', replyOptions);
}
2017-09-23 15:39:12 +03:30
if (await isBanned(userToBan)) {
2017-09-24 14:28:18 +03:30
return reply(`🚫 ${link(userToBan)} <b>is already banned.</b>`,
replyOptions);
}
2017-09-23 21:49:23 +03:30
try {
await ban(userToBan, reason);
} catch (err) {
logError(process.env.DEBUG)(err);
}
const groups = await listGroups();
const bans = groups.map(group =>
telegram.kickChatMember(group.id, userToBan.id));
2017-09-23 21:49:23 +03:30
try {
await Promise.all(bans);
} catch (err) {
logError(process.env.DEBUG)(err);
}
2017-09-24 14:28:18 +03:30
return reply(`🚫 ${link(userToBan)} <b>got banned</b>.\n\n` +
`Reason: ${reason}`, replyOptions);
};
module.exports = banHandler;