Gitsune (vosjedev.net)
login
To clone locally:
$ git clone https://betagit.vosjedev.net/git/python-discord.git
Last commit: 5c32f2f509e1 idk anymore, stuff that may or may not be temporary[checkout]
NameSizeMode
.gitignore25B100644
README.md3.65KiB100644
poetry.lock21.96KiB100644
pyproject.toml559B100644
src/-040000
utils/-040000
README.md

Yet another discord library for python??

This is a discord library that provides a low-level interface to the discord API. You'll need discord's API documentation open for which parameters a function accepts to use this.

They key defining point of this library is that it's the only one that doesn't use asyncio (as far as I have been able to find). No asyncio means we no longer are relient on every library we use to support asyncio, we can do whatever we want! It also means easy integration with libraries like cherrypy (for which this library includes a plugin under utils.cherrypy).

The organisation of the library is as follows:
- The discord api reference is used as basis
- In the left sidebar of the api reference, under HTTP API Resources, there is a whole list of resources. Expect to find the stuff described in the page for said resource to be in a submodule with the same name (for example all methods from the Message resource is under discord.message).
- The library adds a few new methods to control the library itself:
- The discord module itself contains 3 methods, see below
- discord.gateway allows access to the gateway, see below

The discord module

Apart from the many submodules equivaling discord resources, the discord module houses 3 functions:

How to use the Gateway

Take a look at an example pingbot for a full example with explanations about what I'm doing.

Minimal example without comments:

-
from discord import gateway, message

def run():
    i=gateway.intents
    intents=i.GUILD_MESSAGES | i.DIRECT_MESSAGES  | i.MESSAGE_CONTENT

    def on_ready(client:gateway.DiscordWsConnection, data, payload):
        @client.event()
        def on_message_create(data, payload):
            content=data["content"]
            channel=data["channel_id"]
            if 'ping' in content:
                message.message_send(channel_id=channel, content="pong")

    gw=gateway.GatewayClient(connect_callback=on_ready, intents=intents)
    gw.start()

    # note keeping the program running is up to you. here's a premade idea:
    from time import sleep
    try:
        while True: sleep(3600) # sleep for an hour infinitely
    except KeyboardInterrupt:
        pass

    gw.stop()

if __name__=="__main__":
    run()

Things to keep in mind while using the gateway:
- GatewayClient.start() is not blocking, it is up to you to keep the program running as long as wanted. Above example shows a way to block until KeyboardInterrupts.
- Make sure the pass the right intents. Using gateway.intents.ALL is possible, but will use unnecessary resources of both discord's servers and your bot, so I recommend only passing the intents you need. Especially if your bot is in a few active servers, the amount of network traffic can can climb fast.