mirror of
https://github.com/Redume/Shirino.git
synced 2025-05-19 10:15:26 +01:00
chore: made localization in the inline command to get the course of the command
This commit is contained in:
parent
5fe0a5ae3a
commit
01b8d01285
1 changed files with 60 additions and 46 deletions
|
@ -3,13 +3,15 @@ import hashlib
|
|||
from aiogram import types, Router
|
||||
from aiogram.filters import Command
|
||||
|
||||
from bot import bot
|
||||
from bot import bot, db
|
||||
from functions.convert import Converter
|
||||
from functions.create_chart import create_chart
|
||||
from utils.format_number import format_number
|
||||
from utils.inline_query import reply
|
||||
from i18n.localization import I18n
|
||||
|
||||
router = Router()
|
||||
i18n = I18n()
|
||||
|
||||
|
||||
@router.inline_query()
|
||||
|
@ -17,16 +19,28 @@ async def currency(query: types.InlineQuery) -> None:
|
|||
text = query.query.lower()
|
||||
args = text.split()
|
||||
result_id = hashlib.md5(text.encode()).hexdigest()
|
||||
|
||||
get_bot = await bot.get_me()
|
||||
|
||||
data = await db.fetch(
|
||||
'SELECT lang, chart, chart_period '
|
||||
'FROM users WHERE user_id = ?',
|
||||
query.from_user.id
|
||||
)
|
||||
|
||||
lang = data.get('lang')
|
||||
locale = i18n.get_locale(lang)
|
||||
|
||||
currency_example = locale["currency_example"].format(
|
||||
bot_username=get_bot.username
|
||||
)
|
||||
|
||||
if len(args) < 2:
|
||||
return await reply(result_id,
|
||||
[("2 or 3 arguments are required.",
|
||||
f'@{get_bot.username} USD RUB \n'
|
||||
f'@{get_bot.username} 12 USD RUB',
|
||||
None, None)],
|
||||
query)
|
||||
await reply(
|
||||
result_id,
|
||||
[(locale["error_not_enough_args"], currency_example, None, None)],
|
||||
query
|
||||
)
|
||||
return
|
||||
|
||||
conv = Converter()
|
||||
|
||||
|
@ -36,68 +50,68 @@ async def currency(query: types.InlineQuery) -> None:
|
|||
try:
|
||||
conv.amount = float(args[0].replace(',', '.'))
|
||||
if conv.amount < 0:
|
||||
return await reply(
|
||||
await reply(
|
||||
result_id,
|
||||
[
|
||||
("Negative amounts are not supported.", None, None)
|
||||
],
|
||||
[(locale["error_negative_amount"], None, None)],
|
||||
query
|
||||
)
|
||||
|
||||
return
|
||||
except ValueError:
|
||||
return await reply(
|
||||
await reply(
|
||||
result_id,
|
||||
[
|
||||
(
|
||||
"Please enter a valid number for the amount.",
|
||||
f'@{get_bot.username} USD RUB \n'
|
||||
f'@{get_bot.username} 12 USD RUB',
|
||||
None, None
|
||||
[(locale["error_invalid_number"], currency_example, None, None)],
|
||||
query
|
||||
)
|
||||
],
|
||||
query)
|
||||
|
||||
return
|
||||
from_currency = args[1]
|
||||
conv_currency = args[2]
|
||||
elif len(args) == 2:
|
||||
from_currency = args[0]
|
||||
conv_currency = args[1]
|
||||
else:
|
||||
return await reply(
|
||||
await reply(
|
||||
result_id,
|
||||
[
|
||||
(
|
||||
'The source and target currency could not be determined.',
|
||||
None, None
|
||||
)
|
||||
],
|
||||
query
|
||||
[(locale["error_unknown_currency"], None, None)],
|
||||
query,
|
||||
)
|
||||
return
|
||||
|
||||
conv.from_currency = from_currency.upper()
|
||||
conv.conv_currency = conv_currency.upper()
|
||||
|
||||
try:
|
||||
await conv.convert()
|
||||
except RuntimeError:
|
||||
return await reply(
|
||||
await reply(
|
||||
result_id,
|
||||
[
|
||||
(
|
||||
'The currency exchange rate could not be determined',
|
||||
None, None
|
||||
)
|
||||
],
|
||||
[(locale["error_currency_rate"], None, None)],
|
||||
query
|
||||
)
|
||||
return
|
||||
|
||||
chart = await create_chart(from_currency, conv_currency)
|
||||
chart = None
|
||||
|
||||
message = f'{format_number(conv.amount)} {conv.from_currency} ' \
|
||||
f'= {conv.conv_amount} {conv.conv_currency}'
|
||||
if bool(data.get('chart', 0)):
|
||||
chart = await create_chart(
|
||||
from_currency,
|
||||
conv_currency,
|
||||
data.get('chart_period', 'month')
|
||||
)
|
||||
|
||||
message = (
|
||||
f"{format_number(conv.amount)} {conv.from_currency} = "
|
||||
f"{conv.conv_amount} {conv.conv_currency}"
|
||||
)
|
||||
results = [(message, None, None)]
|
||||
|
||||
if chart:
|
||||
results.insert(0, (f'{message}\n[Chart]({chart})', None, chart))
|
||||
results.insert(
|
||||
0,
|
||||
(
|
||||
message,
|
||||
None,
|
||||
chart
|
||||
)
|
||||
)
|
||||
|
||||
await reply(result_id, results, query)
|
Loading…
Add table
Reference in a new issue