Skip to content

Commit ea08a23

Browse files
committed
Initial commit
1 parent 62868ac commit ea08a23

File tree

2 files changed

+177
-2
lines changed

2 files changed

+177
-2
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
# python3-network-discord-bot
2-
CryptoNote network stats discord bot that displays a variety of network information and statistics on demand
1+
# Python 3 cryptonote network stats discord bot
2+
3+
Cryptonote network stats discord bot that displays a variety of network information and statistics on demand
4+
5+
6+
## DEPENDENCIES
7+
8+
``
9+
apt-get install python3
10+
``
11+
12+
``
13+
apt-get -y install python3-pip
14+
``
15+
16+
``
17+
python3 -m pip install discord.py==0.16.12
18+
``
19+
20+
## HOW TO USE
21+
22+
- Go to https://discordapp.com/developers/applications/
23+
- Create an application
24+
- Go to 'Bot' section of discordapp.com/developers portal from the menu and add new bot
25+
- Get a bot token and add it to TOKEN value inside networkbot.py file
26+
- Go to 'Auth' section of discordapp.com/developers portal and mark the checkbox 'bot' to generate an invite link for your new bot.
27+
- Visit the generated link to invite your bot to your Discord server.
28+
29+
## HOW TO RUN
30+
31+
``
32+
python3 networkbot.py
33+
``
34+

networkbot.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Python 3 cryptonote network stats discord bot
2+
# Cryptonote network stats discord bot that displays a variety of network information and statistics on demand
3+
# Version 1.0
4+
#
5+
# DEPENDENCIES
6+
# sudo apt-get install python3
7+
# sudo apt-get -y install python3-pip
8+
# python3 -m pip install discord.py==0.16.12
9+
#
10+
# HOW TO USE
11+
# Go to https://discordapp.com/developers/applications/
12+
# Create an application
13+
# Go to 'Bot' section of discordapp.com/developers portal from the menu and add new bot
14+
# Get a bot token and add it to TOKEN value inside networkbot.py file
15+
# Go to 'Auth' section of discordapp.com/developers portal and mark the checkbox 'bot' to generate an invite link for your new bot.
16+
# Visit that generated link to invite your bot to your Discord server.
17+
#
18+
# RUN THE BOT (run from command line)
19+
# python3 networkbot.py
20+
21+
import random
22+
import asyncio
23+
import aiohttp
24+
import json
25+
from discord.ext.commands import Bot
26+
27+
# bot description
28+
# bot description displayed in help section
29+
description = '''Network bot displays a variety of information and statistics on almost any cryptonote network. To use the commands type them with the prefix of ampersand (&). You can find the commands and their use below. Add (&) in front of a command (EXAMPLE: &height)'''
30+
31+
# bot prefix (&)
32+
# prefix used to call your bot's commands
33+
BOT_PREFIX = ("&")
34+
35+
# secret bot token
36+
# never give your secret bot token to anyone
37+
# get it at discordapp.com/developers/applications/me
38+
TOKEN = "YOUR_SUPER_SECRET_BOT_TOKEN_GOES_HERE"
39+
40+
# daemon address
41+
# address used to communicate with the network (127.0.0.1 for localhost)
42+
HOST = "YOUR_DAEMON_ADDRESS_GOES_HERE"
43+
44+
# daemon RPC port
45+
# port used to communicate with the network (your network's RPC port)
46+
PORT = "YOUR_RPC_PORT_GOES_HERE"
47+
48+
# start a bot
49+
client = Bot(command_prefix=BOT_PREFIX, description=description)
50+
51+
# commmand: &height
52+
# network top block height
53+
@client.command(description="Network top block height.", brief="Blockchain height.")
54+
async def height():
55+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getheight'
56+
async with aiohttp.ClientSession() as session: # Async HTTP request
57+
raw_response = await session.get(url)
58+
response = await raw_response.text()
59+
response = json.loads(response)
60+
await client.say("🌐 **Network height:** " + str(response['height']))
61+
62+
# commmand: &hash
63+
# appx. network hash rate
64+
@client.command(description="Appx. network hash rate.", brief="Network hash rate.")
65+
async def hash():
66+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo'
67+
async with aiohttp.ClientSession() as session: # Async HTTP request
68+
raw_response = await session.get(url)
69+
response = await raw_response.text()
70+
response = json.loads(response)
71+
await client.say("🌐 **Network hash rate:** " + str(response['hashrate']) + " H/s")
72+
73+
# commmand: &diff
74+
# current network difficulty
75+
@client.command(description="Current network difficulty.", brief="Network difficulty.")
76+
async def diff():
77+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo'
78+
async with aiohttp.ClientSession() as session: # Async HTTP request
79+
raw_response = await session.get(url)
80+
response = await raw_response.text()
81+
response = json.loads(response)
82+
await client.say("🌐 **Network difficulty:** " + str(response['difficulty']))
83+
84+
# commmand: &tx
85+
# total network transactions
86+
@client.command(description="Total network transactions.", brief="Network transactions.")
87+
async def tx():
88+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo'
89+
async with aiohttp.ClientSession() as session: # Async HTTP request
90+
raw_response = await session.get(url)
91+
response = await raw_response.text()
92+
response = json.loads(response)
93+
await client.say("🌐 **Network transactions:** " + str(response['tx_count']))
94+
95+
# commmand: &txpool
96+
# current transactions pool size
97+
@client.command(description="Current transactions pool size.", brief="TX pool size.")
98+
async def txpool():
99+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo'
100+
async with aiohttp.ClientSession() as session: # Async HTTP request
101+
raw_response = await session.get(url)
102+
response = await raw_response.text()
103+
response = json.loads(response)
104+
await client.say("🌐 **Transactions pool:** " + str(response['tx_pool_size']))
105+
106+
# commmand: &ver
107+
# current daemon version
108+
@client.command(description="Current daemon version.", brief="Daemon version.")
109+
async def ver():
110+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo'
111+
async with aiohttp.ClientSession() as session: # Async HTTP request
112+
raw_response = await session.get(url)
113+
response = await raw_response.text()
114+
response = json.loads(response)
115+
await client.say("🌐 **Daemon Version:** " + str(response['version']))
116+
117+
# commmand: &stats
118+
# key network stats all in one place
119+
@client.command(description="Key network stats all in one place.", brief="Network stats.")
120+
async def stats():
121+
url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo'
122+
async with aiohttp.ClientSession() as session: # Async HTTP request
123+
raw_response = await session.get(url)
124+
response = await raw_response.text()
125+
response = json.loads(response)
126+
await client.say("🌐 NETWORK STATS\n**Height:** " + str(response['height']) + " \n**Hash rate:** " + str(response['hashrate']) + " H/s \n**Difficulty:** " + str(response['difficulty']) + " \n**TX total:** " + str(response['tx_count']) + " \n**TX in the pool:** " + str(response['tx_pool_size']) + " \n**Daemon version:** " + str(response['version'])
127+
)
128+
129+
130+
# print list of servers where this bot is active to console
131+
async def list_servers():
132+
await client.wait_until_ready()
133+
while not client.is_closed:
134+
# you can customize the output message(s) below
135+
print("--- BOT ONLINE ---")
136+
for server in client.servers:
137+
# you can customize the output message(s) below
138+
print('Active servers: ' + str(server.name))
139+
await asyncio.sleep(600)
140+
141+
142+
client.loop.create_task(list_servers())
143+
client.run(TOKEN)

0 commit comments

Comments
 (0)