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
多羅狼 dd44f7e1ae
🤔
2023-04-18 09:56:31 +08:00

33 lines
1.2 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()
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 %H:%M:%S")
await msg.finish(f'{amount} {base_currency} -> {exchange_rate} {target_currency}\n{current_time}')
else:
await msg.finish(f'Error')