Closes #33
This commit is contained in:
parent
9e9c90fce1
commit
c756c6b1dc
@ -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
|
||||
|
||||
|
@ -1,4 +1,16 @@
|
||||
{
|
||||
"metadata": {
|
||||
"flag": "🇬🇧",
|
||||
"name": "English",
|
||||
"codes": [
|
||||
"en"
|
||||
]
|
||||
},
|
||||
"bot": {
|
||||
"name": "",
|
||||
"about": "",
|
||||
"description": ""
|
||||
},
|
||||
"foo": "bar",
|
||||
"messages": {
|
||||
"example": "okay"
|
||||
|
@ -1,4 +1,16 @@
|
||||
{
|
||||
"metadata": {
|
||||
"flag": "🇺🇦",
|
||||
"name": "Українська",
|
||||
"codes": [
|
||||
"uk"
|
||||
]
|
||||
},
|
||||
"bot": {
|
||||
"name": "",
|
||||
"about": "",
|
||||
"description": ""
|
||||
},
|
||||
"foo": "бар",
|
||||
"messages": {
|
||||
"example": "окей"
|
||||
|
Loading…
Reference in New Issue
Block a user