generated from TAG-Epic/speedcord-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.py
42 lines (36 loc) · 1.27 KB
/
workers.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
"""
Created by Epic at 10/29/20
"""
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from custom_types import ExtendedClient as Client
else:
from speedcord import Client
from speedcord.http import Route
from logging import getLogger
from aiohttp import ClientWebSocketResponse
from ujson import dumps
from os import environ as env
class WorkerUtil:
def __init__(self, client: Client):
self.client = client
self.loop = self.client.loop
self.logger = getLogger("workers.manager")
self.ws: ClientWebSocketResponse = None
async def start(self):
self.ws = await self.client.http.create_ws(f"ws://{env['WORKER_MANAGER_HOST']}:6060/controller", compression=0)
async def request(self, guild_id: str, route: Route, **kwargs):
await self.send_ws("request", {
"guild_id": guild_id,
"data": {
"method": route.method,
"path": route.path,
"route_params": {
"guild_id": route.guild_id,
"channel_id": route.channel_id,
"kwargs": kwargs
}
}
})
async def send_ws(self, event_name, event_data):
await self.ws.send_json({"t": event_name, "d": event_data}, dumps=dumps)