17
17
from discord .ext .ipc .errors import *
18
18
19
19
20
- def route (name = None ):
21
- """Used to register a coroutine as an endpoint"""
20
+ def route (name : str = None ):
21
+ """
22
+ Used to register a coroutine as an endpoint when you don't have
23
+ access to an instance of :class:`.Server`
24
+
25
+ Parameters
26
+ ----------
27
+ name: str
28
+ The endpoint name. If not provided the method name will be
29
+ used.
30
+ """
22
31
23
32
def decorator (func ):
24
33
if not name :
@@ -32,9 +41,7 @@ def decorator(func):
32
41
33
42
34
43
class IpcServerResponse :
35
- """Format the json data parsed into a nice object"""
36
-
37
- def __init__ (self , data ):
44
+ def __init__ (self , data : dict ):
38
45
self ._json = data
39
46
self .length = len (data )
40
47
@@ -44,7 +51,6 @@ def __init__(self, data):
44
51
setattr (self , key , value )
45
52
46
53
def to_json (self ):
47
- """Convert object to json"""
48
54
return self ._json
49
55
50
56
def __repr__ (self ):
@@ -55,6 +61,26 @@ def __str__(self):
55
61
56
62
57
63
class Server :
64
+ """The IPC server. Usually used on the bot process for receiving
65
+ requests from the client.
66
+
67
+ Attributes
68
+ ----------
69
+ bot: :class:`~discord.ext.commands.Bot`
70
+ Your bot instance
71
+ host: str
72
+ The host to run the IPC Server on. Defaults to localhost.
73
+ port: int
74
+ The port to run the IPC Server on. Defaults to 8765.
75
+ secret_key: str
76
+ A secret key. Used for authentication and should be the same as
77
+ your client's secret key.
78
+ do_multicast: bool
79
+ Turn multicasting on/off. Defaults to True
80
+ multicast_port: int
81
+ The port to run the multicasting server on. Defaults to 20000
82
+ """
83
+
58
84
ROUTES = {}
59
85
60
86
def __init__ (
@@ -82,8 +108,15 @@ def __init__(
82
108
83
109
self .endpoints = {}
84
110
85
- def route (self , name = None ):
86
- """Used to register a coroutine as an endpoint"""
111
+ def route (self , name : str = None ):
112
+ """Used to register a coroutine as an endpoint when you have
113
+ access to an instance of :class:`.Server`.
114
+
115
+ Parameters
116
+ ----------
117
+ name: str
118
+ The endpoint name. If not provided the method name will be used.
119
+ """
87
120
88
121
def decorator (func ):
89
122
if not name :
@@ -96,11 +129,19 @@ def decorator(func):
96
129
return decorator
97
130
98
131
def update_endpoints (self ):
132
+ """Called internally to update the server's endpoints for cog routes."""
99
133
self .endpoints = {** self .endpoints , ** self .ROUTES }
100
134
101
135
self .ROUTES = {}
102
136
103
- async def handle_accept (self , request ):
137
+ async def handle_accept (self , request : aiohttp .web .Request ):
138
+ """Handles websocket requests from the client process.
139
+
140
+ Parameters
141
+ ----------
142
+ request: :class:`~aiohttp.web.Request`
143
+ The request made by the client, parsed by aiohttp.
144
+ """
104
145
self .update_endpoints ()
105
146
106
147
websocket = aiohttp .web .WebSocketResponse ()
@@ -159,8 +200,14 @@ async def handle_accept(self, request):
159
200
160
201
raise JSONEncodeError (error_response )
161
202
162
- async def handle_multicast (self , request ):
163
- """Handle multicast requests"""
203
+ async def handle_multicast (self , request : aiohttp .web .Request ):
204
+ """Handles multicasting websocket requests from the client.
205
+
206
+ Parameters
207
+ ----------
208
+ request: :class:`~aiohttp.web.Request`
209
+ The request made by the client, parsed by aiohttp.
210
+ """
164
211
websocket = aiohttp .web .WebSocketResponse ()
165
212
await websocket .prepare (request )
166
213
@@ -189,7 +236,7 @@ async def __start(self, application, port):
189
236
await site .start ()
190
237
191
238
def start (self ):
192
- """Start the IPC server"""
239
+ """Starts the IPC server. """
193
240
self .bot .dispatch ("ipc_ready" )
194
241
195
242
self ._server = aiohttp .web .Application (loop = self .loop )
0 commit comments