Added self.config

This commit is contained in:
Profitroll 2023-06-21 13:44:26 +02:00
parent f8b21093a4
commit 0cfe55d3c9
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2

View File

@ -1,11 +1,11 @@
import logging import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from os import getpid from os import getpid
from pathlib import Path
from time import time from time import time
from typing import List, Union from typing import List, Union
import pyrogram import pyrogram
from libbot import config_get
from libbot.i18n import BotLocale from libbot.i18n import BotLocale
from libbot.i18n.sync import _ from libbot.i18n.sync import _
from pyrogram.client import Client from pyrogram.client import Client
@ -34,26 +34,26 @@ logger = logging.getLogger(__name__)
class PyroClient(Client): class PyroClient(Client):
def __init__(self): def __init__(self):
with open("config.json", "r", encoding="utf-8") as f: with open("config.json", "r", encoding="utf-8") as f:
config = loads(f.read()) self.config: dict = loads(f.read())
super().__init__( super().__init__(
name="bot_client", name="bot_client",
api_id=config["bot"]["api_id"], api_id=self.config["bot"]["api_id"],
api_hash=config["bot"]["api_hash"], api_hash=self.config["bot"]["api_hash"],
bot_token=config["bot"]["bot_token"], bot_token=self.config["bot"]["bot_token"],
# Workers should be commented when using convopyro, otherwise # Workers should be commented when using convopyro, otherwise
# handlers land in another event loop and you won't see them # handlers land in another event loop and you won't see them
workers=config["bot"]["workers"], workers=self.config["bot"]["workers"],
plugins=dict(root="plugins", exclude=config["disabled_plugins"]), plugins=dict(root="plugins", exclude=self.config["disabled_plugins"]),
sleep_threshold=120, sleep_threshold=120,
) )
self.owner = config["bot"]["owner"] self.owner: int = self.config["bot"]["owner"]
self.commands: List[PyroCommand] = [] self.commands: List[PyroCommand] = []
self.scoped_commands = config["bot"]["scoped_commands"] self.scoped_commands: bool = self.config["bot"]["scoped_commands"]
self.start_time = 0 self.start_time: float = 0
self.bot_locale = BotLocale(config["locations"]["locale"]) self.bot_locale: BotLocale = BotLocale(Path(self.config["locations"]["locale"]))
self.default_locale = self.bot_locale.default self.default_locale: str = self.bot_locale.default
self.locales = self.bot_locale.locales self.locales: dict = self.bot_locale.locales
self._ = self.bot_locale._ self._ = self.bot_locale._
self.in_all_locales = self.bot_locale.in_all_locales self.in_all_locales = self.bot_locale.in_all_locales
@ -74,7 +74,7 @@ class PyroClient(Client):
try: try:
await self.send_message( await self.send_message(
chat_id=await config_get("chat_id", "reports"), chat_id=self.config["reports"]["chat_id"],
text=f"Bot started PID `{getpid()}`", text=f"Bot started PID `{getpid()}`",
) )
@ -92,7 +92,7 @@ class PyroClient(Client):
async def stop(self): async def stop(self):
try: try:
await self.send_message( await self.send_message(
chat_id=await config_get("chat_id", "reports"), chat_id=self.config["reports"]["chat_id"],
text=f"Bot stopped with PID `{getpid()}`", text=f"Bot stopped with PID `{getpid()}`",
) )
except BadRequest: except BadRequest:
@ -108,14 +108,14 @@ class PyroClient(Client):
""" """
command_sets = None command_sets = None
# If config get bot.scoped_commands is true - more complicated # If config's bot.scoped_commands is true - more complicated
# scopes system will be used instead of simple global commands # scopes system will be used instead of simple global commands
if self.scoped_commands: if self.scoped_commands:
scopes = {} scopes = {}
command_sets = [] command_sets = []
# Iterate through all commands in config # Iterate through all commands in config
for command, contents in (await config_get("commands")).items(): for command, contents in self.config["commands"].items():
# Iterate through all scopes of a command # Iterate through all scopes of a command
for scope in contents["scopes"]: for scope in contents["scopes"]:
if dumps(scope) not in scopes: if dumps(scope) not in scopes: