-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoNotify.py
58 lines (51 loc) · 2.36 KB
/
CryptoNotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from asyncio import tasks
import json
import discord
import requests
from datetime import datetime
from discord.ext import tasks
settings = json.load(open("settings.json"))
class Bot(discord.Client):
async def on_ready(self):
print("Connected")
self.currentWallet.start()
async def on_message(self, message):
if message.author != client.user and message.channel.id == settings["channelID"]:
await self.currentWallet()
return
@tasks.loop(hours=1)
async def currentWallet(self):
message = ""
kanal = self.get_channel(settings["channelID"])
total = 0
lastTotal = 0
message += "------------------------------------------------------------------\n"
for price in settings["LastPrice"]:
response = requests.get(f"https://api.coinbase.com/v2/prices/{price}-EUR/spot")
response = json.loads(response.text)
amount = settings["wallet"][price]
lastExchangerate = settings["LastPrice"][price]
currentExchangerate = float(response["data"]["amount"])
currentPrice = round(currentExchangerate*amount,2)
lastPrice = round(lastExchangerate*amount,2)
message += f"{self.getEmoji(currentPrice, lastPrice)} **{price}**: {lastPrice}€ ---> {currentPrice}€ | {lastExchangerate}€ ---> {currentExchangerate}€ | **{self.getPercent(lastPrice,currentPrice)}%**\n"
settings["LastPrice"][price] = float(response["data"]["amount"])
total+=currentPrice
lastTotal+=lastPrice
message += "------------------------------------------------------------------\n"
message += f"{self.getEmoji(total, lastTotal)} **Total**: {round(lastTotal,2)}€ ---> {round(total,2)}€ | **{self.getPercent(lastTotal,total)}%**\n"
json.dump(settings, open("settings.json", "w"))
messages = await kanal.history(limit=200).flatten()
await kanal.delete_messages(messages)
await kanal.send(message)
def getEmoji(self, currentPrice, lastPrice):
if lastPrice < currentPrice:
return "📈"
elif lastPrice > currentPrice:
return "📉"
else:
return "=="
def getPercent(self, lastprice, newprice):
return round((100/lastprice)*(newprice-lastprice),2)
client = Bot()
client.run(settings["token"])