Skip to main content
PyPI Documentation Status License https://img.shields.io/badge/code%20style-black-000000.svg https://github.com/LiBa001/discord-interactions.py/workflows/Python%20package/badge.svg

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

Furthermore, it allows for strict separation between your commands’ structure and and the data that is received when triggering it.

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

If this doesn’t work, you might try:

python -m pip install ...

Or if you are on windows:

py -m pip install ...

Use with Flask

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

Using API-like Data Classes

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(
        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.

Let’s make it a bit simpler:

@interactions.command(echo_cmd)
def _echo(interaction: Interaction):
    # different way of getting an option
    msg = interaction.data.get_option("message").value

    return msg

Now, we don’t need to deal with InteractionResponse anymore, but instead just return the response content as a string. The response type then defaults to InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE. You could also just return None, if you don’t want to send a response. You can also simply return a boolean as a second value, indicating whether or not the command call should be displayed in Discord (i.e. the _WITH_SOURCE part of the response type). Also we get the option via the get_option helper method.

The Object-Command Mapper

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

Followup Messages

If you want to send messages after the initial response, you need to create followup messages. For this purpose you can use the after_command decorator, that registers a function to be called after the actual command function has returned. The function needs to take exactly one parameter, the AfterCommandContext, which contains the several things, like the Interaction and initial InteractionResponse.

interactions = Interactions(app, PUBLIC_KEY)

@interactions.command("delay")
def delay(_: Interaction):
    return "starting countdown", True  # this message is ephemeral


@delay.after_command
def after_delay(ctx: AfterCommandContext):
    delay_time = ctx.interaction.data.options[0].value
    time.sleep(delay_time)
    ctx.send(f"{delay_time} seconds have passed")

Message Components

You can also register callbacks for message components, such as buttons. Components are registered and identified by their custom_id.

@interactions.component("my_button")
def my_button_handler(ctx: ComponentContext):
    return f"{ctx.interaction.user.username} clicked the button"

More Examples

For more examples of the different features take a look at examples.

If you want to know how to make your Discord bot work with Slash Commands and how to set everything up, take a look at this example project. It hosts the program in a serverless environment via Google Cloud Run and also provides a demo bot, so you can try out Slash Commands in your Discord server. Check it out to learn more!

Metadata

Release files for discord-interactions.py 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for discord-interactions.py 0.0.8
File Size Uploaded
discord-interactions.py-0.0.8.tar.gz 23.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for discord-interactions.py 0.0.8
File Interpreter ABI Platform
discord_interactions.py-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 65.6 kB

Release files / discord-interactions.py-0.0.8.tar.gz

Download URL discord-interactions.py-0.0.8.tar.gz
Size 23.1 kB
Tags Source
SHA-256 checksum
How to use checksums
892f5c54a8cfa143ed11d52f41869242e54e387fc9041da9af526ea80af23682
BLAKE2b-256 checksum
How to use checksums
1fef59ea86a43c3dbd0669952f36eb33c7e38874086693ff186aa5c01e566333
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / discord_interactions.py-0.0.8-py3-none-any.whl

Download URL discord_interactions.py-0.0.8-py3-none-any.whl
Size 42.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a503d8e5015577f58bf4573ca1a9ce2cea105d551328a93dd69463521810c66
BLAKE2b-256 checksum
How to use checksums
42f0c74fd6f7ac7a31cf6d3efa34feb7950846d909c3389ff5e4b67bdff805e1
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page