forked from Scottcjn/Rustchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord_rich_presence.py
More file actions
325 lines (271 loc) · 10 KB
/
discord_rich_presence.py
File metadata and controls
325 lines (271 loc) · 10 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
"""
RustChain Discord Rich Presence
Shows mining status in Discord profile:
- Current hashrate/attestations
- RTC earned today
- Miner uptime
- Hardware type (G4/G5/POWER8/etc)
Usage:
python3 discord_rich_presence.py --miner-id YOUR_MINER_ID [--client-id DISCORD_CLIENT_ID]
Requirements:
pip install pypresence requests
"""
import os
import sys
import time
import json
import requests
from datetime import datetime, timedelta
from pypresence import Presence
# RustChain API endpoint (self-signed cert requires verification=False)
RUSTCHAIN_API = "https://50.28.86.131"
# Local state file for tracking earnings
STATE_FILE = os.path.expanduser("~/.rustchain_discord_state.json")
# Default update interval (seconds)
UPDATE_INTERVAL = 60
def load_state():
"""Load previous state from file."""
if os.path.exists(STATE_FILE):
try:
with open(STATE_FILE, 'r') as f:
return json.load(f)
except:
pass
return {}
def save_state(state):
"""Save current state to file."""
with open(STATE_FILE, 'w') as f:
json.dump(state, f, indent=2)
def get_miner_info(miner_id):
"""Get miner information from RustChain API."""
try:
response = requests.get(
f"{RUSTCHAIN_API}/wallet/balance",
params={"miner_id": miner_id},
verify=False, # Self-signed cert
timeout=10
)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"Error getting balance: {e}")
return None
def get_miners_list():
"""Get list of all active miners."""
try:
response = requests.get(
f"{RUSTCHAIN_API}/api/miners",
verify=False,
timeout=10
)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"Error getting miners list: {e}")
return []
def get_epoch_info():
"""Get current epoch information."""
try:
response = requests.get(
f"{RUSTCHAIN_API}/epoch",
verify=False,
timeout=10
)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"Error getting epoch info: {e}")
return None
def get_node_health():
"""Get node health information."""
try:
response = requests.get(
f"{RUSTCHAIN_API}/health",
verify=False,
timeout=10
)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"Error getting health: {e}")
return None
def calculate_rtc_earned_today(current_balance, state):
"""Calculate RTC earned since last state update."""
if not state:
return 0.0
previous_balance = state.get('last_balance', 0.0)
earned = current_balance - previous_balance
# Don't show negative earnings (withdrawals)
return max(0.0, earned)
def calculate_miner_uptime(last_attest_timestamp, state):
"""Calculate miner uptime based on last attestation."""
if not last_attest_timestamp:
return "Unknown"
last_attest = datetime.fromtimestamp(last_attest_timestamp)
now = datetime.now()
# Time since last attestation
time_since = now - last_attest
# If last attestation was recent (within 2 epochs), consider online
if time_since < timedelta(hours=2):
return "Online"
elif time_since < timedelta(hours=24):
return f"{int(time_since.total_seconds() // 3600)}h ago"
else:
return "Offline"
def get_hardware_display(hardware_type):
"""Get a short display string for hardware type."""
if "G4" in hardware_type:
return "🍎 PowerPC G4"
elif "G5" in hardware_type:
return "🍎 PowerPC G5"
elif "POWER8" in hardware_type:
return "⚡ POWER8"
elif "x86_64" in hardware_type:
return "💻 Modern PC"
elif "M1" in hardware_type or "M2" in hardware_type:
return "🍎 Apple Silicon"
else:
return "💻 " + hardware_type.split()[0]
def format_presence_data(miner_data, balance_data, epoch_data):
"""Format data for Discord Rich Presence."""
hardware_type = miner_data.get('hardware_type', 'Unknown')
antiquity_multiplier = miner_data.get('antiquity_multiplier', 1.0)
last_attest = miner_data.get('last_attest', 0)
# Current balance
balance = balance_data.get('amount_rtc', 0.0)
# Hardware icon and short name
hw_display = get_hardware_display(hardware_type)
# Multiplier badge
multiplier_badge = f"{antiquity_multiplier}x"
# Uptime status
uptime = calculate_miner_uptime(last_attest, {})
# Epoch info
epoch_num = epoch_data.get('epoch', 0) if epoch_data else 0
slot = epoch_data.get('slot', 0) if epoch_data else 0
epoch_progress = f"E{epoch_num} · S{slot}"
# Discord state (top line)
state_text = f"{hw_display} {multiplier_badge} · {uptime}"
# Discord details (bottom line)
details_text = f"Balance: {balance:.2f} RTC"
# Large image text
large_text = f"{hardware_type} ({antiquity_multiplier}x reward)"
# Small image text
small_text = epoch_progress
return {
'state': state_text,
'details': details_text,
'large_text': large_text,
'small_text': small_text,
'balance': balance,
'uptime': uptime
}
def main():
"""Main loop for Discord Rich Presence."""
import argparse
parser = argparse.ArgumentParser(description='RustChain Discord Rich Presence')
parser.add_argument('--miner-id', required=True, help='Your miner ID (wallet address)')
parser.add_argument('--client-id', help='Discord application client ID (optional)')
parser.add_argument('--interval', type=int, default=UPDATE_INTERVAL, help='Update interval in seconds')
args = parser.parse_args()
miner_id = args.miner_id
client_id = args.client_id
print(f"🍎 RustChain Discord Rich Presence")
print(f"Miner ID: {miner_id}")
print(f"Update interval: {args.interval}s")
print()
# If no client_id provided, use default RustChain app ID (placeholder)
# In production, create your own Discord app at https://discord.com/developers/applications
if not client_id:
print("⚠️ No --client-id provided.")
print("Create a Discord app at https://discord.com/developers/applications")
print("Enable Rich Presence and use your Application ID as --client-id")
print("Continuing without Discord connection (data only)...\n")
client_id = None
# Initialize Discord Presence
rpc = None
if client_id:
try:
rpc = Presence(client_id)
rpc.connect()
print(f"✅ Connected to Discord Rich Presence")
except Exception as e:
print(f"⚠️ Failed to connect to Discord: {e}")
print("Continuing without Discord connection...\n")
rpc = None
# Load previous state
state = load_state()
# Main loop
try:
while True:
# Get miner info from list to find hardware type
miners_list = get_miners_list()
miner_data = None
for m in miners_list:
if m.get('miner') == miner_id:
miner_data = m
break
if not miner_data:
print(f"⚠️ Miner {miner_id} not found in active miners list")
print("Make sure your miner is running and enrolled.\n")
# Show basic data if available
balance_data = get_miner_info(miner_id)
if balance_data:
print(f"Balance: {balance_data.get('amount_rtc', 0):.2f} RTC")
time.sleep(args.interval)
continue
# Get balance
balance_data = get_miner_info(miner_id)
# Get epoch info
epoch_data = get_epoch_info()
# Get node health
health_data = get_node_health()
# Calculate earnings today
if balance_data:
balance = balance_data.get('amount_rtc', 0.0)
earned_today = calculate_rtc_earned_today(balance, state)
# Save current balance
state['last_balance'] = balance
state['last_update'] = datetime.now().isoformat()
save_state(state)
else:
balance = 0.0
earned_today = 0.0
# Format data for Discord
presence_data = format_presence_data(miner_data, balance_data, epoch_data)
# Print status
print(f"[{datetime.now().strftime('%H:%M:%S')}] {presence_data['state']}")
print(f" {presence_data['details']}")
if earned_today > 0:
print(f" +{earned_today:.4f} RTC today")
if health_data:
print(f" Node: {health_data.get('version', 'Unknown')} (uptime: {health_data.get('uptime_s', 0) // 3600}h)")
print()
# Update Discord presence
if rpc:
try:
rpc.update(
state=presence_data['state'],
details=presence_data['details'],
large_image="rustchain",
large_text=presence_data['large_text'],
small_image="epoch",
small_text=presence_data['small_text'],
start=int(time.time())
)
except Exception as e:
print(f"⚠️ Discord update failed: {e}")
# Try to reconnect
try:
rpc.connect()
except:
pass
# Wait for next update
time.sleep(args.interval)
except KeyboardInterrupt:
print("\n👋 Shutting down RustChain Discord Rich Presence")
if rpc:
rpc.close()
sys.exit(0)
if __name__ == '__main__':
main()