-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
78 lines (61 loc) · 1.99 KB
/
bot.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
import discord
from discord.ext import commands
import requests
import asyncio
#discord API key
API_KEY = ""
#time to cache the price before requesting again
CACHE_PRICE_TIME = 300
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
cached_response = None
@bot.command()
async def stats(ctx):
await ctx.send("We will output stats here!!!")
@bot.command()
async def supply(ctx):
api_url = "https://explorer.rethereum.org/api?module=stats&action=coinsupply"
try:
response = requests.get(api_url).json()
await ctx.send(response)
except Exception as e:
error_message = f"Sorry we're unable to do that at this time."
await ctx.send(error_message)
@bot.command()
async def blocknum(ctx):
url = "https://explorer.rethereum.org/api?module=block&action=eth_block_number"
try:
response = requests.get(url).json()
blocks = int(response.get("result"), 16)
await ctx.send(blocks)
except Exception as e:
error_message = f"Sorry we're unable to do that at this time."
await ctx.send(error_message)
@bot.command()
async def price(ctx):
global cached_response
if cached_response:
await ctx.send(cached_response)
else:
await ctx.send("Response not available.")
async def request_and_cache():
global cached_response
url = "https://safe.trade/api/v2/peatio/public/markets/rthusdt/tickers"
try:
headers = {
'User-Agent': 'RTH_DISCORD_BOT',
}
response = requests.get(url, headers=headers).json()
cached_response = response['ticker']['last']
print("Updating price cache - " + cached_response)
await asyncio.sleep(CACHE_PRICE_TIME)
except Exception as e:
print(str(e))
await asyncio.sleep(300)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
while True:
await request_and_cache()
bot.run(API_KEY)