From 591086c3c87c945ccf5ecc0c4e797905bf5ba1b5 Mon Sep 17 00:00:00 2001 From: Martin Grant Date: Tue, 4 Dec 2018 16:53:55 +0000 Subject: [PATCH] Started a conversion command. --- config.json | 8 ++-- main.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 +- 3 files changed, 113 insertions(+), 5 deletions(-) diff --git a/config.json b/config.json index 8060027..b610570 100644 --- a/config.json +++ b/config.json @@ -1,13 +1,13 @@ { "bot": { "channels": { - "role-assignment": "491196614346801162", + "introductions": "413835267557031937", + "bjarne": "413882124249071618", "rules": "405741154643214356", + "announcements": "405451914973806602", "lobby": "412327350366240768", - "bjarne": "413882124249071618", "committee": "416255534438547456", - "introductions": "413835267557031937", - "announcements": "405451914973806602" + "role-assignment": "491196614346801162" } } } \ No newline at end of file diff --git a/main.py b/main.py index 52c8c3d..1684b69 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,7 @@ #import wikipedia from translate import Translator from coinmarketcap import Market +from forex_python.converter import CurrencyRates import utilities as utils import modules.roles @@ -912,6 +913,112 @@ async def crypto(ctx, *symbol): await BOT.say(output) + +@BOT.command(pass_context=True) +async def convert(ctx, value: float, fromUnit, toUnit): + """Convert between quantities. + Metre to Imperial: feet, mile, yard, inch. + Temperatures: c, f, k. + Time: secs, mins, hours, days. + Currency: Forex symbols e.g USD, GBP etc. + """ + + message = "Conversion not yet supported. Use !help convert to see supported conversions." + result = value + valid = False + + # Metre to Imperial Lengths + metreToImperial = { + "feet": 3.281, + "mile": 1609.344, + "yard": 1.094, + "inch": 39.37 + } + + if fromUnit == "m" and toUnit in metreToImperial: + result = value * metreToImperial[toUnit] + + if fromUnit in metreToImperial and toUnit == "m": + result = value / metreToImperial[fromUnit] + + # Temperatures + if fromUnit == "c": + if toUnit == "k": + result = value + 273.15 + if toUnit == "f": + result = (value * 9/5) + 32 + if fromUnit == "k": + if toUnit == "c": + result = value - 273.15 + if toUnit == "f": + result = (value - 273.15) * (9/5) + 32 + if fromUnit == "f": + if toUnit == "c": + result = (value - 32) * 5/9 + if toUnit == "k": + result = (value + -32) * (5/9) + 273.15 + + # Time + if fromUnit == "secs": + if toUnit == "mins": + result = value / 60 + if toUnit == "hours": + result = value / 3600 + if toUnit == "days": + result = value / 86400 + + if fromUnit == "mins": + if toUnit == "secs": + result = value * 60 + if toUnit == "hours": + result = value / 60 + if toUnit == "days": + result = value / 1440 + + if fromUnit == "hours": + if toUnit == "secs": + result = value * 3600 + if toUnit == "mins": + result = value * 60 + if toUnit == "days": + result = value / 24 + + if fromUnit == "days": + if toUnit == "secs": + result = value * 86400 + if toUnit == "mins": + result = value * 1440 + if toUnit == "hours": + result = value * 24 + + # Currency + currency = False + c = CurrencyRates() + try: + currency = True + result = c.convert(fromUnit.upper(), toUnit.upper(), value) + message = "Conversion: " + str(value) + message += " " + message += fromUnit + message += " equals " + message += str(result) + message += " " + message += toUnit + except: + currency = False + pass + + if result != value and currency == False: + message = "Conversion: " + str(value) + message += " " + message += fromUnit + message += " equals " + message += str(result) + message += " " + message += toUnit + + await BOT.say(message) + ##### [ BOT LOGIN ] ##### BOT.run(BOT_TOKEN) diff --git a/requirements.txt b/requirements.txt index a1f8d75..571d010 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ weather-api requests wikipedia translate -coinmarketcap \ No newline at end of file +coinmarketcap +forex-python \ No newline at end of file