From a0538625e2b187533045c850281a125796db1cfa Mon Sep 17 00:00:00 2001 From: Renovate Date: Mon, 1 Jan 2024 17:06:07 +0200 Subject: [PATCH 1/4] Update dependency pytest-asyncio to v0.23.3 --- requirements/dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/dev.txt b/requirements/dev.txt index 8e5ea9c..6428cb2 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -3,7 +3,7 @@ build==1.0.3 isort==5.13.2 mypy==1.8.0 pylint==3.0.3 -pytest-asyncio==0.23.2 +pytest-asyncio==0.23.3 pytest-cov==4.1.0 pytest==7.4.3 tox==4.11.4 From 91cc03f9211778adcd8715523a31ccdfa1a6bf9d Mon Sep 17 00:00:00 2001 From: Renovate Date: Mon, 1 Jan 2024 18:09:34 +0200 Subject: [PATCH 2/4] Update dependency pytest to v7.4.4 --- requirements/dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/dev.txt b/requirements/dev.txt index 6428cb2..165d741 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -5,7 +5,7 @@ mypy==1.8.0 pylint==3.0.3 pytest-asyncio==0.23.3 pytest-cov==4.1.0 -pytest==7.4.3 +pytest==7.4.4 tox==4.11.4 types-aiofiles==23.2.0.0 types-ujson==5.9.0.0 \ No newline at end of file From 9e9c90fce1873ab7068d079543661136fe80ae2a Mon Sep 17 00:00:00 2001 From: profitroll Date: Wed, 3 Jan 2024 22:35:46 +0100 Subject: [PATCH 3/4] Pyrofork has replaced the vanilla Pyrogram (#51) --- requirements/pyrogram.txt | 2 +- src/libbot/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements/pyrogram.txt b/requirements/pyrogram.txt index a7d1e59..13119ee 100644 --- a/requirements/pyrogram.txt +++ b/requirements/pyrogram.txt @@ -1,2 +1,2 @@ apscheduler~=3.10.4 -pyrogram~=2.0.106 \ No newline at end of file +pyrofork~=2.3.16.post1 \ No newline at end of file diff --git a/src/libbot/__init__.py b/src/libbot/__init__.py index 4f9160f..3bd8739 100644 --- a/src/libbot/__init__.py +++ b/src/libbot/__init__.py @@ -1,4 +1,4 @@ -__version__ = "2.1.0" +__version__ = "3.0.0" __license__ = "GPL3" __author__ = "Profitroll" From c756c6b1dc1a9b38b9630ed1d5746306ef158af0 Mon Sep 17 00:00:00 2001 From: profitroll Date: Wed, 3 Jan 2024 22:37:04 +0100 Subject: [PATCH 4/4] Closes #33 --- src/libbot/pyrogram/classes/client.py | 82 +++++++++++++++++++++------ tests/data/locale/en.json | 12 ++++ tests/data/locale/uk.json | 12 ++++ 3 files changed, 89 insertions(+), 17 deletions(-) diff --git a/src/libbot/pyrogram/classes/client.py b/src/libbot/pyrogram/classes/client.py index c232280..90422f6 100644 --- a/src/libbot/pyrogram/classes/client.py +++ b/src/libbot/pyrogram/classes/client.py @@ -103,7 +103,7 @@ class PyroClient(Client): self.bot_locale: BotLocale = BotLocale( default_locale=self.config["locale"], - locales_root=(Path("locale") if locales_root is None else locales_root) + locales_root=(Path("locale") if locales_root is None else locales_root), ) self.default_locale: str = self.bot_locale.default self.locales: dict = self.bot_locale.locales @@ -129,6 +129,53 @@ class PyroClient(Client): getpid(), ) + # Register default bot's info + try: + await self.set_bot_info( + name=self._("name", "bot"), + about=self._("about", "bot"), + description=self._("description", "bot"), + lang_code="", + ) + logger.info( + "Bot's info for the default locale %s has been updated", + self.default_locale, + ) + except KeyError: + logger.warning( + "Default locale %s has incorrect keys or values in bot section", + self.default_locale, + ) + + # Register bot's info for each available locale + for locale_code in self.locales: + locale = self.locales[locale_code] + + if "metadata" not in locale or ("codes" not in locale["metadata"]): + logger.warning( + "Locale %s is missing metadata or metadata.codes key", locale_code + ) + continue + + for code in locale["metadata"]["codes"]: + try: + await self.set_bot_info( + name=locale["bot"]["name"], + about=locale["bot"]["about"], + description=locale["bot"]["description"], + lang_code=code, + ) + logger.info( + "Bot's info for the locale %s has been updated", + self.default_locale, + ) + except KeyError: + logger.warning( + "Locale %s has incorrect keys or values in bot section", + locale_code, + ) + + # Send a message to the bot's reports chat about the startup try: await self.send_message( chat_id=self.owner @@ -136,22 +183,23 @@ class PyroClient(Client): else self.config["reports"]["chat_id"], text=f"Bot started PID `{getpid()}`", ) - - if self.scheduler is None: - return - - if register_commands: - self.scheduler.add_job( - self.register_commands, - trigger="date", - run_date=datetime.now() + timedelta(seconds=5), - kwargs={"command_sets": await self.collect_commands()}, - ) - - self.scheduler.start() except BadRequest: logger.warning("Unable to send message to report chat.") + if self.scheduler is None: + return + + # Schedule the task to register all commands + if register_commands: + self.scheduler.add_job( + self.register_commands, + trigger="date", + run_date=datetime.now() + timedelta(seconds=5), + kwargs={"command_sets": await self.collect_commands()}, + ) + + self.scheduler.start() + async def stop(self, exit_completely: bool = True) -> None: try: await self.send_message( @@ -170,16 +218,16 @@ class PyroClient(Client): if exit_completely: try: exit() - except SystemExit as exp: + except SystemExit as exc: raise SystemExit( "Bot has been shut down, this is not an application error!" - ) from exp + ) from exc async def collect_commands(self) -> Union[List[CommandSet], None]: """Gather list of the bot's commands ### Returns: - * `List[CommandSet]`: List of the commands' sets + * `List[CommandSet]`: List of the commands' sets. """ command_sets = None diff --git a/tests/data/locale/en.json b/tests/data/locale/en.json index 1b44b7e..914a805 100644 --- a/tests/data/locale/en.json +++ b/tests/data/locale/en.json @@ -1,4 +1,16 @@ { + "metadata": { + "flag": "🇬🇧", + "name": "English", + "codes": [ + "en" + ] + }, + "bot": { + "name": "", + "about": "", + "description": "" + }, "foo": "bar", "messages": { "example": "okay" diff --git a/tests/data/locale/uk.json b/tests/data/locale/uk.json index 34ff1fa..444b17b 100644 --- a/tests/data/locale/uk.json +++ b/tests/data/locale/uk.json @@ -1,4 +1,16 @@ { + "metadata": { + "flag": "🇺🇦", + "name": "Українська", + "codes": [ + "uk" + ] + }, + "bot": { + "name": "", + "about": "", + "description": "" + }, "foo": "бар", "messages": { "example": "окей"