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
|
HELLO = 10
|
||||||
HEARTBEAT_ACK = 11
|
HEARTBEAT_ACK = 11
|
||||||
REQUEST_SOUNDBOARD_SOUNDS = 31
|
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
|
import asyncio
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import websockets
|
import websockets
|
||||||
import json
|
import json
|
||||||
from enums import OpCode
|
|
||||||
|
from websockets import Response
|
||||||
|
|
||||||
|
from enums import OpCode, EventTitle
|
||||||
|
|
||||||
with open('configuration.json', 'r') as file:
|
with open('configuration.json', 'r') as file:
|
||||||
CONFIG = json.load(file)
|
CONFIG = json.load(file)
|
||||||
|
|
||||||
GATEWAY_URL = "wss://gateway.discord.gg/?v=10&encoding=json"
|
GATEWAY_URL: str = "wss://gateway.discord.gg/?v=10&encoding=json"
|
||||||
API_URL = "https://discord.com/api/v10"
|
API_URL: str = "https://discord.com/api/v10"
|
||||||
TOKEN = CONFIG["TOKEN"]
|
TOKEN: str = CONFIG["TOKEN"]
|
||||||
|
|
||||||
|
|
||||||
async def send_message(channel_id: int, message: str):
|
async def send_message(channel_id: int, message: str):
|
||||||
body = {
|
body: dict= {
|
||||||
"content": message,
|
"content": message,
|
||||||
"tts": False,
|
"tts": False,
|
||||||
"embeds": [{
|
"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)
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
async def identify(websocket):
|
async def identify(websocket):
|
||||||
payload = {
|
payload: dict = {
|
||||||
"op": OpCode.IDENTIFY,
|
"op": OpCode.IDENTIFY,
|
||||||
"d": {
|
"d": {
|
||||||
"token": TOKEN,
|
"token": TOKEN,
|
||||||
"properties": {
|
"properties": {
|
||||||
"os": "windows",
|
"os": "linux",
|
||||||
"browser": "gambling",
|
"browser": "gambling",
|
||||||
"device": "gambling"
|
"device": "gambling"
|
||||||
},
|
},
|
||||||
"intents": 513,
|
"intents": 65024,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await websocket.send(json.dumps(payload))
|
await websocket.send(json.dumps(payload))
|
||||||
@ -47,6 +52,13 @@ async def heartbeat(websocket, interval):
|
|||||||
await asyncio.sleep(interval / 1000)
|
await asyncio.sleep(interval / 1000)
|
||||||
await websocket.send(json.dumps({"op": OpCode.HEARTBEAT, "d": None}))
|
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 def connect():
|
||||||
async with websockets.connect(GATEWAY_URL) as websocket:
|
async with websockets.connect(GATEWAY_URL) as websocket:
|
||||||
@ -58,9 +70,18 @@ async def connect():
|
|||||||
await identify(websocket)
|
await identify(websocket)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
message = json.loads(await websocket.recv())
|
response = json.loads(await websocket.recv())
|
||||||
print("Received: ", message)
|
#print("Received: ", response)
|
||||||
|
match response["op"]:
|
||||||
|
case OpCode.DISPATCH:
|
||||||
|
await get_event(response)
|
||||||
|
case _:
|
||||||
|
print("osef")
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(connect())
|
async def main():
|
||||||
# send_message(channel_id=840107060901052426, message="Salut")
|
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