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/convert/__init__.py

25 lines
864 B
Python
Raw Normal View History

2023-01-28 05:53:11 +00:00
import os
from decimal import Decimal
2023-05-27 15:12:29 +00:00
from pint import UnitRegistry
2023-01-28 05:53:11 +00:00
2023-02-05 14:33:33 +00:00
from core.builtins import Bot
2023-03-04 08:51:56 +00:00
from core.component import module
2023-01-16 08:58:41 +00:00
2023-05-12 10:39:33 +00:00
# ureg = UnitRegistry(os.path.dirname(os.path.abspath(__file__)) +
# '/default_bi_zh-cn_en.txt', non_int_type=Decimal)
ureg = UnitRegistry(non_int_type=Decimal)
2023-05-13 02:42:45 +00:00
i = module('convert', alias=('conv', 'unit'), desc='{convert.help.desc}',
2023-05-27 15:10:37 +00:00
developers=['Dianliang233'], support_languages=['en_us'])
2023-01-16 08:58:41 +00:00
2023-05-13 02:42:45 +00:00
@i.command('<from_val> <to_unit> {convert.help}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2023-01-16 08:58:41 +00:00
from_val = msg.parsed_msg['<from_val>']
to_unit = msg.parsed_msg['<to_unit>']
2023-05-27 14:55:22 +00:00
try:
ori = ureg.parse_expression(from_val)
res = ureg.parse_expression(from_val).to(to_unit)
await msg.finish(f"{ori:~Pg} = {res:~Pg}")
2023-05-27 15:20:49 +00:00
except:
return msg.locale.t("convert.message.error")