Send and receive messages
This commit is contained in:
parent
c3591f5c51
commit
a606aa46ec
5
enums.py
5
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"
|
47
main.py
47
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())
|
Loading…
Reference in New Issue
Block a user