From 122fced329c9dea6da04b53e84420763250eb522 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 7 Jan 2020 18:04:00 +0100 Subject: [PATCH] Added Currency Conversion --- api/inspirobot.py | 2 +- bot.py | 12 ++++--- cogs/fun/autoconvert.py | 75 +++++++++++++++++++++++++++++++++++++++++ util/checking.py | 8 +++++ 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 cogs/fun/autoconvert.py create mode 100644 util/checking.py diff --git a/api/inspirobot.py b/api/inspirobot.py index 8547e15..d47cd36 100644 --- a/api/inspirobot.py +++ b/api/inspirobot.py @@ -5,7 +5,7 @@ def get_inspirational_quote() -> str: response = requests.get("http://inspirobot.me/api?generate=true") if not response.ok: - logging.error(f"Steam API response not OK: {response.status_code} [http://inspirobot.me/api?generate=true]") + logging.error(f"Inspirobot API response not OK: {response.status_code} [http://inspirobot.me/api?generate=true]") return None return response.text diff --git a/bot.py b/bot.py index 59e57d9..b9117b2 100644 --- a/bot.py +++ b/bot.py @@ -6,7 +6,12 @@ from util import logging # Bot URL https://discordapp.com/api/oauth2/authorize?client_id=657709911337074698&permissions=314432&scope=bot -client = commands.Bot(command_prefix='.', case_insensitive=True) +config = open("config.json", "r") +json = json.load(config) +config.close() + +client = commands.Bot(command_prefix=json["prefix"], case_insensitive=True) + @client.command() @commands.is_owner() @@ -75,8 +80,7 @@ async def on_ready(): logging. info("Deep Blue finished loading.") -with open("config.json", "r") as key_file: - json = json.load(key_file) - key = json["client_key"] + +key = json["client_key"] client.run(key) \ No newline at end of file diff --git a/cogs/fun/autoconvert.py b/cogs/fun/autoconvert.py new file mode 100644 index 0000000..c10f276 --- /dev/null +++ b/cogs/fun/autoconvert.py @@ -0,0 +1,75 @@ +import discord +from discord.ext import commands +from util import logging, checking +import re + +class AutoConvert(commands.Cog): + + def __init__(self, client): + self.client = client + self.currencies = ['€', '$', '£'] + self.currency_conversion = { + "€$":1.1144, + "€£":0.85 + } + + ''' + Checks wether a message contains currency symbols + Returns an array of prices found + + €, $, £ + ''' + def contains_currency(self, message : discord.Message) -> []: + prices = [] + words = message.content.split(' ') + for word in words: + for currency in self.currencies: + if currency in word: + try: + prices.append([float(re.findall(r"[-+]?\d*\.\d+|\d+", word)[0]), currency]) + except: + pass + break + + return prices + + def convert_currency(self, _from, to, value): + if _from == '€': + conversion_rate = self.currency_conversion[_from + to] + elif to == '€': + conversion_rate = 1/self.currency_conversion[to + _from] + else: + conversion_rate = self.currency_conversion['€' + to] / self.currency_conversion['€' + _from] + + return round(value * conversion_rate, 2) + + @commands.Cog.listener() + async def on_message(self, message): + if message.author.bot: + return + + empty = True + + currencies = self.contains_currency(message) + embed = discord.Embed(title="Here are some useful conversions:", color=0x111387) + + if len(currencies) != 0: + empty = False + for element in currencies: + currency_string = "" + for currency in self.currencies: + if currency == element[1]: + continue + currency_string += f"{self.convert_currency(element[1], currency, element[0])}{currency}, " + embed.add_field(name=str(element[0])+element[1], value=currency_string[:-2]) + + + + if not empty: + await message.channel.send(embed=embed) + + + + +def setup(client): + client.add_cog(AutoConvert(client)) \ No newline at end of file diff --git a/util/checking.py b/util/checking.py new file mode 100644 index 0000000..8588a0b --- /dev/null +++ b/util/checking.py @@ -0,0 +1,8 @@ +import discord +from discord.ext import commands +from util import logging + +def is_author_bot(ctx : commands.Context): + if ctx.message.author.bot: + return False + return True \ No newline at end of file