about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorNoah <mounderfod@gmail.com>2022-07-08 20:01:29 +0100
committerNoah <mounderfod@gmail.com>2022-07-08 21:20:36 +0100
commit74d5b1199adc085d39417b8e1233a8730394d42e (patch)
tree7ca80f2128c9d4f8dc05ed17d19f18812a4b74d2
parente5cce6c643bbae775fd6beeb03c00d43df06d9b9 (diff)
downloaddiscobra-74d5b1199adc085d39417b8e1233a8730394d42e.tar.gz
feat(gateway): Added basic gateway connection
Could really use refining, this is just a basic implementation
-rw-r--r--discord/client.py49
-rw-r--r--discord/user.py0
2 files changed, 49 insertions, 0 deletions
diff --git a/discord/client.py b/discord/client.py
index e69de29..73fe0e3 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -0,0 +1,49 @@
+import asyncio
+import json
+import sys
+import threading
+
+import websockets
+
+from discord.intents import Intents, gen_number
+
+loop = asyncio.get_event_loop()
+
+
+class Client:
+    def __init__(self, token: str, intents: list[Intents]):
+        self.gateway = None
+        code = gen_number(intents)
+        asyncio.run(self.connect(token, code))
+
+    async def connect(self, token: str, intent_code: int):
+        async with websockets.connect("wss://gateway.discord.gg/?v=10&encoding=json") as gateway:
+            hello = await gateway.recv()
+            self.gateway = gateway
+            threading.Thread(target=loop.run_forever).start()
+            heartbeat = asyncio.run_coroutine_threadsafe(
+                self.heartbeat(gateway, json.loads(hello)['d']['heartbeat_interval']), loop)
+            identify = {
+                "op": 2,
+                "d": {
+                    "token": token,
+                    "intents": intent_code,
+                    "properties": {
+                        "os": sys.platform,
+                        "browser": "discobra",
+                        "device": "discobra"
+                    }
+                }
+            }
+            await gateway.send(json.dumps(identify))
+            ready = await gateway.recv()
+
+    async def heartbeat(self, gateway: websockets.WebSocketClientProtocol, interval: int):
+        while True:
+            await asyncio.sleep(interval / 1000)
+            heartbeat = {
+                "op": 1,
+                "d": None
+            }
+            await gateway.send(json.dumps(heartbeat))
+            ack = await gateway.recv()
diff --git a/discord/user.py b/discord/user.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/discord/user.py