Gateway implementation for the Wumpy project
Project description
Wumpy-gateway
Gateway implementation for the Wumpy project.
Installation
Wumpy-gateway is available as a package on PyPI. Just use your favourite package manager such as pip or Poetry:
# Installing from PyPI using pip:
pip install wumpy-gateway
# Alternatively, using Poetry:
poetry add wumpy-gateway
Quickstart
The easiest - and recommended - way to get started is using Shard.connect()
:
from wumpy.gateway import Shard
INTENTS = 65535
TOKEN = 'ABC123.XYZ789'
async def main():
# Connect to the URI wss://gateway.discord.gg/ with the token
# ABC123.XYZ8789 and all intents.
async with Shard.connect('wss://gateway.discord.gg/', TOKEN, INTENTS) as ws:
async for event in ws:
print(event) # The deserialized JSON event payload
The connect()
classmethod is an abstraction that also spawns a task to handle
the heartbeating. It is not recommended but can re-implemented like this:
import anyio
from wumpy.gateway import Shard
TOKEN = 'ABC123.XYZ789'
async def handle_event(event):
print(event)
async def main():
conn, sock = await Shard.create_connection(
'wss://gateway.discord.gg/', TOKEN, INTENTS
)
ws = Shard(conn, sock, TOKEN, INTENTS)
async with anyio.create_task_group() as tg:
tg.start_soon(ws.run_heartbeater)
try:
async for event in ws:
await handle_event(event)
finally:
# Cancel the heartbeater task - otherwise this will loop forever
tg.cancel_scope.cancel()
# Cleanup the gateway connection (handles closing the socket)
await ws.aclose()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wumpy-gateway-0.1.1.tar.gz
(7.1 kB
view hashes)
Built Distribution
Close
Hashes for wumpy_gateway-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | decd12a37ffd0ff3b717d232364f856e52c8542bbe5dab19eaadaa02901c7fd5 |
|
MD5 | 692e3a06a412371f772b2cdd60141f9f |
|
BLAKE2b-256 | 0d58a1df811d01561dbf2f0fb05501cd378e33645168fc275660d11611abaaae |