Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/exchange_rate/__init__.py
多羅狼 a2fee8bb02
fix
2023-04-18 12:44:54 +08:00

45 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
import requests
from core.builtins import Bot
from core.component import module
exchange_rate = module('exchange_rate', required_superuser = True)
api_key = 'd31697e581d5c35b038c625c'
@exchange_rate.command('<amount> <base> <target>')
async def _(msg: Bot.MessageSession):
base_currency = msg.parsed_msg['<base>'].upper()
target_currency = msg.parsed_msg['<target>'].upper()
url = f'https://v6.exchangerate-api.com/v6/{api_key}/codes'
response = requests.get(url)
if response.status_code == 200:
supported_currencies = data['supported_codes']
if base_currency not in supported_currencies:
await msg.finish("发生错误:无效的货币单位:" + base_currency)
elif target_currency not in supported_currencies:
await msg.finish("发生错误:无效的货币单位:" + target_currency)
else:
await msg.finish(f'Error')
amount = None
while amount is None:
try:
amount = float(msg.parsed_msg['<amount>'])
if amount <= 0:
await msg.finish("发生错误:金额必须为正数。")
except ValueError:
await msg.finish("发生错误:无效的金额。")
url = f'https://v6.exchangerate-api.com/v6/{api_key}/pair/{base_currency}/{target_currency}/{amount}'
response = requests.get(url)
if response.status_code == 200:
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}')
else:
await msg.finish(f'Error')