Skip to content

Commit

Permalink
feat(utils): using client.set_bot_commands
Browse files Browse the repository at this point in the history
  • Loading branch information
HitaloM committed Jul 23, 2024
1 parent 122c61c commit d2767c7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
22 changes: 21 additions & 1 deletion locales/bot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PyKorone 1.0\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-07-22 21:28-0300\n"
"POT-Creation-Date: 2024-07-23 19:03-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -940,3 +940,23 @@ msgstr ""
#: src/korone/modules/web/handlers/whois.py:45
msgid "An error occurred while fetching whois information: {error}"
msgstr ""

#: src/korone/utils/commands_list.py:28
msgid "Start the bot"
msgstr ""

#: src/korone/utils/commands_list.py:32
msgid "Show help message"
msgstr ""

#: src/korone/utils/commands_list.py:36
msgid "Show information about the bot"
msgstr ""

#: src/korone/utils/commands_list.py:40
msgid "Show privacy policy"
msgstr ""

#: src/korone/utils/commands_list.py:44
msgid "Change the bot language"
msgstr ""
8 changes: 5 additions & 3 deletions src/korone/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from hydrogram.errors import MessageIdInvalid, MessageNotModified
from hydrogram.raw.all import layer

from korone import __version__, cache, constants
from korone import __version__, cache, constants, i18n
from korone.database.sqlite import SQLite3Connection
from korone.modules import load_all_modules
from korone.utils.commands_list import set_ui_commands
from korone.utils.logging import logger

if TYPE_CHECKING:
Expand Down Expand Up @@ -55,13 +56,14 @@ def __init__(self, parameters: AppParameters):
async def start(self) -> None:
await super().start()

self.me = await self.get_me()

async with SQLite3Connection() as conn:
await conn.execute(constants.SQLITE3_TABLES, script=True)
await conn.vacuum()

await load_all_modules(self)
await set_ui_commands(self, i18n)

self.me = await self.get_me()

await logger.ainfo(
"Korone v%s running with Hydrogram v%s (Layer %s) started on @%s. Hi!",
Expand Down
59 changes: 59 additions & 0 deletions src/korone/utils/commands_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

import asyncio
from contextlib import suppress

from hydrogram import Client
from hydrogram.errors import FloodWait
from hydrogram.types import (
BotCommand,
BotCommandScopeAllGroupChats,
BotCommandScopeAllPrivateChats,
)

from korone.utils.i18n import I18nNew


async def set_ui_commands(client: Client, i18n: I18nNew):
_ = i18n.gettext

with suppress(FloodWait):
await client.delete_bot_commands()
tasks = []
for locale in i18n.available_locales:
commands = [
BotCommand(
command="start",
description=_("Start the bot", locale=locale),
),
BotCommand(
command="help",
description=_("Show help message", locale=locale),
),
BotCommand(
command="about",
description=_("Show information about the bot", locale=locale),
),
BotCommand(
command="privacy",
description=_("Show privacy policy", locale=locale),
),
BotCommand(
command="language",
description=_("Change the bot language", locale=locale),
),
]

language_code = locale.split("_")[0].lower() if "_" in locale else locale

tasks.extend([
client.set_bot_commands(
commands, scope=BotCommandScopeAllPrivateChats(), language_code=language_code
),
client.set_bot_commands(
commands, scope=BotCommandScopeAllGroupChats(), language_code=language_code
),
])

await asyncio.gather(*tasks)

0 comments on commit d2767c7

Please sign in to comment.