about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--discord/client.py12
-rw-r--r--discord/utils/rest.py41
-rw-r--r--main.py11
3 files changed, 34 insertions, 30 deletions
diff --git a/discord/client.py b/discord/client.py
index 01e7fdd..3384657 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -5,10 +5,11 @@ import sys
 import threading
 from typing import Optional, Coroutine, Any, Callable
 import zlib
+import aiohttp
 import websockets
 
 from .utils import EventEmitter
-from .utils.rest import get
+from .utils.rest import RESTClient
 from .intents import Intents, get_number
 from .user import User
 
@@ -26,12 +27,13 @@ class GatewayEvents(IntEnum):
     HELLO              = 10
     HEARTBEAT_ACK      = 11
     GUILD_SYNC         = 12
+
 class Client:
     _token: str
 
     @property
     async def user(self):
-        data = await get(self._token, '/users/@me')
+        data = await self.rest_client.get('/users/@me')
         return User(data)
 
     def __init__(self, intents: list[Intents]):
@@ -43,6 +45,10 @@ class Client:
         self.inflator = zlib.decompressobj()
         self.heartbeat_interval: int = None
         self.ready: bool = False
+        self.rest_client = RESTClient(self._token, aiohttp.ClientSession(headers={
+            "Authorization": f"Bot {self._token}",
+            "User-Agent": "DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
+        }))
 
     async def connect(self):
         async with websockets.connect("wss://gateway.discord.gg/?v=10&encoding=json") as gateway:
@@ -92,7 +98,7 @@ class Client:
         event = msg['t']
 
         if event == 'READY':
-            self.user = User(data['user'])
+            print(data)
 
         self.event_emitter.emit('on_' + event.lower())
 
diff --git a/discord/utils/rest.py b/discord/utils/rest.py
index f7f6ab8..919d54d 100644
--- a/discord/utils/rest.py
+++ b/discord/utils/rest.py
@@ -1,15 +1,14 @@
 import aiohttp
-import asyncio
 
 from discord.utils.exceptions import APIException
 
+class RESTClient:
+    def __init__(self, token: str, session: aiohttp.ClientSession):
+        self.token = token
+        self.session = session
 
-async def get(token, url):
-    async with aiohttp.ClientSession(headers={
-        "Authorization": f"Bot {token}",
-        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
-    }) as session:
-        async with session.get(url='https://discord.com/api/v10' + url) as r:
+    async def get(self, url: str):
+        async with self.session.get(url='https://discord.com/api/v10' + url) as r:
             data = await r.json()
             match r.status:
                 case 200:
@@ -18,12 +17,8 @@ async def get(token, url):
                     raise APIException(data['message'])
 
 
-async def post(token, url, data):
-    async with aiohttp.ClientSession(headers={
-        "Authorization": f"Bot {token}",
-        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
-    }) as session:
-        async with session.post(url='https://discord.com/api/v10' + url, data=data) as r:
+    async def post(self, url: str, data):
+        async with self.session.post(url='https://discord.com/api/v10' + url, data=data) as r:
             data = await r.json()
             match r.status:
                 case 200 | 204:
@@ -32,26 +27,18 @@ async def post(token, url, data):
                     raise APIException(data['message'])
 
 
-async def patch(token, url, data):
-    async with aiohttp.ClientSession(headers={
-        "Authorization": f"Bot {token}",
-        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
-    }) as session:
-        async with session.patch(url='https://discord.com/api/v10' + url, data=data) as r:
-            data = await r.json()
-            match r.status:
+    async def patch(self, url, data):
+        async with self.session.patch(url='https://discord.com/api/v10' + url, data=data) as res:
+            data = await res.json()
+            match res.status:
                 case 200 | 204:
                     return data
                 case other:
                     raise APIException(data['message'])
 
 
-async def delete(token, url):
-    async with aiohttp.ClientSession(headers={
-        "Authorization": f"Bot {token}",
-        "User-Agent": f"DiscordBot (https://github.com/mounderfod/discobra 0.0.1)"
-    }) as session:
-        async with session.delete(url='https://discord.com/api/v10' + url) as r:
+    async def delete(self, url):
+        async with self.session.delete(url='https://discord.com/api/v10' + url) as r:
             data = await r.json()
             match r.status:
                 case 200:
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..5167f6e
--- /dev/null
+++ b/main.py
@@ -0,0 +1,11 @@
+from discord import Client
+from discord.intents import Intents
+
+bot = Client(intents=[Intents.GUILD_MESSAGES])
+
+
+@bot.event
+async def on_ready():
+    print(f"We have logged in as {bot.user.username}")
+
+bot.run("NzM1MTA3NDc0OTY0ODczMzM3.G6_nDX.Ctozd9eWn3xY_lGFT46uCJjrHQ9fQYRHAAD9Ic")
\ No newline at end of file