about summary refs log tree commit diff stats
path: root/discord
diff options
context:
space:
mode:
authorNoah <mounderfod@gmail.com>2022-07-08 20:01:29 +0100
committerNoah <mounderfod@gmail.com>2022-07-08 20:01:29 +0100
commit51304896fc682787670506b44054150054dfd71e (patch)
tree90b4c7a94177965473f5528e039ecbbfae25e017 /discord
parente5cce6c643bbae775fd6beeb03c00d43df06d9b9 (diff)
downloaddiscobra-51304896fc682787670506b44054150054dfd71e.tar.gz
feat(gateway): Added basic gateway connection
Could really use refining, this is just a basic implementation
Diffstat (limited to 'discord')
-rw-r--r--discord/client.py49
1 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()