diff --git a/app.py b/app.py index 833c744..a30216e 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,7 @@ +from os import path, sep +from ujson import JSONDecodeError from modules.logging import logWrite -from modules.utils import configGet +from modules.utils import configGet, jsonLoad from pyrogram.client import Client 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): if (admin_id == configGet("owner")) or (admin_id in configGet("admins")): 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: async for member in app.get_chat_members(configGet("admin_group")): if member.user.id == admin_id: diff --git a/modules/commands/application.py b/modules/commands/application.py index 2f90f99..abc758d 100644 --- a/modules/commands/application.py +++ b/modules/commands/application.py @@ -12,7 +12,7 @@ from modules.database import col_applications from modules import custom_filters # 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): try: diff --git a/modules/commands/applications.py b/modules/commands/applications.py index f51a699..ba409e9 100644 --- a/modules/commands/applications.py +++ b/modules/commands/applications.py @@ -11,7 +11,7 @@ from modules.database import col_applications from modules import custom_filters # 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): logWrite(f"Admin {msg.from_user.id} requested export of a database") diff --git a/modules/commands/label.py b/modules/commands/label.py index 27636d0..60763b0 100644 --- a/modules/commands/label.py +++ b/modules/commands/label.py @@ -6,7 +6,7 @@ from modules.utils import locale, should_quote, find_user from classes.holo_user import HoloUser, LabelTooLongError 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): if len(msg.command) < 3: diff --git a/modules/commands/message.py b/modules/commands/message.py index 265395c..60ce874 100644 --- a/modules/commands/message.py +++ b/modules/commands/message.py @@ -7,7 +7,7 @@ from modules.utils import logWrite, locale, should_quote from modules import custom_filters # 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): try: diff --git a/modules/commands/nearby.py b/modules/commands/nearby.py index 97eb9cb..39d6ba8 100644 --- a/modules/commands/nearby.py +++ b/modules/commands/nearby.py @@ -10,7 +10,7 @@ from modules.database import col_applications from classes.errors.geo import PlaceNotFoundError # 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): holo_user = HoloUser(msg.from_user) diff --git a/modules/commands/reboot.py b/modules/commands/reboot.py index d929e1a..0a5faf9 100644 --- a/modules/commands/reboot.py +++ b/modules/commands/reboot.py @@ -11,7 +11,7 @@ from modules import custom_filters pid = getpid() # 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): logWrite(f"Shutting down bot with pid {pid}") diff --git a/modules/commands/sponsorship.py b/modules/commands/sponsorship.py index c64fe88..32e14dc 100644 --- a/modules/commands/sponsorship.py +++ b/modules/commands/sponsorship.py @@ -8,7 +8,7 @@ from modules.utils import locale, should_quote from modules.database import col_applications # 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): 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)) diff --git a/modules/commands/warn.py b/modules/commands/warn.py index 0002b6d..e974de6 100644 --- a/modules/commands/warn.py +++ b/modules/commands/warn.py @@ -8,7 +8,7 @@ from modules.database import col_warnings from modules import custom_filters # 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): if msg.chat.id == configGet("destination_group"): diff --git a/modules/commands/warnings.py b/modules/commands/warnings.py index 74d3e14..d2a9c7e 100644 --- a/modules/commands/warnings.py +++ b/modules/commands/warnings.py @@ -8,7 +8,7 @@ from modules.database import col_users, col_warnings from modules import custom_filters # 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): if len(msg.command) <= 1: diff --git a/modules/handlers/contact.py b/modules/handlers/contact.py index bd51f1e..b6459a5 100644 --- a/modules/handlers/contact.py +++ b/modules/handlers/contact.py @@ -10,7 +10,7 @@ from classes.holo_user import HoloUser from modules import custom_filters # 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): holo_user = HoloUser(msg.from_user) diff --git a/modules/scheduled.py b/modules/scheduled.py index 3da6b8d..601ee5a 100644 --- a/modules/scheduled.py +++ b/modules/scheduled.py @@ -1,4 +1,4 @@ -from os import listdir, path, sep +from os import listdir, makedirs, path, sep from apscheduler.schedulers.asyncio import AsyncIOScheduler from datetime import datetime, timedelta from app import app @@ -6,12 +6,28 @@ from pyrogram.types import BotCommand, BotCommandScopeChat from pyrogram.errors import bad_request_400 from pyrogram.enums.chat_members_filter import ChatMembersFilter 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 modules.database import col_applications, col_sponsorships 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 if configGet("enabled", "scheduler", "cache_avatars"): @scheduler.scheduled_job(trigger="date", run_date=datetime.now()+timedelta(seconds=10))