bluenix2 / discord-gateway Goto Github PK
View Code? Open in Web Editor NEWSans-I/O implementation of the Discord gateway
License: MIT License
Sans-I/O implementation of the Discord gateway
License: MIT License
`
import socket
import ssl
import threading
import time
from sys import platform
import certifi
from discord_gateway import DiscordConnection
import socks # pip install PySocks
TOKEN = '********************************'
RECV_SIZE = 65536
SERVER_NAME = 'gateway.discord.gg'
socks.set_default_proxy(socks.HTTP, "127.0.0.1", 7890)
socket.socket = socks.socksocket
def heartbeat(conn, sock):
while True:
sock.send(conn.heartbeat())
time.sleep(conn.heartbeat_interval)
def recv_event(conn, sock):
while True:
for event in conn.events():
return event
for to_send in conn.receive(sock.recv(RECV_SIZE)):
sock.send(to_send)
def main():
# Setup the socket and SSL for the WebSocket Secure connection.
conn = DiscordConnection(SERVER_NAME, encoding='json')
ctx = ssl.create_default_context(cafile=certifi.where())
sock = socket.create_connection(conn.destination)
sock = ctx.wrap_socket(sock, server_hostname=SERVER_NAME)
sock.send(conn.connect()) # Convert to a WebSocket
# Receive the very first HELLO event.
hello = recv_event(conn, sock)
# Send RESUME or IDENTIFY depending on state (will always be False
# when initially connecting, but may be different when reconnecting).
if conn.should_resume:
sock.send(conn.resume(TOKEN))
else:
sock.send(conn.identify(
token=TOKEN,
intents=65535,
properties={
'$os': platform,
'$browser': 'discord-gateway',
'$device': 'discord-gateway'
},
))
heartbeater = threading.Thread(target=heartbeat, args=(conn, sock))
heartbeater.start()
try:
while True:
event = recv_event(conn, sock)
print('Received:', event)
finally:
sock.shutdown(socket.SHUT_WR)
sock.close()
if name == 'main':
main()`
`
Traceback (most recent call last):
File "F:\code\dc-monitor\dc_websocket.py", line 78, in
main()
File "F:\code\dc-monitor\dc_websocket.py", line 41, in main
sock = socket.create_connection(conn.destination)
^^^^^^^^^^^^^^^^
File "F:\code\dc-monitor\venv\Lib\site-packages\discord_gateway\conn.py", line 171, in destination
raise ValueError(f"Cannot parse hostname out of URI '{self.uri}'")
ValueError: Cannot parse hostname out of URI 'gateway.discord.gg'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.