Added Currency Conversion
This commit is contained in:
parent
2f16876177
commit
122fced329
|
@ -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
|
||||
|
|
12
bot.py
12
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)
|
75
cogs/fun/autoconvert.py
Normal file
75
cogs/fun/autoconvert.py
Normal file
|
@ -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))
|
8
util/checking.py
Normal file
8
util/checking.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue