2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-26 11:57:09 +00:00

70 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
// Utils
const { displayUser, scheduleDeletion } = require('../../utils/tg');
2020-05-13 15:11:43 +02:00
const { html } = require('../../utils/html');
const { parse, strip, substom } = require('../../utils/cmd');
// Bot
// DB
const { getUser } = require('../../stores/user');
2020-03-10 22:10:48 +01:00
/** @param { import('../../typings/context').ExtendedContext } ctx */
const banHandler = async (ctx) => {
if (ctx.chat.type === 'private') {
2020-05-13 15:11:43 +02:00
return ctx.replyWithHTML(
' <b>This command is only available in groups.</b>',
);
}
if (ctx.from.status !== 'admin') return null;
2020-05-13 15:11:43 +02:00
const { flags, targets, reason } = parse(ctx.message);
2019-01-31 20:19:39 +01:00
if (targets.length === 0) {
2020-05-13 15:11:43 +02:00
return ctx.replyWithHTML(
2019-01-31 20:19:39 +01:00
' <b>Specify at least one user to ban.</b>',
).then(scheduleDeletion());
}
2019-01-31 20:19:39 +01:00
if (reason.length === 0) {
2020-05-13 15:11:43 +02:00
return ctx.replyWithHTML(' <b>Need a reason to ban.</b>')
2019-01-31 20:19:39 +01:00
.then(scheduleDeletion());
}
if (targets.length > 1) {
return ctx.batchBan({ admin: ctx.from, reason, targets });
}
const userToBan = await getUser(strip(targets[0])) || targets[0];
if (!userToBan.id) {
2020-05-13 15:11:43 +02:00
return ctx.replyWithHTML(
'❓ <b>User unknown.</b>\n' +
'Please forward their message, then try again.',
).then(scheduleDeletion());
2017-09-25 12:40:04 +03:30
}
if (userToBan.id === ctx.botInfo.id) return null;
if (userToBan.status === 'admin') {
2020-05-13 15:11:43 +02:00
return ctx.replyWithHTML(' <b>Can\'t ban other admins.</b>');
}
2020-04-29 17:25:59 +02:00
if (!flags.has('amend') && userToBan.status === 'banned') {
2020-05-13 15:11:43 +02:00
return ctx.replyWithHTML(
html`🚫 ${displayUser(userToBan)} <b>is already banned.</b>`,
);
}
return ctx.ban({
admin: ctx.from,
2022-03-29 13:38:22 +02:00
reason: '[' + ctx.chat.title + '] ' + await substom(reason),
userToBan,
msg: ctx.message.reply_to_message
});
};
module.exports = banHandler;