Archived
1
0
Fork 0
This commit is contained in:
多羅狼 2023-04-18 18:20:54 +08:00 committed by GitHub
parent df6df2e888
commit 0e3e1ba75c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 7 deletions

View file

@ -6,14 +6,14 @@ from core.component import module
from core.exceptions import NoReportException
exchange_rate = module('exchange_rate',
desc='汇率转换器。',
desc='{exchange_rate.help.desc}',
alias={'exchangerate': 'exchange_rate',
'excr': 'exchange_rate'},
developers=['DoroWolf'], required_superuser = True)
developers=['DoroWolf'])
api_key = 'd31697e581d5c35b038c625c'
@exchange_rate.command('<amount> <base> <target> {根据当日汇率转换货币价格。}')
@exchange_rate.command('<amount> <base> <target> {{exchange_rate.help}}')
async def _(msg: Bot.MessageSession):
base_currency = msg.parsed_msg['<base>'].upper()
target_currency = msg.parsed_msg['<target>'].upper()
@ -29,7 +29,7 @@ async def _(msg: Bot.MessageSession):
# if target_currency not in supported_currencies:
# unsupported_currencies.append(target_currency)
# if unsupported_currencies:
# await msg.finish(f"发生错误:无效的货币单位:{' '.join(unsupported_currencies)}")
# await msg.finish(f"{msg.locale.t('exchange_rate.message.error.invalid')}{' '.join(unsupported_currencies)}")
# exit()
# else:
# data = response.json()
@ -41,9 +41,9 @@ async def _(msg: Bot.MessageSession):
try:
amount = float(msg.parsed_msg['<amount>'])
if amount <= 0:
await msg.finish("发生错误:金额必须为正数。")
await msg.finish(msg.locale.t('exchange_rate.message.error.non_positive'))
except ValueError:
await msg.finish("发生错误:无效的金额。")
await msg.finish(msg.locale.t('exchange_rate.message.error.non_digital'))
url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}'
response = requests.get(url)
@ -52,7 +52,7 @@ async def _(msg: Bot.MessageSession):
data = response.json()
exchange_rate = data['conversion_result']
current_time = datetime.datetime.now().strftime("%Y-%m-%d")
await msg.finish(f'{amount} {base_currency} -> {exchange_rate} {target_currency}\n{current_time}')
await msg.finish(msg.locale.t('exchange_rate.message', amount=amount, base=base_currency, exchange_rate=exchange_rate, target=target_currency, time=current_time))
else:
data = response.json()
error_type = data['error-type']

View file

@ -0,0 +1,8 @@
{
"exchange_rate.help.desc": "汇率转换器。",
"exchange_rate.help": "根据当日汇率转换货币价格。",
"exchange_rate.message": "${amount} ${base} -> ${exchange_rate} ${target}\n{time}",
"exchange_rate.message.error.non_positive": "发生错误:金额必须为正数。",
"exchange_rate.message.error.non_digital": "发生错误:无效的金额,金额必须为纯数字。",
"exchange_rate.message.error.invalid": "发生错误:无效的货币单位:"
}