2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-28 04:37:52 +00:00
GingerPlusPlus 1b7f750d53
Warn and ban actions (#54)
This feature allows any part of the bot to warn and ban,
without having to repeat what that means,
which reduces duplication and makes the UX more consistent.
2018-02-05 19:17:52 +01:00

67 lines
1.6 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, scheduleDeletion } = require('../../utils/tg');
// Bot
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;
const userToBan = message.reply_to_message
? Object.assign({ username: '' }, message.reply_to_message.from)
: message.commandMention
? Object.assign({ username: '' }, message.commandMention)
: 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
);
}
if (!userToBan) {
return reply(
' <b>Reply to a message or mention a user.</b>',
replyOptions
).then(scheduleDeletion);
}
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);
}
if (message.reply_to_message) {
ctx.deleteMessage(message.reply_to_message.message_id);
}
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;