-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.py
43 lines (36 loc) · 1.24 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
# https://github.com/juliankoh/ribbon-discord-bot
# https://github.com/melenxyz/abracadabra-tvl-bot
import discord
from discord.ext import tasks
import os
from dotenv import load_dotenv
import requests
import json
import aiohttp
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
REFRESH_TIMER = os.getenv('REFRESH_TIMER')
CONTRACT = os.getenv('CONTRACT')
NAME = os.getenv('NAME')
CHAIN = os.getenv('CHAIN')
CURRENCY = os.getenv('CURRENCY')
client = discord.Client()
async def get_price():
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.coingecko.com/api/v3/simple/token_price/{CHAIN}?contract_addresses={CONTRACT}&vs_currencies={CURRENCY}") as r:
if r.status == 200:
js = await r.json()
price = js[CONTRACT][CURRENCY]
pricestring = (f"{NAME}: ${price}")
return pricestring
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord! ')
for guild in client.guilds:
print("connected to ", guild.name)
refresh_price.start()
@tasks.loop(seconds=float(REFRESH_TIMER))
async def refresh_price():
for guild in client.guilds:
await guild.me.edit(nick=await get_price())
client.run(TOKEN)