2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-25 03:17:09 +00:00
2017-09-24 14:28:31 +03:30

65 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
// Utils
const { link } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Bot
const bot = require('../../bot');
const { replyOptions } = require('../../bot/options');
// DB
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) {
return reply(' <b>Need a reason to ban.</b>', replyOptions);
}
if (!message.reply_to_message) {
return reply(' <b>Reply to a message.</b>', replyOptions);
}
bot.telegram.deleteMessage(chat.id, message.reply_to_message.message_id);
if (await isAdmin(userToBan)) {
return reply(' <b>Can\'t ban other admins.</b>', replyOptions);
}
if (await isBanned(userToBan)) {
return reply(`🚫 ${link(userToBan)} <b>is already banned.</b>`,
replyOptions);
}
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));
try {
await Promise.all(bans);
} catch (err) {
logError(process.env.DEBUG)(err);
}
return reply(`🚫 ${link(userToBan)} <b>got banned</b>.\n\n` +
`Reason: ${reason}`, replyOptions);
};
module.exports = banHandler;