mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-09-04 08:05:30 +00:00
merged warn db into user db & improved db methods
This commit is contained in:
@@ -12,9 +12,13 @@ const { masterID } = loadJSON('config.json');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { isAdmin, admin } = require('../../stores/user');
|
const {
|
||||||
const { isBanned } = require('../../stores/user');
|
isAdmin,
|
||||||
const { getWarns, nowarns } = require('../../stores/warn');
|
admin,
|
||||||
|
isBanned,
|
||||||
|
getWarns,
|
||||||
|
nowarns
|
||||||
|
} = require('../../stores/user');
|
||||||
|
|
||||||
const adminHandler = async ({ message, reply }) => {
|
const adminHandler = async ({ message, reply }) => {
|
||||||
if (message.from.id !== masterID) {
|
if (message.from.id !== masterID) {
|
||||||
|
@@ -10,8 +10,7 @@ const { replyOptions } = require('../../bot/options');
|
|||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { listGroups } = require('../../stores/group');
|
const { listGroups } = require('../../stores/group');
|
||||||
const { isBanned, ban } = require('../../stores/user');
|
const { isAdmin, isBanned, ban } = require('../../stores/user');
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
|
|
||||||
const banHandler = async ({ chat, message, reply, telegram }) => {
|
const banHandler = async ({ chat, message, reply, telegram }) => {
|
||||||
if (!await isAdmin(message.from)) {
|
if (!await isAdmin(message.from)) {
|
||||||
|
@@ -7,8 +7,7 @@ const { link } = require('../../utils/tg');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { getWarns } = require('../../stores/warn');
|
const { isAdmin, getWarns } = require('../../stores/user');
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
|
|
||||||
const getWarnsHandler = async ({ message, reply }) => {
|
const getWarnsHandler = async ({ message, reply }) => {
|
||||||
if (!await isAdmin(message.from)) {
|
if (!await isAdmin(message.from)) {
|
||||||
@@ -25,13 +24,13 @@ const getWarnsHandler = async ({ message, reply }) => {
|
|||||||
}
|
}
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const warns = await getWarns(theUser);
|
const warns = await getWarns(theUser);
|
||||||
if (warns.length < 1) {
|
if (!warns) {
|
||||||
return reply(`✅ <b>no warns for:</b> ${link(theUser)}`, replyOptions);
|
return reply(`✅ <b>no warns for:</b> ${link(theUser)}`, replyOptions);
|
||||||
}
|
}
|
||||||
return reply(`⚠️ <b>Warns for</b> ${link(theUser)}:\n\n` +
|
return reply(`⚠️ <b>Warns for</b> ${link(theUser)}:\n\n` +
|
||||||
warns
|
warns
|
||||||
.map(warn => ++i + '. ' + warn)
|
.map(warn => ++i + '. ' + warn)
|
||||||
.join('\n\n'), replyOptions);
|
.join('\n'), replyOptions);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = getWarnsHandler;
|
module.exports = getWarnsHandler;
|
||||||
|
@@ -8,8 +8,7 @@ const { logError } = require('../../utils/log');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { getWarns, nowarns } = require('../../stores/warn');
|
const { isAdmin, getWarns, nowarns } = require('../../stores/user');
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
|
|
||||||
const nowarnsHandler = async ({ message, reply }) => {
|
const nowarnsHandler = async ({ message, reply }) => {
|
||||||
if (!await isAdmin(message.from)) {
|
if (!await isAdmin(message.from)) {
|
||||||
@@ -29,7 +28,7 @@ const nowarnsHandler = async ({ message, reply }) => {
|
|||||||
|
|
||||||
const warns = await getWarns(userToUnwarn);
|
const warns = await getWarns(userToUnwarn);
|
||||||
|
|
||||||
if (warns.length < 1) {
|
if (!warns) {
|
||||||
return reply(`ℹ️ ${link(userToUnwarn)} <b>already has no warnings.</b>`,
|
return reply(`ℹ️ ${link(userToUnwarn)} <b>already has no warnings.</b>`,
|
||||||
replyOptions);
|
replyOptions);
|
||||||
}
|
}
|
||||||
|
@@ -9,8 +9,7 @@ const { replyOptions } = require('../../bot/options');
|
|||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { listGroups } = require('../../stores/group');
|
const { listGroups } = require('../../stores/group');
|
||||||
const { isBanned, unban } = require('../../stores/user');
|
const { isAdmin, isBanned, unban } = require('../../stores/user');
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
|
|
||||||
const noop = Function.prototype;
|
const noop = Function.prototype;
|
||||||
|
|
||||||
|
@@ -7,8 +7,7 @@ const { link } = require('../../utils/tg');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const Warn = require('../../stores/warn');
|
const { isAdmin, getWarns, unwarn } = require('../../stores/user');
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
|
|
||||||
const unwarnHandler = async ({ message, reply }) => {
|
const unwarnHandler = async ({ message, reply }) => {
|
||||||
if (!await isAdmin(message.from)) {
|
if (!await isAdmin(message.from)) {
|
||||||
@@ -26,12 +25,19 @@ const unwarnHandler = async ({ message, reply }) => {
|
|||||||
replyOptions);
|
replyOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
const allWarns = await Warn.getWarns(userToUnwarn);
|
const allWarns = await getWarns(userToUnwarn);
|
||||||
const warn = await Warn.unwarn(userToUnwarn);
|
|
||||||
|
if (!allWarns) {
|
||||||
|
return reply(`ℹ️ ${link(userToUnwarn)} <b>already has no warnings.</b>`,
|
||||||
|
replyOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
await unwarn(userToUnwarn);
|
||||||
|
|
||||||
return reply(
|
return reply(
|
||||||
`❎ ${link(message.from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
`❎ ${link(message.from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
||||||
`<b>for:</b>\n\n${warn} (${allWarns.length}/3)`,
|
`<b>for:</b>\n\n${allWarns[allWarns.length - 1]}` +
|
||||||
|
`(${allWarns.length}/3)`,
|
||||||
replyOptions);
|
replyOptions);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -13,9 +13,7 @@ const bot = require('../../bot');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { warn } = require('../../stores/warn');
|
const { isAdmin, ban, getWarns, warn } = require('../../stores/user');
|
||||||
const { ban } = require('../../stores/user');
|
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
|
|
||||||
const warnHandler = async ({ message, chat, reply }) => {
|
const warnHandler = async ({ message, chat, reply }) => {
|
||||||
if (!await isAdmin(message.from)) {
|
if (!await isAdmin(message.from)) {
|
||||||
@@ -43,7 +41,8 @@ const warnHandler = async ({ message, chat, reply }) => {
|
|||||||
return reply('ℹ️ <b>Can\'t warn other admins.</b>', replyOptions);
|
return reply('ℹ️ <b>Can\'t warn other admins.</b>', replyOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
const warnCount = await warn(userToWarn, reason);
|
await warn(userToWarn, reason);
|
||||||
|
const warnCount = await getWarns(userToWarn);
|
||||||
const promises = [
|
const promises = [
|
||||||
bot.telegram.deleteMessage(chat.id, message.message_id)
|
bot.telegram.deleteMessage(chat.id, message.message_id)
|
||||||
];
|
];
|
||||||
@@ -54,10 +53,10 @@ const warnHandler = async ({ message, chat, reply }) => {
|
|||||||
message.reply_to_message.message_id));
|
message.reply_to_message.message_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (warnCount < numberOfWarnsToBan) {
|
if (warnCount.length < numberOfWarnsToBan) {
|
||||||
promises.push(reply(
|
promises.push(reply(
|
||||||
`⚠️ ${link(message.from)} <b>warned</b> ${link(userToWarn)} ` +
|
`⚠️ ${link(message.from)} <b>warned</b> ${link(userToWarn)} ` +
|
||||||
`<b>for:</b>\n\n ${reason} (${warnCount}/3)`,
|
`<b>for:</b>\n\n ${reason} (${warnCount.length}/3)`,
|
||||||
replyOptions));
|
replyOptions));
|
||||||
} else {
|
} else {
|
||||||
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
|
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
|
||||||
@@ -65,7 +64,7 @@ const warnHandler = async ({ message, chat, reply }) => {
|
|||||||
promises.push(reply(
|
promises.push(reply(
|
||||||
`🚫 ${link(message.from)} <b>banned</b> ${link(userToWarn)} ` +
|
`🚫 ${link(message.from)} <b>banned</b> ${link(userToWarn)} ` +
|
||||||
'<b>for:</b>\n\nReached max number of warnings ' +
|
'<b>for:</b>\n\nReached max number of warnings ' +
|
||||||
`(${warnCount}/3)\n\n`,
|
`(${warnCount.length}/3)\n\n`,
|
||||||
replyOptions));
|
replyOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
handlers/middlewares/addUser.js
Normal file
33
handlers/middlewares/addUser.js
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
const { logError } = require('../../utils/log');
|
||||||
|
|
||||||
|
// DB
|
||||||
|
const { addUser, isUser } = require('../../stores/user');
|
||||||
|
|
||||||
|
const addUserHandler = async ({ message }, next) => {
|
||||||
|
const usersToAdd = [];
|
||||||
|
if (message.from && !await isUser(message.from)) {
|
||||||
|
usersToAdd.push(message.from);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
message.reply_to_message &&
|
||||||
|
message.reply_to_message.from &&
|
||||||
|
!await isUser(message.reply_to_message.from)
|
||||||
|
) {
|
||||||
|
usersToAdd.push(message.reply_to_message.from);
|
||||||
|
}
|
||||||
|
|
||||||
|
usersToAdd.forEach(async user => {
|
||||||
|
try {
|
||||||
|
await addUser(user);
|
||||||
|
} catch (err) {
|
||||||
|
logError(process.env.DEBUG)(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return next();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = addUserHandler;
|
@@ -9,18 +9,10 @@ const bot = require('../../bot');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { addUser, isUser } = require('../../stores/user');
|
|
||||||
const { isBanned } = require('../../stores/user');
|
const { isBanned } = require('../../stores/user');
|
||||||
|
|
||||||
const middlewareHandler = async ({ chat, from, message, reply }, next) => {
|
const middlewareHandler = async ({ chat, from, message, reply }, next) => {
|
||||||
process.env.DEBUG === 'true' && message && print(message);
|
process.env.DEBUG === 'true' && message && print(message);
|
||||||
if (message && message.from && !await isUser(message.from)) {
|
|
||||||
try {
|
|
||||||
await addUser(message.from);
|
|
||||||
} catch (err) {
|
|
||||||
logError(process.env.DEBUG)(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (
|
if (
|
||||||
message &&
|
message &&
|
||||||
message.text &&
|
message.text &&
|
||||||
|
@@ -17,9 +17,7 @@ const bot = require('../../bot');
|
|||||||
const { replyOptions } = require('../../bot/options');
|
const { replyOptions } = require('../../bot/options');
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
const { warn } = require('../../stores/warn');
|
const { isAdmin, ban, warn } = require('../../stores/user');
|
||||||
const { ban } = require('../../stores/user');
|
|
||||||
const { isAdmin } = require('../../stores/user');
|
|
||||||
const { listGroups } = require('../../stores/group');
|
const { listGroups } = require('../../stores/group');
|
||||||
|
|
||||||
const removeLinks = async ({ message, chat, reply }, next) => {
|
const removeLinks = async ({ message, chat, reply }, next) => {
|
||||||
|
2
index.js
2
index.js
@@ -15,6 +15,7 @@ bot.telegram.getMe().then((botInfo) => {
|
|||||||
// Middleware Handlers
|
// Middleware Handlers
|
||||||
const leaveUnmanagedHandler = require('./handlers/middlewares/leaveUnmanaged');
|
const leaveUnmanagedHandler = require('./handlers/middlewares/leaveUnmanaged');
|
||||||
const middlewareHandler = require('./handlers/middlewares/middleware');
|
const middlewareHandler = require('./handlers/middlewares/middleware');
|
||||||
|
const addUserHandler = require('./handlers/middlewares/addUser');
|
||||||
const removeLinksHandler = require('./handlers/middlewares/removeLinks');
|
const removeLinksHandler = require('./handlers/middlewares/removeLinks');
|
||||||
const checkUsernameHandler = require('./handlers/middlewares/checkUsername');
|
const checkUsernameHandler = require('./handlers/middlewares/checkUsername');
|
||||||
const antibotHandler = require('./handlers/middlewares/antibot');
|
const antibotHandler = require('./handlers/middlewares/antibot');
|
||||||
@@ -36,6 +37,7 @@ const groupsHandler = require('./handlers/commands/groups');
|
|||||||
bot.on('new_chat_members', addedToGroupHandler);
|
bot.on('new_chat_members', addedToGroupHandler);
|
||||||
bot.use(leaveUnmanagedHandler);
|
bot.use(leaveUnmanagedHandler);
|
||||||
bot.use(middlewareHandler);
|
bot.use(middlewareHandler);
|
||||||
|
bot.on('message', addUserHandler);
|
||||||
bot.on('message', removeLinksHandler);
|
bot.on('message', removeLinksHandler);
|
||||||
bot.on('message', checkUsernameHandler);
|
bot.on('message', checkUsernameHandler);
|
||||||
bot.on('new_chat_members', antibotHandler);
|
bot.on('new_chat_members', antibotHandler);
|
||||||
|
@@ -32,11 +32,10 @@ const isUser = ({ id }) =>
|
|||||||
const getUser = user =>
|
const getUser = user =>
|
||||||
User.findOne(user);
|
User.findOne(user);
|
||||||
|
|
||||||
const admin = ({ id, first_name = '', last_name = '', username = '' }) =>
|
const admin = ({ id }) =>
|
||||||
User.update(
|
User.update(
|
||||||
{ id },
|
{ id },
|
||||||
{ first_name, id, last_name, status: 'admin', username, warns: [] })
|
{ $set: { status: 'admin' } });
|
||||||
.catch(logError(process.env.DEBUG));
|
|
||||||
|
|
||||||
const getAdmins = () =>
|
const getAdmins = () =>
|
||||||
User.find({ status: 'admin' });
|
User.find({ status: 'admin' });
|
||||||
@@ -47,24 +46,30 @@ const unadmin = ({ id }) =>
|
|||||||
const isAdmin = ({ id }) =>
|
const isAdmin = ({ id }) =>
|
||||||
User.findOne({ id, status: 'admin' });
|
User.findOne({ id, status: 'admin' });
|
||||||
|
|
||||||
const ban = (userToBan, banReason) => {
|
const ban = ({ id }, ban_reason) =>
|
||||||
const { id, first_name = '', last_name = '', username = '' } = userToBan;
|
User.update({ id }, { $set: { ban_reason, status: 'banned', warns: [] } });
|
||||||
const userObj = { first_name, id, last_name, username, warns: [] };
|
|
||||||
return User.findOne({ id })
|
|
||||||
.then(user => user
|
|
||||||
? User.update({ id }, { $set: { banReason, status: 'banned' } })
|
|
||||||
: User.update(
|
|
||||||
{ id },
|
|
||||||
Object.assign({}, userObj, { banReason, status: 'banned' }),
|
|
||||||
{ upsert: true }));
|
|
||||||
};
|
|
||||||
|
|
||||||
const unban = ({ id }) =>
|
const unban = ({ id }) =>
|
||||||
User.update({ id }, { $set: { banReason: '', status: 'member' } });
|
User.update({ id }, { $set: { banReason: '', status: 'member' } });
|
||||||
|
|
||||||
const isBanned = ({ id }) =>
|
const isBanned = ({ id }) =>
|
||||||
User.findOne({ id, status: 'banned' })
|
User.findOne({ id, status: 'banned' })
|
||||||
.then(user => user ? user.banReason : null);
|
.then(user => user ? user.ban_reason : null);
|
||||||
|
|
||||||
|
const warn = ({ id }, reason) =>
|
||||||
|
User.update({ id }, { $push: { warns: reason } });
|
||||||
|
|
||||||
|
const unwarn = ({ id }) =>
|
||||||
|
User.update({ id }, { $pop: { warns: 1 } });
|
||||||
|
|
||||||
|
const nowarns = ({ id }) =>
|
||||||
|
User.update({ id }, { $set: { warns: [] } });
|
||||||
|
|
||||||
|
const getWarns = ({ id }) =>
|
||||||
|
User.findOne({ id })
|
||||||
|
.then(user => user && user.warns.length > 0
|
||||||
|
? user.warns
|
||||||
|
: null);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
addUser,
|
addUser,
|
||||||
@@ -72,9 +77,13 @@ module.exports = {
|
|||||||
ban,
|
ban,
|
||||||
getAdmins,
|
getAdmins,
|
||||||
getUser,
|
getUser,
|
||||||
|
getWarns,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
isBanned,
|
isBanned,
|
||||||
isUser,
|
isUser,
|
||||||
|
nowarns,
|
||||||
unadmin,
|
unadmin,
|
||||||
unban
|
unban,
|
||||||
|
unwarn,
|
||||||
|
warn
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user