2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-23 02:17:47 +00:00
the-guard-bot/index.js

165 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-07-24 17:12:38 +02:00
'use strict';
2017-07-24 18:08:31 +02:00
const DEBUG = true;
2017-07-24 17:12:38 +02:00
// validate config
require('child_process').execSync('node init.js', { stdio: 'inherit' });
const Telegraf = require('telegraf');
// Utils
const { loadJSON } = require('./utils/json');
const { print, logError } = require('./utils/log');
2017-07-24 18:08:31 +02:00
const { link, deleteAfter } = require('./utils/tg');
2017-07-24 17:12:38 +02:00
// DBs
const bans = require('./stores/bans');
const warns = require('./stores/warns');
const admins = require('./stores/admins');
const replyOptions = {
disable_web_page_preview: true,
parse_mode: 'HTML'
};
const config = loadJSON('config.json');
const bot = new Telegraf(config.token);
2017-07-24 18:27:48 +02:00
DEBUG && bot.command('adminme', ctx =>
2017-07-24 18:08:31 +02:00
(admins.admin(ctx.from),
ctx.reply('Admined')));
bot.on('new_chat_member', deleteAfter(10 * 60 * 1000));
bot.on('left_chat_member', deleteAfter(10 * 60 * 1000));
2017-07-24 17:12:38 +02:00
bot.use(async (ctx, next) => {
2017-07-24 18:17:47 +02:00
DEBUG && ctx.message && print(ctx.message);
2017-07-24 17:12:38 +02:00
const banned = await bans.isBanned(ctx.from);
if (banned) {
return bot.telegram.kickChatMember(ctx.chat.id, ctx.from.id)
.then(() => ctx.reply(
`${link(ctx.from)} <b>banned</b>!\n` +
`Reason: ${banned}`,
replyOptions))
.catch(logError(DEBUG))
.then(next);
}
return next();
});
bot.command('warn', async ({ message, chat, reply }) => {
if (!await admins.isAdmin(message.from)) {
return null;
}
if (!message.reply_to_message) {
return reply('Reply to a message');
}
const messageToWarn = message.reply_to_message;
const userToWarn = messageToWarn.from;
const reason = message.text.split(' ').slice(1).join(' ').trim();
if (reason.length === 0) {
return reply('Need a reason');
}
2017-07-24 18:08:31 +02:00
if (await admins.isAdmin(userToWarn)) {
return reply('Can\'t warn other admin');
}
2017-07-24 17:12:38 +02:00
const warnCount = await warns.warn(userToWarn, reason);
const promises = [
bot.telegram.deleteMessage(chat.id, messageToWarn.message_id),
bot.telegram.deleteMessage(chat.id, message.message_id)
];
if (warnCount < 3) {
promises.push(reply(
`${link(userToWarn)} warned! (${warnCount}/3)\n` +
`Reason: ${reason}`,
replyOptions));
} else {
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
promises.push(bans.ban(userToWarn, 'Reached max number of warnings'));
promises.push(reply(
`${link(userToWarn)} <b>banned</b>! (${warnCount}/3)\n` +
'Reason: Reached max number of warnings',
replyOptions));
}
return Promise.all(promises).catch(logError(DEBUG));
});
bot.command('unwarn', async ({ message, reply }) => {
if (!await admins.isAdmin(message.from)) {
return null;
}
if (!message.reply_to_message) {
return reply('Reply to a message');
}
const messageToUnwarn = message.reply_to_message;
const userToUnwarn = messageToUnwarn.from;
2017-07-24 18:08:31 +02:00
const warnCount = await warns.getWarns(userToUnwarn);
2017-07-24 17:12:38 +02:00
const warn = await warns.unwarn(userToUnwarn);
return reply(
`${link(userToUnwarn)} pardoned for: ${warn}\n(${warnCount}/3)`,
replyOptions);
});
2017-07-24 18:08:31 +02:00
bot.command('warns', async ({ message, reply }) => {
if (!await admins.isAdmin(message.from)) {
return null;
}
if (!message.reply_to_message) {
return reply('Reply to a message');
}
2017-07-24 18:17:47 +02:00
let i = 0;
const theUser = message.reply_to_message.from;
2017-07-24 18:19:29 +02:00
return reply('Warns for ' + link(theUser) + ':\n' +
2017-07-24 18:17:47 +02:00
(await warns.getWarns(theUser))
.map(x => ++i + '. ' + x)
.join('\n'), replyOptions);
});
bot.on('message', async ({ message, chat, reply }) => {
if (
message.forward_from_chat &&
message.forward_from_chat.type !== 'private' &&
message.forward_from_chat.username !== 'thedevs'
) {
const userToWarn = message.from;
if (await admins.isAdmin(userToWarn)) {
return null;
}
const reason = 'Forward from ' +
message.forward_from_chat.type +
': ' + link(message.forward_from_chat);
const warnCount = await warns.warn(userToWarn, reason);
const promises = [
bot.telegram.deleteMessage(chat.id, message.message_id)
];
if (warnCount < 3) {
promises.push(reply(
`${link(userToWarn)} warned! (${warnCount}/3)\n` +
`Reason: ${reason}`,
replyOptions));
} else {
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
promises.push(bans.ban(userToWarn,
'Reached max number of warnings'));
promises.push(reply(
`${link(userToWarn)} <b>banned</b>! (${warnCount}/3)\n` +
'Reason: Reached max number of warnings',
replyOptions));
}
return Promise.all(promises).catch(logError(DEBUG));
}
return null;
2017-07-24 18:08:31 +02:00
});
2017-07-24 17:12:38 +02:00
bot.startPolling();