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