From a606aa46ec70e0e06e17cecd8de6227c603ee7c8 Mon Sep 17 00:00:00 2001 From: guams Date: Thu, 6 Mar 2025 09:29:25 +0100 Subject: [PATCH] Send and receive messages --- enums.py | 5 +++++ main.py | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/enums.py b/enums.py index 6e46ead..3a5b32d 100644 --- a/enums.py +++ b/enums.py @@ -14,3 +14,8 @@ class OpCode(int, Enum): HELLO = 10 HEARTBEAT_ACK = 11 REQUEST_SOUNDBOARD_SOUNDS = 31 + +class EventTitle(str, Enum): + MESSAGE_CREATE = "MESSAGE_CREATE" + GUILD_CREATE = "GUILD_CREATE" + READY = "READY" \ No newline at end of file diff --git a/main.py b/main.py index cf3be6a..9327b4d 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,24 @@ import asyncio +from typing import Any + import requests import websockets import json -from enums import OpCode + +from websockets import Response + +from enums import OpCode, EventTitle with open('configuration.json', 'r') as file: CONFIG = json.load(file) -GATEWAY_URL = "wss://gateway.discord.gg/?v=10&encoding=json" -API_URL = "https://discord.com/api/v10" -TOKEN = CONFIG["TOKEN"] +GATEWAY_URL: str = "wss://gateway.discord.gg/?v=10&encoding=json" +API_URL: str = "https://discord.com/api/v10" +TOKEN: str = CONFIG["TOKEN"] async def send_message(channel_id: int, message: str): - body = { + body: dict= { "content": message, "tts": False, "embeds": [{ @@ -22,21 +27,21 @@ async def send_message(channel_id: int, message: str): }] } - res = await requests.post(url=f"{API_URL}/channels/{channel_id}/messages", json=body) + res: Response = requests.post(url=f"{API_URL}/channels/{channel_id}/messages", json=body, headers={"Authorization": f"Bot {TOKEN}"}) print(res) async def identify(websocket): - payload = { + payload: dict = { "op": OpCode.IDENTIFY, "d": { "token": TOKEN, "properties": { - "os": "windows", + "os": "linux", "browser": "gambling", "device": "gambling" }, - "intents": 513, + "intents": 65024, } } await websocket.send(json.dumps(payload)) @@ -47,6 +52,13 @@ async def heartbeat(websocket, interval): await asyncio.sleep(interval / 1000) await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None})) +async def get_event(response: Any): + match response["t"]: + case EventTitle.MESSAGE_CREATE: + print(f'{response["d"]["author"]["username"]}: {response["d"]["content"]}') + case _: + print("Unknown event") + async def connect(): async with websockets.connect(GATEWAY_URL) as websocket: @@ -58,9 +70,18 @@ async def connect(): await identify(websocket) while True: - message = json.loads(await websocket.recv()) - print("Received: ", message) + response = json.loads(await websocket.recv()) + #print("Received: ", response) + match response["op"]: + case OpCode.DISPATCH: + await get_event(response) + case _: + print("osef") -asyncio.run(connect()) -# send_message(channel_id=840107060901052426, message="Salut") +async def main(): + gateway_connect = asyncio.create_task(connect()) + await send_message(channel_id=840107060901052426, message="Salut") + await gateway_connect + +asyncio.run(main()) \ No newline at end of file