From b3f1e83cffa871c4aaf53f8e4fcd191d2f97577b Mon Sep 17 00:00:00 2001 From: Mika Lindqvist Date: Tue, 29 Sep 2020 05:31:30 +0200 Subject: [PATCH 1/3] Update to new Discord API. --- README.md | 8 ++------ networkbot.py | 33 +++++++++++++++++---------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8e11852..2022df3 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,11 @@ Cryptonote network stats discord bot that displays a variety of network informat ## DEPENDENCIES `` -apt-get install python3 +apt-get -y install python3 python3-pip `` `` -apt-get -y install python3-pip -`` - -`` -python3 -m pip install discord.py==0.16.12 +python3 -m pip install discord.py==1.3.4 `` ## HOW TO USE diff --git a/networkbot.py b/networkbot.py index f0abfb8..bb4c995 100644 --- a/networkbot.py +++ b/networkbot.py @@ -5,7 +5,7 @@ # DEPENDENCIES # sudo apt-get install python3 # sudo apt-get -y install python3-pip -# python3 -m pip install discord.py==0.16.12 +# python3 -m pip install discord.py==1.3.4 # # HOW TO USE # Go to https://discordapp.com/developers/applications/ @@ -22,9 +22,10 @@ import asyncio import aiohttp import json +from discord.ext import commands from discord.ext.commands import Bot -# bot description +# bot description # bot description displayed in help section 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)''' @@ -51,86 +52,86 @@ # commmand: &height # network top block height @client.command(description="Network top block height.", brief="Blockchain height.") -async def height(): +async def height(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getheight' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await client.say("🌐 **Network height:** " + str(response['height'])) + await context.send("🌐 **Network height:** " + str(response['height'])) # commmand: &hash # appx. network hash rate @client.command(description="Appx. network hash rate.", brief="Network hash rate.") -async def hash(): +async def hash(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await client.say("🌐 **Network hash rate:** " + str(response['hashrate']) + " H/s") + await context.send("🌐 **Network hash rate:** " + str(response['hashrate']) + " H/s") # commmand: &diff # current network difficulty @client.command(description="Current network difficulty.", brief="Network difficulty.") -async def diff(): +async def diff(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await client.say("🌐 **Network difficulty:** " + str(response['difficulty'])) + await context.send("🌐 **Network difficulty:** " + str(response['difficulty'])) # commmand: &tx # total network transactions @client.command(description="Total network transactions.", brief="Network transactions.") -async def tx(): +async def tx(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await client.say("🌐 **Network transactions:** " + str(response['tx_count'])) + await context.send("🌐 **Network transactions:** " + str(response['tx_count'])) # commmand: &txpool # current transactions pool size @client.command(description="Current transactions pool size.", brief="TX pool size.") -async def txpool(): +async def txpool(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await client.say("🌐 **Transactions pool:** " + str(response['tx_pool_size'])) + await context.send("🌐 **Transactions pool:** " + str(response['tx_pool_size'])) # commmand: &ver # current daemon version @client.command(description="Current daemon version.", brief="Daemon version.") -async def ver(): +async def ver(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await client.say("🌐 **Daemon Version:** " + str(response['version'])) + await context.send("🌐 **Daemon Version:** " + str(response['version'])) # commmand: &stats # key network stats all in one place @client.command(description="Key network stats all in one place.", brief="Network stats.") -async def stats(): +async def stats(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request raw_response = await session.get(url) response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - 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']) + await context.send("🌐 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']) ) From d27a2467115288a7519b0e39d590a140e6cee943 Mon Sep 17 00:00:00 2001 From: Mika Lindqvist Date: Fri, 2 Sep 2022 18:16:21 +0300 Subject: [PATCH 2/3] Update for discord.py 2.0.* --- README.md | 2 +- networkbot.py | 52 +++++++++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 2022df3..3f45bec 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ apt-get -y install python3 python3-pip `` `` -python3 -m pip install discord.py==1.3.4 +python3 -m pip install discord.py==2.0.1 `` ## HOW TO USE diff --git a/networkbot.py b/networkbot.py index bb4c995..9b6015a 100644 --- a/networkbot.py +++ b/networkbot.py @@ -5,7 +5,7 @@ # DEPENDENCIES # sudo apt-get install python3 # sudo apt-get -y install python3-pip -# python3 -m pip install discord.py==1.3.4 +# python3 -m pip install discord.py>=2.0.0 # # HOW TO USE # Go to https://discordapp.com/developers/applications/ @@ -21,6 +21,7 @@ import random import asyncio import aiohttp +import discord import json from discord.ext import commands from discord.ext.commands import Bot @@ -46,12 +47,32 @@ # port used to communicate with the network (your network's RPC port) PORT = "YOUR_RPC_PORT_GOES_HERE" +# Create bot +class NetworkBot(Bot): + def __init__(self, *args, **kwargs): + super(NetworkBot, self).__init__(*args, **kwargs) + + # print list of servers where this bot is active to console + async def list_servers(self): + await self.tree.sync() + while not self.is_closed(): + # you can customize the output message(s) below + print("--- NETWORK BOT ONLINE ---") + for guild in self.guilds: + # you can customize the output message(s) below + print('Active servers: ' + str(guild.name)) + await asyncio.sleep(600) + + async def on_ready(self): + self.loop.create_task(self.list_servers()) + # start a bot -client = Bot(command_prefix=BOT_PREFIX, description=description) +intents = discord.Intents.all() +client = NetworkBot(command_prefix=BOT_PREFIX, description=description, intents=intents) # commmand: &height # network top block height -@client.command(description="Network top block height.", brief="Blockchain height.") +@client.hybrid_command(description="Network top block height.", brief="Blockchain height.") async def height(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getheight' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -63,7 +84,7 @@ async def height(context: commands.Context): # commmand: &hash # appx. network hash rate -@client.command(description="Appx. network hash rate.", brief="Network hash rate.") +@client.hybrid_command(description="Appx. network hash rate.", brief="Network hash rate.") async def hash(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -75,7 +96,7 @@ async def hash(context: commands.Context): # commmand: &diff # current network difficulty -@client.command(description="Current network difficulty.", brief="Network difficulty.") +@client.hybrid_command(description="Current network difficulty.", brief="Network difficulty.") async def diff(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -87,7 +108,7 @@ async def diff(context: commands.Context): # commmand: &tx # total network transactions -@client.command(description="Total network transactions.", brief="Network transactions.") +@client.hybrid_command(description="Total network transactions.", brief="Network transactions.") async def tx(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -99,7 +120,7 @@ async def tx(context: commands.Context): # commmand: &txpool # current transactions pool size -@client.command(description="Current transactions pool size.", brief="TX pool size.") +@client.hybrid_command(description="Current transactions pool size.", brief="TX pool size.") async def txpool(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -111,7 +132,7 @@ async def txpool(context: commands.Context): # commmand: &ver # current daemon version -@client.command(description="Current daemon version.", brief="Daemon version.") +@client.hybrid_command(description="Current daemon version.", brief="Daemon version.") async def ver(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -123,7 +144,7 @@ async def ver(context: commands.Context): # commmand: &stats # key network stats all in one place -@client.command(description="Key network stats all in one place.", brief="Network stats.") +@client.hybrid_command(description="Key network stats all in one place.", brief="Network stats.") async def stats(context: commands.Context): url = 'http://' + str(HOST) + ':' + str(PORT) + '/getinfo' async with aiohttp.ClientSession() as session: # Async HTTP request @@ -135,17 +156,4 @@ async def stats(context: commands.Context): ) -# print list of servers where this bot is active to console -async def list_servers(): - await client.wait_until_ready() - while not client.is_closed: - # you can customize the output message(s) below - print("--- NETWORK BOT ONLINE ---") - for server in client.servers: - # you can customize the output message(s) below - print('Active servers: ' + str(server.name)) - await asyncio.sleep(600) - - -client.loop.create_task(list_servers()) client.run(TOKEN) From 841accec1630a43d89444b0e1a06ca41df03e218 Mon Sep 17 00:00:00 2001 From: Mika Lindqvist Date: Sat, 19 Nov 2022 12:59:56 +0200 Subject: [PATCH 3/3] Use reply() instead of send(). --- networkbot.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/networkbot.py b/networkbot.py index 9b6015a..2598327 100644 --- a/networkbot.py +++ b/networkbot.py @@ -80,7 +80,7 @@ async def height(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 **Network height:** " + str(response['height'])) + await context.reply("🌐 **Network height:** " + str(response['height'])) # commmand: &hash # appx. network hash rate @@ -92,7 +92,7 @@ async def hash(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 **Network hash rate:** " + str(response['hashrate']) + " H/s") + await context.reply("🌐 **Network hash rate:** " + str(response['hashrate']) + " H/s") # commmand: &diff # current network difficulty @@ -104,7 +104,7 @@ async def diff(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 **Network difficulty:** " + str(response['difficulty'])) + await context.reply("🌐 **Network difficulty:** " + str(response['difficulty'])) # commmand: &tx # total network transactions @@ -116,7 +116,7 @@ async def tx(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 **Network transactions:** " + str(response['tx_count'])) + await context.reply("🌐 **Network transactions:** " + str(response['tx_count'])) # commmand: &txpool # current transactions pool size @@ -128,7 +128,7 @@ async def txpool(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 **Transactions pool:** " + str(response['tx_pool_size'])) + await context.reply("🌐 **Transactions pool:** " + str(response['tx_pool_size'])) # commmand: &ver # current daemon version @@ -140,7 +140,7 @@ async def ver(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 **Daemon Version:** " + str(response['version'])) + await context.reply("🌐 **Daemon Version:** " + str(response['version'])) # commmand: &stats # key network stats all in one place @@ -152,7 +152,7 @@ async def stats(context: commands.Context): response = await raw_response.text() response = json.loads(response) # you can customize the output message(s) below - await context.send("🌐 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']) + await context.reply("🌐 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']) )