Improved admin group filter

This commit is contained in:
Profitroll 2022-12-30 20:36:06 +01:00
parent 8b2abc2cfa
commit e6589fc3e5
12 changed files with 36 additions and 13 deletions

9
app.py
View File

@ -1,5 +1,7 @@
from os import path, sep
from ujson import JSONDecodeError
from modules.logging import logWrite from modules.logging import logWrite
from modules.utils import configGet from modules.utils import configGet, jsonLoad
from pyrogram.client import Client from pyrogram.client import Client
from pyrogram.errors import bad_request_400 from pyrogram.errors import bad_request_400
@ -8,6 +10,11 @@ app = Client("holochecker", bot_token=configGet("bot_token", "bot"), api_id=conf
async def isAnAdmin(admin_id): async def isAnAdmin(admin_id):
if (admin_id == configGet("owner")) or (admin_id in configGet("admins")): if (admin_id == configGet("owner")) or (admin_id in configGet("admins")):
return True return True
if path.exists(f"cache{sep}admins") is True:
try:
return True if admin_id in jsonLoad(f"cache{sep}admins") else False
except (FileNotFoundError, JSONDecodeError):
pass
try: try:
async for member in app.get_chat_members(configGet("admin_group")): async for member in app.get_chat_members(configGet("admin_group")):
if member.user.id == admin_id: if member.user.id == admin_id:

View File

@ -12,7 +12,7 @@ from modules.database import col_applications
from modules import custom_filters from modules import custom_filters
# Applications command ========================================================================================================= # Applications command =========================================================================================================
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.command(["application"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.command(["application"], prefixes=["/"]) & custom_filters.admin)
async def cmd_application(app: Client, msg: Message): async def cmd_application(app: Client, msg: Message):
try: try:

View File

@ -11,7 +11,7 @@ from modules.database import col_applications
from modules import custom_filters from modules import custom_filters
# Applications command ========================================================================================================= # Applications command =========================================================================================================
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.command(["applications"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.command(["applications"], prefixes=["/"]) & custom_filters.admin)
async def cmd_applications(app: Client, msg: Message): async def cmd_applications(app: Client, msg: Message):
logWrite(f"Admin {msg.from_user.id} requested export of a database") logWrite(f"Admin {msg.from_user.id} requested export of a database")

View File

@ -6,7 +6,7 @@ from modules.utils import locale, should_quote, find_user
from classes.holo_user import HoloUser, LabelTooLongError from classes.holo_user import HoloUser, LabelTooLongError
from modules import custom_filters from modules import custom_filters
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.private & filters.command(["label"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.private & filters.command(["label"], prefixes=["/"]) & custom_filters.admin)
async def cmd_label(app: Client, msg: Message): async def cmd_label(app: Client, msg: Message):
if len(msg.command) < 3: if len(msg.command) < 3:

View File

@ -7,7 +7,7 @@ from modules.utils import logWrite, locale, should_quote
from modules import custom_filters from modules import custom_filters
# Message command ============================================================================================================== # Message command ==============================================================================================================
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.command(["message"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.command(["message"], prefixes=["/"]) & custom_filters.admin)
async def cmd_message(app: Client, msg: Message): async def cmd_message(app: Client, msg: Message):
try: try:

View File

@ -10,7 +10,7 @@ from modules.database import col_applications
from classes.errors.geo import PlaceNotFoundError from classes.errors.geo import PlaceNotFoundError
# Nearby command =============================================================================================================== # Nearby command ===============================================================================================================
@app.on_message(~ filters.scheduled & (custom_filters.allowed | custom_filters.admin) & (filters.private | (filters.chat(configGet("admin_group")) | filters.chat(configGet("destination_group")))) & filters.command(["nearby"], prefixes=["/"])) @app.on_message(~ filters.scheduled & (filters.private | (filters.chat(configGet("admin_group")) | filters.chat(configGet("destination_group")))) & filters.command(["nearby"], prefixes=["/"]) & (custom_filters.allowed | custom_filters.admin))
async def cmd_nearby(app: Client, msg: Message): async def cmd_nearby(app: Client, msg: Message):
holo_user = HoloUser(msg.from_user) holo_user = HoloUser(msg.from_user)

View File

@ -11,7 +11,7 @@ from modules import custom_filters
pid = getpid() pid = getpid()
# Shutdown command ============================================================================================================= # Shutdown command =============================================================================================================
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.private & filters.command(["kill", "die", "reboot"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.private & filters.command(["kill", "die", "reboot"], prefixes=["/"]) & custom_filters.admin)
async def cmd_kill(app: Client, msg: Message): async def cmd_kill(app: Client, msg: Message):
logWrite(f"Shutting down bot with pid {pid}") logWrite(f"Shutting down bot with pid {pid}")

View File

@ -8,7 +8,7 @@ from modules.utils import locale, should_quote
from modules.database import col_applications from modules.database import col_applications
# Sponsorship command ========================================================================================================== # Sponsorship command ==========================================================================================================
@app.on_message(~ filters.scheduled & (custom_filters.allowed | custom_filters.admin) & filters.command(["sponsorship"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.command(["sponsorship"], prefixes=["/"]) & (custom_filters.allowed | custom_filters.admin))
async def cmd_sponsorship(app: Client, msg: Message): async def cmd_sponsorship(app: Client, msg: Message):
if HoloUser(msg.from_user).application_state()[0] == "fill": if HoloUser(msg.from_user).application_state()[0] == "fill":
await msg.reply_text(locale("finish_application", "message", locale=msg.from_user), quote=should_quote(msg)) await msg.reply_text(locale("finish_application", "message", locale=msg.from_user), quote=should_quote(msg))

View File

@ -8,7 +8,7 @@ from modules.database import col_warnings
from modules import custom_filters from modules import custom_filters
# Warn command ================================================================================================================= # Warn command =================================================================================================================
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.command(["warn"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.command(["warn"], prefixes=["/"]) & custom_filters.admin)
async def cmd_warn(app: Client, msg: Message): async def cmd_warn(app: Client, msg: Message):
if msg.chat.id == configGet("destination_group"): if msg.chat.id == configGet("destination_group"):

View File

@ -8,7 +8,7 @@ from modules.database import col_users, col_warnings
from modules import custom_filters from modules import custom_filters
# Warnings command ============================================================================================================= # Warnings command =============================================================================================================
@app.on_message(~ filters.scheduled & custom_filters.admin & filters.command(["warnings"], prefixes=["/"])) @app.on_message(~ filters.scheduled & filters.command(["warnings"], prefixes=["/"]) & custom_filters.admin)
async def cmd_warnings(app: Client, msg: Message): async def cmd_warnings(app: Client, msg: Message):
if len(msg.command) <= 1: if len(msg.command) <= 1:

View File

@ -10,7 +10,7 @@ from classes.holo_user import HoloUser
from modules import custom_filters from modules import custom_filters
# Contact getting ============================================================================================================== # Contact getting ==============================================================================================================
@app.on_message(~ filters.scheduled & (custom_filters.allowed | custom_filters.admin) & filters.contact & filters.private) @app.on_message(~ filters.scheduled & filters.contact & filters.private & (custom_filters.allowed | custom_filters.admin))
async def get_contact(app: Client, msg: Message): async def get_contact(app: Client, msg: Message):
holo_user = HoloUser(msg.from_user) holo_user = HoloUser(msg.from_user)

View File

@ -1,4 +1,4 @@
from os import listdir, path, sep from os import listdir, makedirs, path, sep
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime, timedelta from datetime import datetime, timedelta
from app import app from app import app
@ -6,12 +6,28 @@ from pyrogram.types import BotCommand, BotCommandScopeChat
from pyrogram.errors import bad_request_400 from pyrogram.errors import bad_request_400
from pyrogram.enums.chat_members_filter import ChatMembersFilter from pyrogram.enums.chat_members_filter import ChatMembersFilter
from classes.holo_user import HoloUser from classes.holo_user import HoloUser
from modules.utils import configGet, locale, logWrite from modules.utils import configGet, jsonSave, locale, logWrite
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from modules.database import col_applications, col_sponsorships from modules.database import col_applications, col_sponsorships
scheduler = AsyncIOScheduler() scheduler = AsyncIOScheduler()
@scheduler.scheduled_job(trigger="interval", seconds=20)
async def cache_group_members():
list_of_users = []
async for member in app.get_chat_members(configGet("destination_group")):
list_of_users.append(member.user.id)
makedirs("cache", exist_ok=True)
jsonSave(list_of_users, f"cache{sep}group_members")
@scheduler.scheduled_job(trigger="interval", seconds=40)
async def cache_admins():
list_of_users = []
async for member in app.get_chat_members(configGet("admin_group")):
list_of_users.append(member.user.id)
makedirs("cache", exist_ok=True)
jsonSave(list_of_users, f"cache{sep}admins")
# Cache the avatars of group members # Cache the avatars of group members
if configGet("enabled", "scheduler", "cache_avatars"): if configGet("enabled", "scheduler", "cache_avatars"):
@scheduler.scheduled_job(trigger="date", run_date=datetime.now()+timedelta(seconds=10)) @scheduler.scheduled_job(trigger="date", run_date=datetime.now()+timedelta(seconds=10))