Spoilers, major command system improvements #4

Merged
profitroll merged 27 commits from dev into master 2023-01-05 13:45:15 +02:00
Showing only changes of commit b437092fe7 - Show all commits

View File

@ -4,6 +4,7 @@ some scheduled tasks is the main idea of this module"""
from os import listdir, makedirs, 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 ujson import dumps
from app import app from app import app
from pyrogram.types import BotCommand, BotCommandScopeChat from pyrogram.types import BotCommand, BotCommandScopeChat
from pyrogram.errors import bad_request_400 from pyrogram.errors import bad_request_400
@ -109,6 +110,14 @@ async def commands_register():
"locales": {} "locales": {}
} }
commands_raw = {
"users": [],
"admins": [],
"group_users": [],
"group_admins": [],
"locales": {}
}
valid_locales = [] valid_locales = []
files_locales = listdir(f'{configGet("locale", "locations")}') files_locales = listdir(f'{configGet("locale", "locations")}')
@ -121,6 +130,13 @@ async def commands_register():
"group_users": [], "group_users": [],
"group_admins": [] "group_admins": []
} }
if configGet("debug") is True:
commands_raw["locales"][".".join(entry.split(".")[:-1])] = {
"users": [],
"admins": [],
"group_users": [],
"group_admins": []
}
config_modules = configGet("features") config_modules = configGet("features")
config_commands = configGet("commands") config_commands = configGet("commands")
@ -135,13 +151,26 @@ async def commands_register():
enabled = True enabled = True
if enabled is False: if enabled is False:
if configGet("debug") is True:
logWrite(f"Not registering {command} at all")
continue continue
for permission in config_commands[command]["permissions"]: for permission in config_commands[command]["permissions"]:
commands[permission].append(BotCommand(command, locale("commands")[command])) commands[permission].append(BotCommand(command, locale("commands")[command]))
if configGet("debug") is True:
commands_raw[permission].append({f"{command}": locale("commands")[command]})
logWrite(f"Registering {command} for {permission}")
for lc in valid_locales: for lc in valid_locales:
commands["locales"][lc][permission].append(BotCommand(command, locale("commands", locale=lc)[command])) commands["locales"][lc][permission].append(BotCommand(command, locale("commands", locale=lc)[command]))
if configGet("debug") is True:
commands_raw["locales"][lc][permission].append({f"{command}": locale("commands", locale=lc)[command]})
logWrite(f"Registering {command} for {permission} [{lc}]")
# Registering user commands # Registering user commands
await app.set_bot_commands(commands["users"]) await app.set_bot_commands(commands["users"])
@ -155,7 +184,7 @@ async def commands_register():
# Registering admin/owner commands # Registering admin/owner commands
for admin in configGet("admins")+[configGet("owner")]: for admin in configGet("admins")+[configGet("owner")]:
try: try:
await app.set_bot_commands(commands["admins"], scope=BotCommandScopeChat(chat_id=admin)) await app.set_bot_commands(commands["admins"]+commands["users"], scope=BotCommandScopeChat(chat_id=admin))
if admin == configGet("owner"): if admin == configGet("owner"):
logWrite(f"Registered admin commands for owner {configGet('owner')}") logWrite(f"Registered admin commands for owner {configGet('owner')}")
else: else:
@ -175,4 +204,8 @@ async def commands_register():
await app.set_bot_commands(commands["group_users"], scope=BotCommandScopeChat(chat_id=configGet("users", "groups"))) await app.set_bot_commands(commands["group_users"], scope=BotCommandScopeChat(chat_id=configGet("users", "groups")))
logWrite("Registered destination group commands") logWrite("Registered destination group commands")
except bad_request_400.ChannelInvalid: except bad_request_400.ChannelInvalid:
logWrite(f"Could not register commands for destination group. Bot is likely not in the group.") logWrite(f"Could not register commands for destination group. Bot is likely not in the group.")
if configGet("debug") is True:
logWrite(f"Complete commands registration:\n{dumps(commands_raw, indent=4, ensure_ascii=False, encode_html_chars=False)}")