mirror of
https://github.com/Redume/Shirino.git
synced 2025-05-19 18:25: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 import types, Router
|
||||||
from aiogram.filters import Command
|
from aiogram.filters import Command
|
||||||
|
|
||||||
from bot import bot
|
from bot import bot, db
|
||||||
from functions.convert import Converter
|
from functions.convert import Converter
|
||||||
from functions.create_chart import create_chart
|
from functions.create_chart import create_chart
|
||||||
from utils.format_number import format_number
|
from utils.format_number import format_number
|
||||||
from utils.inline_query import reply
|
from utils.inline_query import reply
|
||||||
|
from i18n.localization import I18n
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
i18n = I18n()
|
||||||
|
|
||||||
|
|
||||||
@router.inline_query()
|
@router.inline_query()
|
||||||
|
@ -17,16 +19,28 @@ async def currency(query: types.InlineQuery) -> None:
|
||||||
text = query.query.lower()
|
text = query.query.lower()
|
||||||
args = text.split()
|
args = text.split()
|
||||||
result_id = hashlib.md5(text.encode()).hexdigest()
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
||||||
|
|
||||||
get_bot = await bot.get_me()
|
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:
|
if len(args) < 2:
|
||||||
return await reply(result_id,
|
await reply(
|
||||||
[("2 or 3 arguments are required.",
|
result_id,
|
||||||
f'@{get_bot.username} USD RUB \n'
|
[(locale["error_not_enough_args"], currency_example, None, None)],
|
||||||
f'@{get_bot.username} 12 USD RUB',
|
query
|
||||||
None, None)],
|
)
|
||||||
query)
|
return
|
||||||
|
|
||||||
conv = Converter()
|
conv = Converter()
|
||||||
|
|
||||||
|
@ -36,68 +50,68 @@ async def currency(query: types.InlineQuery) -> None:
|
||||||
try:
|
try:
|
||||||
conv.amount = float(args[0].replace(',', '.'))
|
conv.amount = float(args[0].replace(',', '.'))
|
||||||
if conv.amount < 0:
|
if conv.amount < 0:
|
||||||
return await reply(
|
await reply(
|
||||||
result_id,
|
result_id,
|
||||||
[
|
[(locale["error_negative_amount"], None, None)],
|
||||||
("Negative amounts are not supported.", None, None)
|
|
||||||
],
|
|
||||||
query
|
query
|
||||||
)
|
)
|
||||||
|
return
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return await reply(
|
await reply(
|
||||||
result_id,
|
result_id,
|
||||||
[
|
[(locale["error_invalid_number"], currency_example, None, None)],
|
||||||
(
|
query
|
||||||
"Please enter a valid number for the amount.",
|
|
||||||
f'@{get_bot.username} USD RUB \n'
|
|
||||||
f'@{get_bot.username} 12 USD RUB',
|
|
||||||
None, None
|
|
||||||
)
|
)
|
||||||
],
|
return
|
||||||
query)
|
|
||||||
|
|
||||||
from_currency = args[1]
|
from_currency = args[1]
|
||||||
conv_currency = args[2]
|
conv_currency = args[2]
|
||||||
elif len(args) == 2:
|
elif len(args) == 2:
|
||||||
from_currency = args[0]
|
from_currency = args[0]
|
||||||
conv_currency = args[1]
|
conv_currency = args[1]
|
||||||
else:
|
else:
|
||||||
return await reply(
|
await reply(
|
||||||
result_id,
|
result_id,
|
||||||
[
|
[(locale["error_unknown_currency"], None, None)],
|
||||||
(
|
query,
|
||||||
'The source and target currency could not be determined.',
|
|
||||||
None, None
|
|
||||||
)
|
|
||||||
],
|
|
||||||
query
|
|
||||||
)
|
)
|
||||||
|
return
|
||||||
|
|
||||||
conv.from_currency = from_currency.upper()
|
conv.from_currency = from_currency.upper()
|
||||||
conv.conv_currency = conv_currency.upper()
|
conv.conv_currency = conv_currency.upper()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await conv.convert()
|
await conv.convert()
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
return await reply(
|
await reply(
|
||||||
result_id,
|
result_id,
|
||||||
[
|
[(locale["error_currency_rate"], None, None)],
|
||||||
(
|
|
||||||
'The currency exchange rate could not be determined',
|
|
||||||
None, None
|
|
||||||
)
|
|
||||||
],
|
|
||||||
query
|
query
|
||||||
)
|
)
|
||||||
|
return
|
||||||
|
|
||||||
chart = await create_chart(from_currency, conv_currency)
|
chart = None
|
||||||
|
|
||||||
message = f'{format_number(conv.amount)} {conv.from_currency} ' \
|
if bool(data.get('chart', 0)):
|
||||||
f'= {conv.conv_amount} {conv.conv_currency}'
|
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)]
|
results = [(message, None, None)]
|
||||||
|
|
||||||
if chart:
|
if chart:
|
||||||
results.insert(0, (f'{message}\n[Chart]({chart})', None, chart))
|
results.insert(
|
||||||
|
0,
|
||||||
|
(
|
||||||
|
message,
|
||||||
|
None,
|
||||||
|
chart
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
await reply(result_id, results, query)
|
await reply(result_id, results, query)
|
Loading…
Add table
Reference in a new issue