2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-27 20:37:30 +00:00

67 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
// Utils
const { link, scheduleDeletion } = require('../../utils/tg');
// Bot
2017-09-23 21:52:58 +03:30
const { replyOptions } = require('../../bot/options');
// DB
const { isAdmin, isBanned } = require('../../stores/user');
// Actions
const ban = require('../../actions/ban');
const banHandler = async (ctx) => {
const { message, reply, me } = ctx;
2017-09-25 12:40:04 +03:30
const userToBan = message.reply_to_message
? Object.assign({ username: '' }, message.reply_to_message.from)
2017-09-25 12:40:04 +03:30
: message.commandMention
? Object.assign({ username: '' }, message.commandMention)
2017-09-25 12:40:04 +03:30
: null;
const reason = message.text.split(' ').slice(1).join(' ').trim();
if (ctx.from.status !== 'admin') return null;
if (message.chat.type === 'private') {
return reply(
' <b>This command is only available in groups.</b>',
replyOptions
);
}
2017-09-25 12:40:04 +03:30
if (!userToBan) {
return reply(
' <b>Reply to a message or mention a user.</b>',
replyOptions
).then(scheduleDeletion);
2017-09-25 12:40:04 +03:30
}
if (userToBan.username.toLowerCase() === me.toLowerCase()) return null;
if (await isAdmin(userToBan)) {
return reply(' <b>Can\'t ban other admins.</b>', replyOptions);
}
if (reason.length === 0) {
return reply(' <b>Need a reason to ban.</b>', replyOptions)
.then(scheduleDeletion);
}
2017-09-25 12:40:04 +03:30
if (message.reply_to_message) {
ctx.deleteMessage(message.reply_to_message.message_id);
}
2017-09-23 15:39:12 +03:30
if (await isBanned(userToBan)) {
return reply(
`🚫 ${link(userToBan)} <b>is already banned.</b>`,
replyOptions
);
}
return ban({ admin: ctx.from, reason, userToBan }).then(ctx.replyWithHTML);
};
module.exports = banHandler;