-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc.py
173 lines (136 loc) · 5.45 KB
/
rpc.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
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
"""
Python Discord Status
----------------------
By Ibai Farina
Version 1.2 adds new functionality and a refactor of some files.
Discord-Status-v1.2 lets you add buttons to your RPC.
"""
__title__ = 'Discord-Status'
__author__ = 'Ibai Farina'
__license__ = 'MIT'
__version__ = '1.2'
import datetime
import json
import os
import random
import sys
import time
from pypresence import Presence, ServerError, InvalidID, exceptions
from misc import color, logger, date
col = color.Color()
class DiscordPresence:
"""
Set Discord Presence Class
"""
def __init__(self, config_path: str):
"""
Init config
:param config_path: the path to config.json
"""
# Load config.json file
with open(config_path, encoding="utf8") as file:
self.config = json.load(file)
file.close()
try:
# Identifier ID
self.client_id_ = str(self.config['Identifiers']['clientID'])
# State
self.state = str(self.config['State']['state'])
self.startTimeStamp = int(self.config['State']['startTimeStamp'])
# If state is null then use the sequence
self.state_seq = self.config["State"]["stateRndSeq"]
# Whether track running time
self.use_time = int(self.config['State']['useTime'])
# The sleeping time between each status update in seconds
self.interval_time = int(self.config['State']['intervalTime'])
self.buttons = self.config["State"]["buttons"]
if self.buttons:
assert len(self.buttons) <= 2, logger.logger("Error", "Max number of buttons is two!", "error")
# Images
self.largeImage = self.config['Images']['largeImage']
self.smallImage = self.config['Images']['smallImage']
except ValueError:
logger.logger("Error", "value types are incorrect", "error")
raise ValueError
# Assign default time stamp if it's none
if not self.startTimeStamp:
self.startTimeStamp = 0
@property
def client_id(self):
return self.client_id_
def connect(self) -> None:
"""
Connect to Discord RPC
:return: None
"""
if self.client_id and self.state:
# Log params to the console
logger.logger("ClientID", self.client_id[:int(len(self.client_id) / 2)] + "...")
if self.state == "None":
logger.logger("State sequence", ",".join(self.state_seq))
else:
logger.logger("State", self.state)
rpc = Presence(self.client_id) # Initialize RPC
try:
rpc.connect() # Start the handshake loop
logger.logger("Connect", "true", "success")
except exceptions.InvalidPipe:
logger.logger("Error",
"An error has occurred while trying to connect to Discord. Make sure your Discord "
"client is running!",
"error")
sys.exit(-1)
# Set initial played time
played_seconds = datetime.timedelta(seconds=self.startTimeStamp)
# Start Loop
while True:
display_time = date.format_date(played_seconds) if self.use_time else " "
try:
# Update status every second
if self.state == "None":
iter_state = random.choice(self.state_seq)
else:
iter_state = self.state
rpc.update(large_image=self.largeImage,
small_image=self.smallImage,
details=iter_state,
state=display_time,
buttons=self.buttons)
played_seconds += datetime.timedelta(seconds=1)
time.sleep(self.interval_time) # Delay in seconds
except ServerError as E:
logger.logger("Error", f"ServerError: {E}", "error")
break
except InvalidID:
logger.logger("Error", "Connection closed. Possible cause: Invalid ID", "error")
break
except Exception as E:
logger.logger("Error", f"Unexpected error: {col.fore_color(str(E), 'BLUE')}", "error")
break
else:
logger.logger("Error", "Please fill the required values", "error")
# Do the magic
if __name__ == "__main__":
try:
os.system("cls" if os.name == "nt" else "clear") # Clear console
print("Auax")
print("Discord RPC: https://github.com/auax/DiscordRichPresence")
print("-" * 50 + "\n") # Escape
try:
# Initialize class
dp = DiscordPresence("config.json")
# Connect to Discord
dp.connect()
except FileNotFoundError:
logger.logger("Error", "config.json file not found. Make sure it's in the same directory.",
"error")
except Exception as E:
logger.logger("Error", f"Unexpected error: {col.fore_color(str(E), 'WHITE')}", "error")
except KeyboardInterrupt:
pass
except Exception as E:
input(E)
finally:
print(col.fore_color("\nExiting...", "RED"))
input("Exit? >> ")
sys.exit(-1)