mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-10-17 14:35:56 +00:00
refactoring, created messages folder and added context state
This commit is contained in:
@@ -1,18 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
// DB
|
||||
const { isAdmin } = require('../../stores/user');
|
||||
const { addCommand } = require('../../stores/command');
|
||||
|
||||
// Bot
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
const addCommandHandler = async ({ chat, message, reply }) => {
|
||||
const user = message.from;
|
||||
const addCommandHandler = async ({ chat, reply, state }) => {
|
||||
const { isAdmin, user } = state;
|
||||
if (chat.type !== 'private') {
|
||||
return null;
|
||||
}
|
||||
if (!await isAdmin(user)) {
|
||||
if (!isAdmin) {
|
||||
return reply('ℹ️ <b>Sorry, only admins access this command.</b>',
|
||||
replyOptions);
|
||||
}
|
||||
|
@@ -1,13 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
// Utils
|
||||
const { loadJSON } = require('../../utils/json');
|
||||
const { link } = require('../../utils/tg');
|
||||
const { logError } = require('../../utils/log');
|
||||
|
||||
// Config
|
||||
const { masterID } = loadJSON('config.json');
|
||||
|
||||
// Bot
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
@@ -20,8 +16,9 @@ const {
|
||||
nowarns
|
||||
} = require('../../stores/user');
|
||||
|
||||
const adminHandler = async ({ message, reply }) => {
|
||||
if (message.from.id !== masterID) {
|
||||
const adminHandler = async ({ message, reply, state }) => {
|
||||
const { isMaster, user } = state;
|
||||
if (!isMaster) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -29,7 +26,7 @@ const adminHandler = async ({ message, reply }) => {
|
||||
? message.reply_to_message.from
|
||||
: message.commandMention
|
||||
? message.commandMention
|
||||
: message.from;
|
||||
: user;
|
||||
|
||||
if (await isBanned(userToAdmin)) {
|
||||
return reply('ℹ️ <b>Can\'t admin banned user.</b>', replyOptions);
|
||||
|
@@ -12,8 +12,8 @@ const { replyOptions } = require('../../bot/options');
|
||||
const { listGroups } = require('../../stores/group');
|
||||
const { isAdmin, isBanned, ban } = require('../../stores/user');
|
||||
|
||||
const banHandler = async ({ chat, message, reply, telegram, me }) => {
|
||||
if (!await isAdmin(message.from)) {
|
||||
const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
|
||||
if (!state.isAdmin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ const banHandler = async ({ chat, message, reply, telegram, me }) => {
|
||||
logError(err);
|
||||
}
|
||||
|
||||
return reply(`🚫 ${link(message.from)} <b>banned</b> ${link(userToBan)} ` +
|
||||
return reply(`🚫 ${link(state.user)} <b>banned</b> ${link(userToBan)} ` +
|
||||
`<b>for:</b>\n\n${reason}`, replyOptions);
|
||||
};
|
||||
|
||||
|
@@ -7,10 +7,11 @@ const { link } = require('../../utils/tg');
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { isAdmin, getWarns } = require('../../stores/user');
|
||||
const { getWarns } = require('../../stores/user');
|
||||
|
||||
const getWarnsHandler = async ({ message, reply }) => {
|
||||
if (!await isAdmin(message.from)) {
|
||||
const getWarnsHandler = async ({ message, reply, state }) => {
|
||||
const { isAdmin } = state;
|
||||
if (!isAdmin) {
|
||||
return null;
|
||||
}
|
||||
const theUser = message.reply_to_message
|
||||
|
@@ -1,15 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const { masterID } = require('../../config.json');
|
||||
|
||||
const { removeGroup } = require('../../stores/group');
|
||||
|
||||
const leaveCommandHandler = async ctx => {
|
||||
if (ctx.from.id !== masterID) {
|
||||
const leaveCommandHandler = async ({ chat, telegram, state }) => {
|
||||
const { isMaster } = state;
|
||||
if (!isMaster) {
|
||||
return null;
|
||||
}
|
||||
await removeGroup(ctx.chat);
|
||||
return ctx.telegram.leaveChat(ctx.chat.id);
|
||||
await removeGroup(chat);
|
||||
return telegram.leaveChat(chat.id);
|
||||
};
|
||||
|
||||
module.exports = leaveCommandHandler;
|
||||
|
@@ -8,10 +8,11 @@ const { logError } = require('../../utils/log');
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { isAdmin, getWarns, nowarns } = require('../../stores/user');
|
||||
const { getWarns, nowarns } = require('../../stores/user');
|
||||
|
||||
const nowarnsHandler = async ({ message, reply }) => {
|
||||
if (!await isAdmin(message.from)) {
|
||||
const nowarnsHandler = async ({ message, reply, state }) => {
|
||||
const { isAdmin, user } = state;
|
||||
if (!isAdmin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -39,7 +40,7 @@ const nowarnsHandler = async ({ message, reply }) => {
|
||||
}
|
||||
|
||||
return reply(
|
||||
`♻️ ${link(message.from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
||||
`♻️ ${link(user)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
||||
'<b>for all of their warnings.</b>',
|
||||
replyOptions);
|
||||
};
|
||||
|
@@ -1,25 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
// Utils
|
||||
const { loadJSON } = require('../../utils/json');
|
||||
|
||||
// Config
|
||||
const { masterID } = loadJSON('config.json');
|
||||
|
||||
// DB
|
||||
const { isAdmin } = require('../../stores/user');
|
||||
const { getCommand, removeCommand } = require('../../stores/command');
|
||||
|
||||
// Bot
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
const removeCommandHandler = async ({ chat, message, reply }) => {
|
||||
const user = message.from;
|
||||
const removeCommandHandler = async ({ chat, message, reply, state }) => {
|
||||
const { isAdmin, isMaster } = state;
|
||||
const { text } = message;
|
||||
if (chat.type !== 'private') {
|
||||
return null;
|
||||
}
|
||||
if (!await isAdmin(user)) {
|
||||
if (!isAdmin) {
|
||||
return reply('ℹ️ <b>Sorry, only admins access this command.</b>',
|
||||
replyOptions);
|
||||
}
|
||||
@@ -37,7 +30,7 @@ const removeCommandHandler = async ({ chat, message, reply }) => {
|
||||
replyOptions);
|
||||
}
|
||||
|
||||
if (command.role === 'Master' && user.id !== masterID) {
|
||||
if (command.role === 'Master' && !isMaster) {
|
||||
return reply('ℹ️ <b>Sorry, only master can remove this command.</b>',
|
||||
replyOptions);
|
||||
}
|
||||
|
@@ -1,21 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
// Utils
|
||||
const { loadJSON } = require('../../utils/json');
|
||||
const { link } = require('../../utils/tg');
|
||||
const { logError } = require('../../utils/log');
|
||||
|
||||
// Config
|
||||
const { masterID } = loadJSON('config.json');
|
||||
|
||||
// Bot
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { isAdmin, unadmin } = require('../../stores/user');
|
||||
|
||||
const unAdminHandler = async ({ message, reply }) => {
|
||||
if (message.from.id !== masterID) {
|
||||
const unAdminHandler = async ({ message, reply, state }) => {
|
||||
const { isMaster } = state;
|
||||
if (!isMaster) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@@ -9,12 +9,13 @@ const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { listGroups } = require('../../stores/group');
|
||||
const { isAdmin, isBanned, unban } = require('../../stores/user');
|
||||
const { isBanned, unban } = require('../../stores/user');
|
||||
|
||||
const noop = Function.prototype;
|
||||
|
||||
const unbanHandler = async ({ message, reply, telegram }) => {
|
||||
if (!await isAdmin(message.from)) {
|
||||
const unbanHandler = async ({ message, reply, telegram, state }) => {
|
||||
const { isAdmin, user } = state;
|
||||
if (!isAdmin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -60,7 +61,7 @@ const unbanHandler = async ({ message, reply, telegram }) => {
|
||||
// hance .catch(noop)
|
||||
// (it's an expected, non-critical failure)
|
||||
|
||||
return reply(`♻️ ${link(message.from)} <b>unbanned</b> ` +
|
||||
return reply(`♻️ ${link(user)} <b>unbanned</b> ` +
|
||||
`${link(userToUnban)}.`, replyOptions);
|
||||
};
|
||||
|
||||
|
@@ -7,10 +7,11 @@ const { link } = require('../../utils/tg');
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { isAdmin, getWarns, unwarn } = require('../../stores/user');
|
||||
const { getWarns, unwarn } = require('../../stores/user');
|
||||
|
||||
const unwarnHandler = async ({ message, reply }) => {
|
||||
if (!await isAdmin(message.from)) {
|
||||
const unwarnHandler = async ({ message, reply, state }) => {
|
||||
const { isAdmin, user } = state;
|
||||
if (!isAdmin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -35,9 +36,9 @@ const unwarnHandler = async ({ message, reply }) => {
|
||||
await unwarn(userToUnwarn);
|
||||
|
||||
return reply(
|
||||
`❎ ${link(message.from)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
||||
`❎ ${link(user)} <b>pardoned</b> ${link(userToUnwarn)} ` +
|
||||
`<b>for:</b>\n\n${allWarns[allWarns.length - 1]}` +
|
||||
`(${allWarns.length - 1}/3)`,
|
||||
` (${allWarns.length - 1}/3)`,
|
||||
replyOptions);
|
||||
};
|
||||
|
||||
|
@@ -15,8 +15,9 @@ const { replyOptions } = require('../../bot/options');
|
||||
// DB
|
||||
const { isAdmin, ban, getWarns, warn } = require('../../stores/user');
|
||||
|
||||
const warnHandler = async ({ message, chat, reply, me }) => {
|
||||
if (!await isAdmin(message.from)) {
|
||||
const warnHandler = async ({ message, chat, reply, me, state }) => {
|
||||
const { user } = state;
|
||||
if (!state.isAdmin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -59,14 +60,14 @@ const warnHandler = async ({ message, chat, reply, me }) => {
|
||||
|
||||
if (warnCount.length < numberOfWarnsToBan) {
|
||||
promises.push(reply(
|
||||
`⚠️ ${link(message.from)} <b>warned</b> ${link(userToWarn)} ` +
|
||||
`⚠️ ${link(user)} <b>warned</b> ${link(userToWarn)} ` +
|
||||
`<b>for:</b>\n\n ${reason} (${warnCount.length}/3)`,
|
||||
replyOptions));
|
||||
} else {
|
||||
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
|
||||
promises.push(ban(userToWarn, 'Reached max number of warnings'));
|
||||
promises.push(reply(
|
||||
`🚫 ${link(message.from)} <b>banned</b> ${link(userToWarn)} ` +
|
||||
`🚫 ${link(user)} <b>banned</b> ${link(userToWarn)} ` +
|
||||
'<b>for:</b>\n\nReached max number of warnings ' +
|
||||
`(${warnCount.length}/3)\n\n`,
|
||||
replyOptions));
|
||||
|
@@ -6,17 +6,17 @@ const { Markup } = require('telegraf');
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { isAdmin } = require('../../stores/user');
|
||||
const {
|
||||
getCommand,
|
||||
removeCommand,
|
||||
updateCommand
|
||||
} = require('../../stores/command');
|
||||
|
||||
const addCustomCmdHandler = async ({ chat, message, reply }, next) => {
|
||||
const addCustomCmdHandler = async ({ chat, message, reply, state }, next) => {
|
||||
const { text, photo, document, video, audio } = message;
|
||||
const { id } = message.from;
|
||||
|
||||
const { isAdmin, user } = state;
|
||||
const { id } = user;
|
||||
console.log(state);
|
||||
if (text && /^\/\w+/.test(text)) {
|
||||
await removeCommand({ id, isActive: false });
|
||||
return next();
|
||||
@@ -24,14 +24,13 @@ const addCustomCmdHandler = async ({ chat, message, reply }, next) => {
|
||||
|
||||
const command = await getCommand({ id, isActive: false });
|
||||
if (chat.type !== 'private' ||
|
||||
!await isAdmin(message.from) ||
|
||||
!isAdmin ||
|
||||
!command ||
|
||||
!command.state) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const { state } = command;
|
||||
if (state === 'add') {
|
||||
if (command.state === 'add') {
|
||||
if (!/^(?=\D)\w+$/.test(text)) {
|
||||
reply('Please send a valid command.');
|
||||
return next();
|
||||
@@ -56,7 +55,7 @@ const addCustomCmdHandler = async ({ chat, message, reply }, next) => {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (state === 'role') {
|
||||
if (command.state === 'role') {
|
||||
if (text !== 'Master' && text !== 'Admins' && text !== 'Everyone') {
|
||||
reply('Please send a valid role.', Markup.keyboard([
|
||||
[ 'Master', 'Admins', 'Everyone' ]
|
||||
@@ -75,7 +74,7 @@ const addCustomCmdHandler = async ({ chat, message, reply }, next) => {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (state === 'content') {
|
||||
if (command.state === 'content') {
|
||||
let newCommand;
|
||||
if (text) {
|
||||
newCommand = { content: text, type: 'text' };
|
41
handlers/messages/addUser.js
Normal file
41
handlers/messages/addUser.js
Normal file
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
// Utils
|
||||
const { loadJSON } = require('../../utils/json');
|
||||
|
||||
// Config
|
||||
const { masterID } = loadJSON('config.json');
|
||||
|
||||
// DB
|
||||
const { addUser, isUser } = require('../../stores/user');
|
||||
|
||||
const addUserHandler = async (ctx, next) => {
|
||||
const { message } = ctx;
|
||||
const newUser = message.from;
|
||||
const user = newUser && await isUser(message.from);
|
||||
const usersToAdd = [];
|
||||
|
||||
if (!user && newUser) {
|
||||
usersToAdd.push(addUser(newUser));
|
||||
}
|
||||
|
||||
ctx.state = {
|
||||
isAdmin: user && user.status === 'admin',
|
||||
isMaster: user.id === masterID,
|
||||
user: newUser,
|
||||
};
|
||||
|
||||
if (
|
||||
message.reply_to_message &&
|
||||
message.reply_to_message.from &&
|
||||
!await isUser(message.reply_to_message.from)
|
||||
) {
|
||||
usersToAdd.push(addUser(message.reply_to_message.from));
|
||||
}
|
||||
|
||||
await Promise.all(usersToAdd);
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
module.exports = addUserHandler;
|
@@ -17,10 +17,11 @@ const bot = require('../../bot');
|
||||
const { replyOptions } = require('../../bot/options');
|
||||
|
||||
// DB
|
||||
const { isAdmin, ban, warn } = require('../../stores/user');
|
||||
const { ban, warn } = require('../../stores/user');
|
||||
const { listGroups } = require('../../stores/group');
|
||||
|
||||
const removeLinks = async ({ message, chat, reply }, next) => {
|
||||
const removeLinks = async ({ message, chat, reply, state }, next) => {
|
||||
const { isAdmin, user } = state;
|
||||
const groups = await listGroups();
|
||||
const groupLinks = [
|
||||
...groups.map(group => group.link
|
||||
@@ -41,26 +42,25 @@ const removeLinks = async ({ message, chat, reply }, next) => {
|
||||
!(excludedChannels.includes(message.text) ||
|
||||
groupLinks.includes(message.text.split('/joinchat/')[1]))
|
||||
) {
|
||||
const userToWarn = message.from;
|
||||
if (await isAdmin(userToWarn)) {
|
||||
if (isAdmin) {
|
||||
return next();
|
||||
}
|
||||
const reason = 'Channel forward/link';
|
||||
const warnCount = await warn(userToWarn, reason);
|
||||
const warnCount = await warn(user, reason);
|
||||
const promises = [
|
||||
bot.telegram.deleteMessage(chat.id, message.message_id)
|
||||
];
|
||||
if (warnCount < numberOfWarnsToBan) {
|
||||
promises.push(reply(
|
||||
`⚠️ ${link(userToWarn)} <b>got warned!</b> (${warnCount}/3)` +
|
||||
`⚠️ ${link(user)} <b>got warned!</b> (${warnCount}/3)` +
|
||||
`\n\nReason: ${reason}`,
|
||||
replyOptions));
|
||||
} else {
|
||||
promises.push(bot.telegram.kickChatMember(chat.id, userToWarn.id));
|
||||
promises.push(ban(userToWarn,
|
||||
promises.push(bot.telegram.kickChatMember(chat.id, user.id));
|
||||
promises.push(ban(user,
|
||||
'Reached max number of warnings'));
|
||||
promises.push(reply(
|
||||
`🚫 ${link(userToWarn)} <b>got banned</b>! (${warnCount}/3)` +
|
||||
`🚫 ${link(user)} <b>got banned</b>! (${warnCount}/3)` +
|
||||
'\n\nReason: Reached max number of warnings',
|
||||
replyOptions));
|
||||
}
|
@@ -1,18 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
// Utils
|
||||
const { loadJSON } = require('../../utils/json');
|
||||
|
||||
// Config
|
||||
const { masterID } = loadJSON('config.json');
|
||||
|
||||
// DB
|
||||
const { getCommand } = require('../../stores/command');
|
||||
const { isAdmin } = require('../../stores/user');
|
||||
|
||||
const runCustomCmdHandler = async (ctx, next) => {
|
||||
const { message } = ctx;
|
||||
const user = message.from;
|
||||
const { message, state } = ctx;
|
||||
const { isAdmin, isMaster } = state;
|
||||
const isCommand = message.entities &&
|
||||
message.entities.filter(entity => entity.type === 'bot_command');
|
||||
if (!isCommand || !isCommand.length) {
|
||||
@@ -33,9 +26,9 @@ const runCustomCmdHandler = async (ctx, next) => {
|
||||
const options = Object.assign(replyTo, caption ? { caption } : {});
|
||||
if (
|
||||
role === 'Master' &&
|
||||
user.id !== masterID ||
|
||||
!isMaster ||
|
||||
role === 'Admins' &&
|
||||
!await isAdmin(user)
|
||||
!isAdmin
|
||||
) {
|
||||
return next();
|
||||
}
|
@@ -1,33 +0,0 @@
|
||||
'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(err);
|
||||
}
|
||||
});
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
module.exports = addUserHandler;
|
16
index.js
16
index.js
@@ -26,6 +26,7 @@ const delTimeout = 2 * 60 * 1000;
|
||||
* Path of middlewares and commands
|
||||
*/
|
||||
const middleware = './handlers/middlewares';
|
||||
const message = './handlers/messages';
|
||||
const command = './handlers/commands';
|
||||
|
||||
/**
|
||||
@@ -35,14 +36,19 @@ const command = './handlers/commands';
|
||||
const leaveUnmanagedHandler = require(`${middleware}/leaveUnmanaged`);
|
||||
const removeCommandsHandler = require(`${middleware}/removeCommands`);
|
||||
const kickBannedHandler = require(`${middleware}/kickBanned`);
|
||||
const addUserHandler = require(`${middleware}/addUser`);
|
||||
const removeLinksHandler = require(`${middleware}/removeLinks`);
|
||||
const checkUsernameHandler = require(`${middleware}/checkUsername`);
|
||||
const addCustomCmdHandler = require(`${middleware}/addCustomCmd`);
|
||||
const runCustomCmdHandler = require(`${middleware}/runCustomCmd`);
|
||||
const antibotHandler = require(`${middleware}/antibot`);
|
||||
const addedToGroupHandler = require(`${middleware}/addedToGroup`);
|
||||
|
||||
/**
|
||||
* @type {function}
|
||||
* Messages Handlers
|
||||
*/
|
||||
const addUserHandler = require(`${message}/addUser`);
|
||||
const removeLinksHandler = require(`${message}/removeLinks`);
|
||||
const checkUsernameHandler = require(`${message}/checkUsername`);
|
||||
const addCustomCmdHandler = require(`${message}/addCustomCmd`);
|
||||
const runCustomCmdHandler = require(`${message}/runCustomCmd`);
|
||||
|
||||
/**
|
||||
* @type {function}
|
||||
* Commmands Handlers
|
||||
|
Reference in New Issue
Block a user