Skip to main content

Python framework for Discord interactions.

Project description

discsocket

Python framework for Discord interactions.

Installation

pip install discsocket

Introduction

This is the code needed for a minimal application with an on_ready event

import discsocket

socket = discsocket.Socket()

# Event names go in the event decorator
# The function can be named whatever
@socket.event('ready')
async def ready():
  print(f"{socket.user.username} is connected")
 
socket.run('token')

or if you want to make the bot function as a class

import discsocket

class Socket(discsocket.Socket):
    def __init__(self):
        super().__init__(gateway_version=8)

    # Events in a class structure won't require a decorator
    # and instead follow the 'on_' + gateway_event format

    async def on_ready(self):
        print(f"{self.user.username} is online")

if __name__ == '__main__':
    Socket().run('token')

Extensions

Extensions work to separate your code into different files so it is not all in a single file

import discsocket
from discsocket import ext

class Boop(ext.Extension):
    def __init__(self, socket):
        self.socket = socket

    # Example of a command within an extension
    @ext.Extension.command('boop', discsocket.utils.SLASH)
    async def boop(self, context: discsocket.models.BaseContext):
        await context.callback(content='boop!')

    # Example of a listener within an extension
    @ext.Extension.listener('message_create')
    async def message(self, message):
        print(message['content'])

def init_ext(socket):
    socket.add_ext(Boop(socket))

As an example, the above extension is in a folder called 'extensions'

import discsocket
import pathlib

class Socket(discsocket.Socket):
    def __init__(self):
        super().__init__(gateway_version=8)
        self.load()

    def load(self):
        for ext in [f'{p.parent}.{p.stem}' for p in pathlib.Path('extensions').glob('*.py')]:
            try:
                self.add_extension(ext)
            except Exception as e:
                print(f"Failed to load {ext}.\n-> {e}")
            else:
                print(f"Loaded {ext}")

    async def on_ready(self):
        print(f"{self.user.username} is online")

if __name__ == '__main__':
    Socket().run('token')

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

discsocket-1.1.6.tar.gz (12.4 kB view details)

Uploaded Source

File details

Details for the file discsocket-1.1.6.tar.gz.

File metadata

  • Download URL: discsocket-1.1.6.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for discsocket-1.1.6.tar.gz
Algorithm Hash digest
SHA256 d715f78c6886bef4c852098253a8988b2441d27056435d0abd0e6d58fc20ef3d
MD5 340417962d96147810b26cf657d78af6
BLAKE2b-256 4de27855408c1e758a4330a07fc553cb4d658085d80240fde77c138986e1acb0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page