Skip to main content

A library around the Discord Interactions API

Project description

PyPI License https://img.shields.io/badge/code%20style-black-000000.svg

A wrapper for the Discord Interactions API that does not rely on websockets and can therefore be used in a stateless webhook environment.

Installation

Requires Python 3.8+

  • latest release from PyPI using pip:

    pip install discord-interactions.py

  • latest commit from GitHub using pip and git:

    pip install git+https://github.com/LiBa001/discord-interactions.py

Use with Flask

This library is specifically designed to work seamlessly with the Flask microframework.

The most API-like example with the flask extension is this:

from discord_interactions.flask_ext import Interactions
from discord_interactions import (
    ApplicationCommand,
    ApplicationCommandOption,
    ApplicationCommandOptionType,
    Interaction,
    InteractionResponse,
    InteractionResponseType,
    InteractionApplicationCommandCallbackData,
)
from flask import Flask
import os

app = Flask(__name__)
interactions = Interactions(app, os.getenv("CLIENT_PUBLIC_KEY"))

echo_cmd = ApplicationCommand("echo", "what goes around comes around")
echo_cmd.add_option(
    ApplicationCommandOption(
        type=ApplicationCommandOptionType.STRING,
        name="message",
        description="This will be echoed.",
        required=True,
    )
)


@interactions.command(echo_cmd)
def _echo(interaction: Interaction):
    msg = interaction.data.options[0].value  # "message" option content

    return InteractionResponse(
        response_type=InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        data=InteractionApplicationCommandCallbackData(content=msg),
    )

Here, we use the rudimentary ApplicationCommand, Interaction and InteractionResponse classes, which are in their structure basically exact counterparts of the original API models.

This library provides another abstraction layer, though. Inspired by the concept of database ORMs, it has an Object-Command Mapper (OCM) that lets you define a class for each command which will then serve as both a generic structural description of the command (like ApplicationCommand) and a container for the actual data that is received when the command is called (like Interaction).

So, the simplest possible example looks like this:

from discord_interactions.flask_ext import Interactions
from discord_interactions.ocm import Command, Option
from flask import Flask
import os

app = Flask(__name__)
interactions = Interactions(app, os.getenv("CLIENT_PUBLIC_KEY"))


class _Echo(Command):
    """ what goes around comes around """

    message: str = Option("This will be echoed.", required=True)


@interactions.command
def _echo(cmd: _Echo):
    return cmd.message

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

discord-interactions.py-0.0.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

discord_interactions.py-0.0.2-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file discord-interactions.py-0.0.2.tar.gz.

File metadata

  • Download URL: discord-interactions.py-0.0.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.0

File hashes

Hashes for discord-interactions.py-0.0.2.tar.gz
Algorithm Hash digest
SHA256 828392b740d6129cea562b5f2868357ee1b39bbc1943bf22752f842137c238f0
MD5 4dd231ad2bd8b174b2f6ca19df2b0634
BLAKE2b-256 2fd998909e91a7d593d1be359c97fdc4f65033d199bb0fd57815a2d151279586

See more details on using hashes here.

File details

Details for the file discord_interactions.py-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: discord_interactions.py-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.0

File hashes

Hashes for discord_interactions.py-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b3291aae85b2015046a8219252fe0b63ccedea9c79fcae388de0e49d7f13b56
MD5 93559dcc7a265b45578f053aa4546694
BLAKE2b-256 913174d1378f8646e6185d35a2ee634f315e5d3b78064219013f6e615b852a03

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