88from .sockets import UnixPipe
99from .commands import *
1010from .exceptions import *
11+ from .constants import MAX_SOCKET_RETRY_ATTEMPTS
1112
1213
1314OP_HANDSHAKE = 0
@@ -26,23 +27,22 @@ def __init__(self, client_id: str, client_secret: str, access_token: str = ""):
2627 self .polling = False
2728
2829 def _send_rpc_command (self , command : str , args : dict = None ):
29- payload = {
30- 'cmd' : command ,
31- 'nonce' : str (uuid .uuid4 ())
32- }
30+ payload = {"cmd" : command , "nonce" : str (uuid .uuid4 ())}
3331 if args is not None :
34- payload [' args' ] = args
32+ payload [" args" ] = args
3533 self .rpc .send (payload , OP_FRAME )
3634
3735 def is_connected (self ):
3836 return self .polling
3937
4038 def connect (self , callback : callable ):
4139 tries = 0
42- while tries < 5 :
43- log .debug (f"Attempting to connect to socket, attempt { tries + 1 } /5" )
40+ while tries < MAX_SOCKET_RETRY_ATTEMPTS :
41+ log .debug (
42+ f"Attempting to connect to socket, attempt { tries + 1 } /{ MAX_SOCKET_RETRY_ATTEMPTS } "
43+ )
4444 self .rpc .connect ()
45- self .rpc .send ({'v' : 1 , ' client_id' : self .client_id }, OP_HANDSHAKE )
45+ self .rpc .send ({"v" : 1 , " client_id" : self .client_id }, OP_HANDSHAKE )
4646 _ , resp = self .rpc .receive ()
4747 if resp :
4848 break
@@ -52,9 +52,9 @@ def connect(self, callback: callable):
5252 except Exception as ex :
5353 log .error (f"invalid response. { ex } " )
5454 raise RPCException
55- if data .get (' code' ) == 4000 :
55+ if data .get (" code" ) == 4000 :
5656 raise InvalidID
57- if data .get (' cmd' ) != ' DISPATCH' or data .get (' evt' ) != ' READY' :
57+ if data .get (" cmd" ) != " DISPATCH" or data .get (" evt" ) != " READY" :
5858 raise RPCException
5959 self .polling = True
6060 threading .Thread (target = self .poll_callback , args = [callback ]).start ()
@@ -76,61 +76,65 @@ def poll_callback(self, callback: callable):
7676 callback (val [0 ], val [1 ])
7777
7878 def authorize (self ):
79- payload = {
80- 'client_id' : self .client_id ,
81- 'scopes' : ['rpc' , 'identify' ]
82- }
79+ payload = {"client_id" : self .client_id , "scopes" : ["rpc" , "identify" ]}
8380 self ._send_rpc_command (AUTHORIZE , payload )
8481
8582 def authenticate (self , access_token : str = None ):
8683 if not access_token :
8784 self .authorize ()
8885 return
8986 self .access_token = access_token
90- payload = {
91- 'access_token' : self .access_token
92- }
87+ payload = {"access_token" : self .access_token }
9388 self ._send_rpc_command (AUTHENTICATE , payload )
9489
9590 def refresh (self , code : str ):
96- token = requests .post ('https://discord.com/api/oauth2/token' , {
97- 'grant_type' : 'refresh_token' ,
98- 'refresh_token' : code ,
99- 'client_id' : self .client_id ,
100- 'client_secret' : self .client_secret
101- }, timeout = 5 )
91+ token = requests .post (
92+ "https://discord.com/api/oauth2/token" ,
93+ {
94+ "grant_type" : "refresh_token" ,
95+ "refresh_token" : code ,
96+ "client_id" : self .client_id ,
97+ "client_secret" : self .client_secret ,
98+ },
99+ timeout = 5 ,
100+ )
102101 resp = token .json ()
103- if not ' access_token' in resp :
104- raise Exception (' refresh failed' )
102+ if not " access_token" in resp :
103+ raise Exception (" refresh failed" )
105104 return resp
106105
107106 def get_access_token (self , code : str ):
108- token = requests .post ('https://discord.com/api/oauth2/token' , {
109- 'grant_type' : 'authorization_code' ,
110- 'code' : code ,
111- 'client_id' : self .client_id ,
112- 'client_secret' : self .client_secret
113- }, timeout = 5 )
107+ token = requests .post (
108+ "https://discord.com/api/oauth2/token" ,
109+ {
110+ "grant_type" : "authorization_code" ,
111+ "code" : code ,
112+ "client_id" : self .client_id ,
113+ "client_secret" : self .client_secret ,
114+ },
115+ timeout = 5 ,
116+ )
114117 resp = token .json ()
115- if not ' access_token' in resp :
116- raise Exception (' invalid oauth request' )
118+ if not " access_token" in resp :
119+ raise Exception (" invalid oauth request" )
117120 return resp
118121
119122 def subscribe (self , event : str , args : dict = None ):
120- self .rpc .send ({
121- 'cmd' : SUBSCRIBE ,
122- 'evt' : event ,
123- 'nonce' : str (uuid .uuid4 ()),
124- 'args' : args
125- }, OP_FRAME )
123+ self .rpc .send (
124+ {"cmd" : SUBSCRIBE , "evt" : event , "nonce" : str (uuid .uuid4 ()), "args" : args },
125+ OP_FRAME ,
126+ )
126127
127128 def unsubscribe (self , event : str , args : dict = None ):
128- self .rpc .send ({
129- 'cmd' : UNSUBSCRIBE ,
130- 'evt' : event ,
131- 'nonce' : str (uuid .uuid4 ()),
132- 'args' : args
133- }, OP_FRAME )
129+ self .rpc .send (
130+ {
131+ "cmd" : UNSUBSCRIBE ,
132+ "evt" : event ,
133+ "nonce" : str (uuid .uuid4 ()),
134+ "args" : args ,
135+ },
136+ OP_FRAME ,
137+ )
134138
135139 def set_voice_settings (self , settings ):
136140 self ._send_rpc_command (SET_VOICE_SETTINGS , settings )
@@ -139,16 +143,11 @@ def get_voice_settings(self):
139143 self ._send_rpc_command (GET_VOICE_SETTINGS )
140144
141145 def select_voice_channel (self , channel_id : str , force : bool = False ):
142- args = {
143- 'channel_id' : channel_id ,
144- 'force' : force
145- }
146+ args = {"channel_id" : channel_id , "force" : force }
146147 self ._send_rpc_command (SELECT_VOICE_CHANNEL , args )
147148
148149 def select_text_channel (self , channel_id : str ):
149- args = {
150- 'channel_id' : channel_id
151- }
150+ args = {"channel_id" : channel_id }
152151 self ._send_rpc_command (SELECT_TEXT_CHANNEL , args )
153152
154153 def get_selected_voice_channel (self ) -> str :
0 commit comments