67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
import asyncio
|
|
import requests
|
|
import websockets
|
|
import json
|
|
from enums import OpCode
|
|
|
|
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"]
|
|
|
|
|
|
async def send_message(channel_id: int, message: str):
|
|
body = {
|
|
"content": message,
|
|
"tts": False,
|
|
"embeds": [{
|
|
"title": "Hello, Embed!",
|
|
"description": "This is an embedded message."
|
|
}]
|
|
}
|
|
|
|
res = await requests.post(url=f"{API_URL}/channels/{channel_id}/messages", json=body)
|
|
print(res)
|
|
|
|
|
|
async def identify(websocket):
|
|
payload = {
|
|
"op": OpCode.IDENTIFY,
|
|
"d": {
|
|
"token": TOKEN,
|
|
"properties": {
|
|
"os": "windows",
|
|
"browser": "gambling",
|
|
"device": "gambling"
|
|
},
|
|
"intents": 513,
|
|
}
|
|
}
|
|
await websocket.send(json.dumps(payload))
|
|
|
|
|
|
async def heartbeat(websocket, interval):
|
|
while True:
|
|
await asyncio.sleep(interval / 1000)
|
|
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
|
|
|
|
|
async def connect():
|
|
async with websockets.connect(GATEWAY_URL) as websocket:
|
|
response = json.loads(await websocket.recv())
|
|
heartbeat_interval = response["d"]["heartbeat_interval"]
|
|
|
|
asyncio.create_task(heartbeat(websocket, heartbeat_interval))
|
|
|
|
await identify(websocket)
|
|
|
|
while True:
|
|
message = json.loads(await websocket.recv())
|
|
print("Received: ", message)
|
|
|
|
|
|
asyncio.run(connect())
|
|
# send_message(channel_id=840107060901052426, message="Salut")
|