-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_faucet_bot.py
122 lines (101 loc) · 5.07 KB
/
discord_faucet_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
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
114
115
116
117
118
119
120
121
122
import discord
import spacemesh_api
import yaml
import logging
import time
import sys
# Turn Down Discord Logging
disc_log = logging.getLogger('discord')
disc_log.setLevel(logging.CRITICAL)
# Configure Logging
logging.basicConfig(stream=sys.stdout, level=logging.CRITICAL)
logger = logging.getLogger(__name__)
# Load config
with open("config.yaml", 'r') as config:
cfg = yaml.load(config, Loader=yaml.FullLoader)
PUBLIC_KEY = str(cfg["public_key"])
ADDRESS = str(cfg["address"])
PRIVATE_KEY = str(cfg["private_key"])
TOKEN = str(cfg["faucet"]["discord_bot_token"])
LISTENING_CHANNELS = str(cfg["faucet"]["discord_listening_channel"])
FAUCET_AMOUNT = int(cfg["faucet"]["amount_to_send"])
FAUCET_FEE = int(cfg["faucet"]["fee"])
REQUEST_COLDOWN = int(cfg["faucet"]["request_coldown_sec"])
APPROVE_EMOJI = "💸"
ACTIVE_REQUESTS = {}
decimal = 1e12
client = discord.Client()
@client.event
async def on_ready():
logger.info(f'Logged in as {client.user}')
@client.event
async def on_message(message):
message_timestamp = time.time()
# Do not listen to your own messages
if message.author == client.user:
return
if message.content.startswith('$help') and message.channel.name in LISTENING_CHANNELS:
help_msg = f'`$faucet_status` - display info about faucet node status\n' \
f'`$tx_info <transaction_id>` - display information about specific transaction\n' \
f'Coins request format example:\n' \
f'`0xafed9a1c17ca7eaa7a6795dbc7bee1b1d992c7ba`'
await message.channel.send(help_msg)
# Show node synchronization settings
if message.content.startswith('$faucet_status') and message.channel.name in LISTENING_CHANNELS:
try:
status = spacemesh_api.get_node_status()
if "synced" in status:
status = f'```' \
f'Peers: {status["peers"]}\n' \
f'Synced: {status["synced"]}\n' \
f'Layers: {status["currentLayer"]}\\{status["syncedLayer"]}\n```'
await message.channel.send(status)
except Exception as statusErr:
print(statusErr)
if message.content.startswith('$tx_info') and message.channel.name in LISTENING_CHANNELS:
try:
hash_id = str(message.content).replace("$tx_info", "").replace(" ", "")
if len(hash_id) == 64:
tr_info = spacemesh_api.get_transaction_info(hash_id)
# See this - https://github.com/spacemeshos/go-spacemesh/issues/1908
if "amount" and "fee" in str(tr_info):
tr_info = f'```' \
f'From: 0x{str(tr_info["sender"]["address"])}\n' \
f'To: 0x{str(tr_info["receiver"]["address"])}\n' \
f'Amount: {float(int(tr_info["amount"]) / decimal)} SMH\n' \
f'Fee: {int(tr_info["fee"])}\n' \
f'STATUS: {tr_info["status"]}```'
await message.channel.send(tr_info)
else:
await message.channel.send(f'Incorrect len hash id {hash_id}')
except Exception as tx_infoErr:
print(tx_infoErr)
if str(message.content[:2]) == "0x" and len(message.content) == 42 and message.channel.name in LISTENING_CHANNELS:
channel = message.channel
requester = message.author
requester_address = str(message.content)
if requester.id in ACTIVE_REQUESTS:
check_time = ACTIVE_REQUESTS[requester.id]["next_request"]
if check_time > message_timestamp:
please_wait_text = f'{requester.mention}, You can request coins no more than once every 3 hours.\n' \
f'The next attempt is possible after {round((check_time - message_timestamp) / 60, 2)} minutes'
await channel.send(please_wait_text)
return
else:
del ACTIVE_REQUESTS[requester.id]
if requester.id not in ACTIVE_REQUESTS and requester_address not in ACTIVE_REQUESTS:
ACTIVE_REQUESTS[requester.id] = {
"address": requester_address,
"requester": requester,
"next_request": message_timestamp + REQUEST_COLDOWN}
print(ACTIVE_REQUESTS)
faucet_balance = int(spacemesh_api.get_balance(ADDRESS))
if faucet_balance > (FAUCET_AMOUNT + FAUCET_FEE) * 10:
transaction = spacemesh_api.send_transaction(frm=ADDRESS, to=requester_address, amount=FAUCET_AMOUNT,
gas_price=FAUCET_FEE, private_key=PRIVATE_KEY)
logger.info(f'Transaction result:\n{transaction}')
if transaction["value"] == "ok":
await message.add_reaction(emoji=APPROVE_EMOJI)
await message.channel.send(f'{requester.mention}, Transaction has been sent. '
f'Check TX status: `$tx_info {str(transaction["id"])}`')
client.run(TOKEN)