💩 rate 顺序错误

This commit is contained in:
xtaodada 2020-08-14 19:58:26 +08:00
parent fca2ec442c
commit a0c6ae52bc
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
2 changed files with 36 additions and 34 deletions

View File

@ -92,7 +92,7 @@
}, },
{ {
"name": "rate", "name": "rate",
"version": "1.0", "version": "1.1",
"section": "daily", "section": "daily",
"maintainer": "fruitymelon", "maintainer": "fruitymelon",
"size": "1.7 kb", "size": "1.7 kb",

68
rate.py
View File

@ -3,8 +3,6 @@
import asyncio, json import asyncio, json
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
import urllib.request import urllib.request
from pagermaid import bot, log
from pagermaid.listener import listener from pagermaid.listener import listener
API = "https://api.exchangeratesapi.io/latest" API = "https://api.exchangeratesapi.io/latest"
@ -13,42 +11,46 @@ data = {}
inited = False inited = False
def init(): def init():
with urllib.request.urlopen(API) as response: with urllib.request.urlopen(API) as response:
result = response.read() result = response.read()
try: try:
global data global data
data = json.loads(result) data = json.loads(result)
data["rates"][data["base"]] = 1.0 data["rates"][data["base"]] = 1.0
for key in list(enumerate(data["rates"])): for key in list(enumerate(data["rates"])):
currencies.append(key[1]) currencies.append(key[1])
currencies.sort() currencies.sort()
except JSONDecodeError as e: except JSONDecodeError as e:
raise e raise e
global inited global inited
inited = True inited = True
init() init()
@listener(is_plugin=True, outgoing=True, command="rate", @listener(is_plugin=True, outgoing=True, command="rate",
description="Currency exchange rate plugin.", description="Currency exchange rate plugin.",
parameters="<FROM> <TO>") parameters="<FROM> <TO>")
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(f"This is the currency exchange rate plugin.\n\nUsage: `-rate <FROM> <TO>`\n\nAvailable currencies: {', '.join(currencies)}") await context.edit(
return f"This is the currency exchange rate plugin.\n\nUsage: `-rate <FROM> <TO>`\n\nAvailable currencies: {', '.join(currencies)}")
if len(context.parameter) != 2: return
await context.edit(f"Usage: `-rate <FROM> <TO>`\n\n`{', '.join(currencies)}`") if len(context.parameter) != 2:
return await context.edit(f"Usage: `-rate <FROM> <TO>`\n\n`{', '.join(currencies)}`")
FROM = context.parameter[0].upper().strip() return
TO = context.parameter[1].upper().strip() FROM = context.parameter[0].upper().strip()
if currencies.count(FROM) == 0: TO = context.parameter[1].upper().strip()
await context.edit(f"Currency type {FROM} is not supported. Choose one among `{', '.join(currencies)}` instead.") if currencies.count(FROM) == 0:
return await context.edit(
if currencies.count(TO) == 0: f"Currency type {FROM} is not supported. Choose one among `{', '.join(currencies)}` instead.")
await context.edit(f"Currency type {TO} is not supported. Choose one among `{', '.join(currencies)}` instead.") return
return if currencies.count(TO) == 0:
await context.edit(f'{FROM} : {TO} = 1 : {int(10000*data["rates"][FROM]/data["rates"][TO])/10000}') await context.edit(f"Currency type {TO} is not supported. Choose one among `{', '.join(currencies)}` instead.")
return
await context.edit(f'{FROM} : {TO} = 1 : {int(10000*data["rates"][TO]/data["rates"][FROM])/10000}')