rate 支持简易计算 (#75)

This commit is contained in:
oahiewuoil 2020-11-22 10:15:14 +08:00 committed by GitHub
parent ee8e52fb39
commit c1791ad986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -92,10 +92,10 @@
}, },
{ {
"name": "rate", "name": "rate",
"version": "1.1", "version": "1.2",
"section": "daily", "section": "daily",
"maintainer": "fruitymelon", "maintainer": "fruitymelon",
"size": "1.7 kb", "size": "1.8 kb",
"supported": true, "supported": true,
"des-short": "汇率转换。", "des-short": "汇率转换。",
"des": "这个人很懒,什么都没有留下。" "des": "这个人很懒,什么都没有留下。"

20
rate.py
View File

@ -32,25 +32,29 @@ init()
@listener(is_plugin=True, outgoing=True, command="rate", @listener(is_plugin=True, outgoing=True, command="rate",
description="Currency exchange rate plugin.", description="货币汇率插件",
parameters="<FROM> <TO>") parameters="<FROM> <TO> <NB>")
async def rate(context): async def rate(context):
while not inited: while not inited:
await asyncio.sleep(1) await asyncio.sleep(1)
if not context.parameter: if not context.parameter:
await context.edit( await context.edit(
f"This is the currency exchange rate plugin.\n\nUsage: `-rate <FROM> <TO>`\n\nAvailable currencies: {', '.join(currencies)}") f"这是货币汇率插件\n\n使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n{', '.join(currencies)}")
return return
if len(context.parameter) != 2: if len(context.parameter) != 3:
await context.edit(f"Usage: `-rate <FROM> <TO>`\n\n`{', '.join(currencies)}`") await context.edit(f"使用方法: `-rate <FROM> <TO> <NB>`\n\n支持货币: \n{', '.join(currencies)}")
return return
FROM = context.parameter[0].upper().strip() FROM = context.parameter[0].upper().strip()
TO = context.parameter[1].upper().strip() TO = context.parameter[1].upper().strip()
try:
NB = float(context.parameter[2].strip())
except:
NB = 1.0
if currencies.count(FROM) == 0: if currencies.count(FROM) == 0:
await context.edit( await context.edit(
f"Currency type {FROM} is not supported. Choose one among `{', '.join(currencies)}` instead.") f"{FROM}不是支持的货币. \n\n支持货币: \n{', '.join(currencies)}")
return return
if currencies.count(TO) == 0: if currencies.count(TO) == 0:
await context.edit(f"Currency type {TO} is not supported. Choose one among `{', '.join(currencies)}` instead.") await context.edit(f"{TO}不是支持的货币. \n\n支持货币: \n{', '.join(currencies)}")
return return
await context.edit(f'{FROM} : {TO} = 1 : {int(10000*data["rates"][TO]/data["rates"][FROM])/10000}') await context.edit(f'{FROM} : {TO} = {NB} : {round(data["rates"][TO]/data["rates"][FROM]*NB,2)}')