2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-09-05 08:35:22 +00:00

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.
This commit is contained in:
GingerPlusPlus
2018-02-05 19:17:52 +01:00
committed by GitHub
parent 9de38b7306
commit 1b7f750d53
6 changed files with 102 additions and 113 deletions

View File

@@ -2,17 +2,19 @@
// Utils
const { link, scheduleDeletion } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Bot
const bot = require('../../bot');
const { replyOptions } = require('../../bot/options');
// DB
const { listGroups } = require('../../stores/group');
const { isAdmin, isBanned, ban } = require('../../stores/user');
const { isAdmin, isBanned } = require('../../stores/user');
// Actions
const ban = require('../../actions/ban');
const banHandler = async (ctx) => {
const { message, reply, me } = ctx;
const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
const userToBan = message.reply_to_message
? Object.assign({ username: '' }, message.reply_to_message.from)
: message.commandMention
@@ -20,7 +22,7 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
: null;
const reason = message.text.split(' ').slice(1).join(' ').trim();
if (!state.isAdmin) return null;
if (ctx.from.status !== 'admin') return null;
if (message.chat.type === 'private') {
return reply(
@@ -48,10 +50,7 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
}
if (message.reply_to_message) {
bot.telegram.deleteMessage(
chat.id,
message.reply_to_message.message_id
);
ctx.deleteMessage(message.reply_to_message.message_id);
}
if (await isBanned(userToBan)) {
@@ -61,30 +60,7 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
);
}
try {
await ban(userToBan, reason);
} catch (err) {
logError(err);
}
const groups = await listGroups();
const bans = groups.map(group =>
telegram.kickChatMember(group.id, userToBan.id));
try {
await Promise.all(bans);
} catch (err) {
logError(err);
}
if (userToBan.first_name === '') {
return reply(`🚫 ${link(state.user)} <b>banned an user with id</b> ` +
`<code>${userToBan.id}</code> <b>for:</b>\n\n${reason}`, replyOptions);
}
return reply(`🚫 ${link(state.user)} <b>banned</b> ${link(userToBan)} ` +
`<b>for:</b>\n\n${reason}`, replyOptions);
return ban({ admin: ctx.from, reason, userToBan }).then(ctx.replyWithHTML);
};
module.exports = banHandler;