This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
113 lines (84 loc) · 2.98 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from asyncio import sleep as asleep
import discord
import requests
from dotenv import load_dotenv
current_dir = os.path.dirname(os.path.realpath(__file__))
if not os.path.isfile(os.path.join(current_dir, ".env")):
print(
"No .env file found, make sure to copy .env.example to .env and fill in the values"
)
exit(1)
load_dotenv()
guildID = int(os.getenv("DAODAO_TREASURY_GUILD_ID", 0))
memberID = int(os.getenv("DAODAO_TREASURY_MEMBER_ID", 0))
BOT_TOKEN = os.getenv("DAODAO_TREASURY_BOT_TOKEN", "")
DAODAO_NAME = os.getenv("DAODAO_NAME", "DAO")
intents = discord.Intents.default()
client = discord.Client(intents=intents)
REST_API = os.getenv("DAODAO_JUNOD_NODE", "https://api.juno.strange.love")
DAO = os.getenv("DAODAO_TREASURY_DAO", "")
headers = {"Content-Type": "application/json"}
def getDAOAssets() -> dict:
assets = {}
print("Getting assets")
a = requests.get(f"{REST_API}/cosmos/bank/v1beta1/balances/{DAO}", timeout=20)
if a.status_code == 200:
for i in a.json()["balances"]:
assets[i["denom"]] = i["amount"]
# get CW20 assets from DAODAOs indexer
b = requests.get(f"https://indexer.daodao.zone/juno-1/contract/{DAO}/daoCore/cw20Balances?", timeout=20)
if b.status_code == 200:
for i in b.json():
assets[i["addr"]] = i["balance"]
return assets
def getPrices() -> dict:
prices = {}
print("Getting prices")
a = requests.get(
"https://api.wynddao.com/assets/prices",
headers=headers,
timeout=20,
)
if a.status_code == 200:
for i in a.json():
prices[i["asset"]] = i["priceInUsd"]
return prices
def getDAOWorth() -> float:
prices = getPrices()
assets = getDAOAssets()
totalUSD = 0.0
for coin in assets:
if coin not in prices:
continue
totalUSD += (float(assets[coin]) / 10**6) * float(prices[coin])
return totalUSD
@client.event
async def on_ready():
print(f"You have logged in as {client}")
guild = client.get_guild(guildID)
member = guild.get_member(memberID)
last_time = 0
USD = getDAOWorth()
await member.edit(nick=f"{DAODAO_NAME}")
wait_seconds = 60
get_new_prices_seconds = 120
while True:
try:
if last_time >= get_new_prices_seconds:
last_time = 0
USD = getDAOWorth()
await client.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name=f"${USD:,.0f}",
)
)
print(f"Updated status, waiting {wait_seconds} seconds")
await asleep(wait_seconds)
last_time += wait_seconds
except:
continue
client.run(BOT_TOKEN)