mirror of
https://github.com/thedevs-network/the-guard-bot
synced 2025-08-30 21:55:17 +00:00
Make some handlers channel-friendly
This commit is contained in:
@@ -13,8 +13,8 @@ const preserved = require('../commands').handlers;
|
|||||||
|
|
||||||
const addCommandHandler = async (ctx) => {
|
const addCommandHandler = async (ctx) => {
|
||||||
const { chat, message, reply } = ctx;
|
const { chat, message, reply } = ctx;
|
||||||
const { id } = ctx.from;
|
|
||||||
if (chat.type !== 'private') return null;
|
if (chat.type !== 'private') return null;
|
||||||
|
const { id } = ctx.from;
|
||||||
|
|
||||||
if (ctx.from.status !== 'admin') {
|
if (ctx.from.status !== 'admin') {
|
||||||
return reply(
|
return reply(
|
||||||
|
@@ -69,11 +69,11 @@ const commandReferenceHandler = async (ctx) => {
|
|||||||
: '';
|
: '';
|
||||||
|
|
||||||
const customCommandsText = masterCommands.repeat(isMaster(ctx.from)) +
|
const customCommandsText = masterCommands.repeat(isMaster(ctx.from)) +
|
||||||
adminCommands.repeat(ctx.from.status === 'admin') +
|
adminCommands.repeat(ctx.from && ctx.from.status === 'admin') +
|
||||||
userCommands +
|
userCommands +
|
||||||
'\n<b>Custom commands(prefix with !):</b>\n' +
|
'\n<b>Custom commands(prefix with !):</b>\n' +
|
||||||
masterCustomCommands.repeat(isMaster(ctx.from)) +
|
masterCustomCommands.repeat(isMaster(ctx.from)) +
|
||||||
adminCustomCommands.repeat(ctx.from.status === 'admin') +
|
adminCustomCommands.repeat(ctx.from && ctx.from.status === 'admin') +
|
||||||
userCustomCommands;
|
userCustomCommands;
|
||||||
|
|
||||||
return ctx.replyWithHTML(customCommandsText)
|
return ctx.replyWithHTML(customCommandsText)
|
||||||
|
@@ -9,6 +9,7 @@ const { replyOptions } = require('../../bot/options');
|
|||||||
const { chats = {} } = require('../../config');
|
const { chats = {} } = require('../../config');
|
||||||
|
|
||||||
const reportHandler = async ctx => {
|
const reportHandler = async ctx => {
|
||||||
|
if (!ctx.chat.type.endsWith('group')) return null;
|
||||||
const msg = ctx.message;
|
const msg = ctx.message;
|
||||||
if (!msg.reply_to_message) {
|
if (!msg.reply_to_message) {
|
||||||
return ctx.reply(
|
return ctx.reply(
|
||||||
|
@@ -32,7 +32,9 @@ const createNewCommand = ctx => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addCustomCmdHandler = async (ctx, next) => {
|
const addCustomCmdHandler = async (ctx, next) => {
|
||||||
const { chat, message, reply, from } = ctx;
|
if (ctx.chat.type !== 'private') return next();
|
||||||
|
|
||||||
|
const { message, reply, from } = ctx;
|
||||||
const { text } = message;
|
const { text } = message;
|
||||||
const { id } = from;
|
const { id } = from;
|
||||||
const isAdmin = from.status === 'admin';
|
const isAdmin = from.status === 'admin';
|
||||||
@@ -43,8 +45,7 @@ const addCustomCmdHandler = async (ctx, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const command = await getCommand({ id, isActive: false });
|
const command = await getCommand({ id, isActive: false });
|
||||||
if (chat.type !== 'private' ||
|
if (!isAdmin ||
|
||||||
!isAdmin ||
|
|
||||||
!command ||
|
!command ||
|
||||||
!command.state) {
|
!command.state) {
|
||||||
return next();
|
return next();
|
||||||
|
@@ -155,7 +155,7 @@ const classifyList = (urls) =>
|
|||||||
const matchTmeLinks = R.match(/\b(?:t\.me|telegram\.(?:me|dog))\/[\w-/]+/gi);
|
const matchTmeLinks = R.match(/\b(?:t\.me|telegram\.(?:me|dog))\/[\w-/]+/gi);
|
||||||
|
|
||||||
const classifyCtx = (ctx) => {
|
const classifyCtx = (ctx) => {
|
||||||
if (ctx.chat.type === 'private') return Action.Nothing;
|
if (!ctx.chat.type.endsWith('group')) return Action.Nothing;
|
||||||
|
|
||||||
const message = ctx.message || ctx.editedMessage;
|
const message = ctx.message || ctx.editedMessage;
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const kickBannedHandler = (ctx, next) => {
|
const kickBannedHandler = (ctx, next) => {
|
||||||
if (ctx.chat.type === 'private') {
|
if (!ctx.chat.type.endsWith('group')) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
if (ctx.from.status === 'banned') {
|
if (ctx.from.status === 'banned') {
|
||||||
|
@@ -16,6 +16,8 @@ const isChannelForward = R.pathEq(
|
|||||||
);
|
);
|
||||||
const fromAdmin = R.pathEq([ 'from', 'status' ], 'admin');
|
const fromAdmin = R.pathEq([ 'from', 'status' ], 'admin');
|
||||||
|
|
||||||
|
const inGroup = ctx => ctx.chat.type.endsWith('group');
|
||||||
|
|
||||||
const capturingGroups = R.tail;
|
const capturingGroups = R.tail;
|
||||||
|
|
||||||
const toUsername = R.compose(
|
const toUsername = R.compose(
|
||||||
@@ -35,6 +37,7 @@ const fromWhitelisted = ctx =>
|
|||||||
isWhitelisted(ctx.message.forward_from_chat.username || '');
|
isWhitelisted(ctx.message.forward_from_chat.username || '');
|
||||||
|
|
||||||
const pred = R.allPass([
|
const pred = R.allPass([
|
||||||
|
inGroup,
|
||||||
isChannelForward,
|
isChannelForward,
|
||||||
R.complement(fromAdmin),
|
R.complement(fromAdmin),
|
||||||
R.complement(fromWhitelisted),
|
R.complement(fromWhitelisted),
|
||||||
|
@@ -10,6 +10,8 @@ const updateUserDataHandler = async (ctx, next) => {
|
|||||||
updateUser(ctx.message.forward_from);
|
updateUser(ctx.message.forward_from);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!ctx.from) return next();
|
||||||
|
|
||||||
const user = await updateUser(ctx.from);
|
const user = await updateUser(ctx.from);
|
||||||
|
|
||||||
Object.defineProperty(ctx, 'from', { value: { ...user, ...ctx.from } });
|
Object.defineProperty(ctx, 'from', { value: { ...user, ...ctx.from } });
|
||||||
|
@@ -100,8 +100,13 @@ const getAdmins = () =>
|
|||||||
const unadmin = ({ id }) =>
|
const unadmin = ({ id }) =>
|
||||||
User.update({ id }, { $set: { status: 'member' } });
|
User.update({ id }, { $set: { status: 'member' } });
|
||||||
|
|
||||||
const isAdmin = ({ id }) =>
|
const isAdmin = (user) => {
|
||||||
User.findOne({ id, status: 'admin' });
|
if (!user) return false;
|
||||||
|
|
||||||
|
if (user.status) return user.status === 'admin';
|
||||||
|
|
||||||
|
return User.findOne({ id: user.id, status: 'admin' });
|
||||||
|
};
|
||||||
|
|
||||||
const ban = ({ id }, ban_details) => {
|
const ban = ({ id }, ban_details) => {
|
||||||
const ban_reason = ban_details.reason;
|
const ban_reason = ban_details.reason;
|
||||||
|
@@ -11,9 +11,9 @@ if (!masterById && !masterByUsername) {
|
|||||||
config.master);
|
config.master);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMaster = masterById
|
const isMaster = masterByUsername
|
||||||
? user => user.id === Number(config.master)
|
? user => user && user.username && eq.username(user.username, config.master)
|
||||||
: user => user.username && eq.username(user.username, config.master);
|
: user => user && user.id === Number(config.master);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isMaster,
|
isMaster,
|
||||||
|
Reference in New Issue
Block a user